Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
feat(directives): Add ifUserInGroup and ifUser directives
Browse files Browse the repository at this point in the history
Contributes to #30.
  • Loading branch information
mraible committed Dec 29, 2016
1 parent b9ff688 commit 5c9fa3a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 3 deletions.
5 changes: 4 additions & 1 deletion demo/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import { Account } from '../src/shared/account';
</div>
<sp-authport></sp-authport>
<h3 ifUser>Directives</h3>
<a href="#" ifUserInGroup="admin">Only show for 'admin' group</a><br>
<span ifUser>Only show if user logged in</span>
</div>
`,
providers: [Stormpath]
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
10 changes: 8 additions & 2 deletions src/stormpath.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { httpFactory } from './stormpath/stormpath.http';
import { EmailVerificationComponent } from './email-verification/email-verification.component';
import { ResetPasswordComponent } from './reset-password/reset-password.component';
import { ResendEmailVerificationComponent } from './resend-email-verification/resend-email-verification.component';
import { IfUserInGroupDirective } from './user/if-user-in-group.directive';
import { IfUserDirective } from './user/if-user.directive';

@NgModule({
declarations: [
Expand All @@ -22,7 +24,9 @@ import { ResendEmailVerificationComponent } from './resend-email-verification/re
RegisterComponent,
EmailVerificationComponent,
ResetPasswordComponent,
ResendEmailVerificationComponent
ResendEmailVerificationComponent,
IfUserDirective,
IfUserInGroupDirective
],
imports: [CommonModule, FormsModule, HttpModule],
exports: [
Expand All @@ -32,7 +36,9 @@ import { ResendEmailVerificationComponent } from './resend-email-verification/re
RegisterComponent,
EmailVerificationComponent,
ResetPasswordComponent,
ResendEmailVerificationComponent
ResendEmailVerificationComponent,
IfUserDirective,
IfUserInGroupDirective,
],
providers: [Stormpath, StormpathConfiguration, LoginService,
{
Expand Down
9 changes: 9 additions & 0 deletions src/stormpath/stormpath.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ export class Stormpath {
.catch(this.errorTranslator);
}

isInGroup(authorities: any, groups: Array<any>): boolean {
// if at least one authority matches, allow
return authorities.filter(authority => this.inGroup(authority, groups)).length > 0;
}

private inGroup(groupName: string, groups: Array<any>): 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
Expand Down
31 changes: 31 additions & 0 deletions src/user/if-user-in-group.directive.ts
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);
}
}
}
31 changes: 31 additions & 0 deletions src/user/if-user.directive.ts
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();
}
}
}
2 changes: 2 additions & 0 deletions src/user/index.ts
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'

0 comments on commit 5c9fa3a

Please sign in to comment.