This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(directives): Add ifUserInGroup and ifUser directives
Contributes to #30.
- Loading branch information
Showing
7 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Directive, ElementRef, Input, Renderer, OnInit } from '@angular/core'; | ||
import { Stormpath } from '../stormpath/index'; | ||
import { IfUserDirective } from './if-user.directive'; | ||
|
||
@Directive({ | ||
selector: '[ifUserInGroup]' | ||
}) | ||
export class IfUserInGroupDirective extends IfUserDirective { | ||
@Input() ifUserInGroup: string; | ||
private authority: string[]; | ||
|
||
ngOnInit(): void { | ||
this.authority = this.ifUserInGroup.replace(/\s+/g, '').split(','); | ||
this.stormpath.user$.subscribe(response => this.setVisibility(response)); | ||
} | ||
|
||
protected setVisibility(account: any): void { | ||
if (account === false) { | ||
this.setHidden(); | ||
// use == instead of === here because triple doesn't detect undefined | ||
} else if (account && account['groups'] == null) { | ||
// handle the fact that /login doesn't result groups | ||
this.stormpath.getAccount().subscribe(response => { | ||
return this.setVisibility(response); | ||
}); | ||
} else { | ||
let result = this.stormpath.isInGroup(this.authority, account['groups'].items); | ||
super.setVisibility(result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Directive, ElementRef, Renderer, OnInit } from '@angular/core'; | ||
import { Stormpath } from '../stormpath/index'; | ||
|
||
@Directive({ | ||
selector: '[ifUser]' | ||
}) | ||
export class IfUserDirective implements OnInit { | ||
|
||
constructor(protected stormpath: Stormpath, protected el: ElementRef, protected renderer: Renderer) { | ||
} | ||
|
||
ngOnInit(): void { | ||
this.stormpath.user$.subscribe(response => this.setVisibility(response)); | ||
} | ||
|
||
protected setVisible(): void { | ||
this.renderer.setElementClass(this.el.nativeElement, 'hidden', false); | ||
} | ||
|
||
protected setHidden(): void { | ||
this.renderer.setElementClass(this.el.nativeElement, 'hidden', true); | ||
} | ||
|
||
protected setVisibility(result: any): void { | ||
if (result) { | ||
this.setVisible(); | ||
} else { | ||
this.setHidden(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './if-user.directive' | ||
export * from './if-user-in-group.directive' |