diff --git a/demo/app.component.ts b/demo/app.component.ts index de6fb97..fbd6526 100644 --- a/demo/app.component.ts +++ b/demo/app.component.ts @@ -23,7 +23,10 @@ import { Account, Stormpath } from 'angular-stormpath'; - + +

Directives

+ Only show for 'admin' group
+ Only show if user logged in `, providers: [Stormpath] diff --git a/src/index.ts b/src/index.ts index df267e3..e9011fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ export * from './stormpath.module'; // all components that will be codegen'd need to be exported for AOT to work +export * from './user/index'; export * from './authport/index'; export * from './email-verification/index'; export * from './forgot-password/index'; diff --git a/src/stormpath.module.ts b/src/stormpath.module.ts index e122a66..90660ba 100644 --- a/src/stormpath.module.ts +++ b/src/stormpath.module.ts @@ -17,6 +17,8 @@ import { LocalStorageTokenStoreManager, CookieTokenStoreManager } from './stormp import { Stormpath } from './stormpath/stormpath.service'; import { Ng2Webstorage } from 'ng2-webstorage'; import { CookieService } from 'angular2-cookie/core'; +import { IfUserInGroupDirective } from './user/if-user-in-group.directive'; +import { IfUserDirective } from './user/if-user.directive'; @NgModule({ declarations: [ @@ -26,7 +28,9 @@ import { CookieService } from 'angular2-cookie/core'; RegisterComponent, EmailVerificationComponent, ResetPasswordComponent, - ResendEmailVerificationComponent + ResendEmailVerificationComponent, + IfUserDirective, + IfUserInGroupDirective ], imports: [CommonModule, FormsModule, HttpModule, Ng2Webstorage], exports: [ @@ -36,7 +40,9 @@ import { CookieService } from 'angular2-cookie/core'; RegisterComponent, EmailVerificationComponent, ResetPasswordComponent, - ResendEmailVerificationComponent + ResendEmailVerificationComponent, + IfUserDirective, + IfUserInGroupDirective, ], providers: [ EventManager, LocalStorageTokenStoreManager, CookieTokenStoreManager, CookieService, diff --git a/src/stormpath/stormpath.service.ts b/src/stormpath/stormpath.service.ts index ed5720b..604c74a 100644 --- a/src/stormpath/stormpath.service.ts +++ b/src/stormpath/stormpath.service.ts @@ -241,6 +241,15 @@ export class Stormpath { .catch(this.errorTranslator); } + isInGroup(authorities: any, groups: Array): boolean { + // if at least one authority matches, allow + return authorities.filter(authority => this.inGroup(authority, groups)).length > 0; + } + + private inGroup(groupName: string, groups: Array): boolean { + return groups.filter(group => group.name == groupName).length > 0 + }; + /** * Returns the JSON error from an HTTP response, or a generic error if the * response is not a JSON error diff --git a/src/user/if-user-in-group.directive.ts b/src/user/if-user-in-group.directive.ts new file mode 100644 index 0000000..9cf7101 --- /dev/null +++ b/src/user/if-user-in-group.directive.ts @@ -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); + } + } +} diff --git a/src/user/if-user.directive.ts b/src/user/if-user.directive.ts new file mode 100644 index 0000000..cd50208 --- /dev/null +++ b/src/user/if-user.directive.ts @@ -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(); + } + } +} diff --git a/src/user/index.ts b/src/user/index.ts new file mode 100644 index 0000000..1b3a851 --- /dev/null +++ b/src/user/index.ts @@ -0,0 +1,2 @@ +export * from './if-user.directive' +export * from './if-user-in-group.directive'