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

Commit 5c9fa3a

Browse files
committed
feat(directives): Add ifUserInGroup and ifUser directives
Contributes to #30.
1 parent b9ff688 commit 5c9fa3a

File tree

7 files changed

+86
-3
lines changed

7 files changed

+86
-3
lines changed

demo/app.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import { Account } from '../src/shared/account';
2727
</div>
2828
2929
<sp-authport></sp-authport>
30-
30+
31+
<h3 ifUser>Directives</h3>
32+
<a href="#" ifUserInGroup="admin">Only show for 'admin' group</a><br>
33+
<span ifUser>Only show if user logged in</span>
3134
</div>
3235
`,
3336
providers: [Stormpath]

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './stormpath.module';
22

33
// all components that will be codegen'd need to be exported for AOT to work
4+
export * from './user/index';
45
export * from './authport/index';
56
export * from './email-verification/index';
67
export * from './forgot-password/index';

src/stormpath.module.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { httpFactory } from './stormpath/stormpath.http';
1313
import { EmailVerificationComponent } from './email-verification/email-verification.component';
1414
import { ResetPasswordComponent } from './reset-password/reset-password.component';
1515
import { ResendEmailVerificationComponent } from './resend-email-verification/resend-email-verification.component';
16+
import { IfUserInGroupDirective } from './user/if-user-in-group.directive';
17+
import { IfUserDirective } from './user/if-user.directive';
1618

1719
@NgModule({
1820
declarations: [
@@ -22,7 +24,9 @@ import { ResendEmailVerificationComponent } from './resend-email-verification/re
2224
RegisterComponent,
2325
EmailVerificationComponent,
2426
ResetPasswordComponent,
25-
ResendEmailVerificationComponent
27+
ResendEmailVerificationComponent,
28+
IfUserDirective,
29+
IfUserInGroupDirective
2630
],
2731
imports: [CommonModule, FormsModule, HttpModule],
2832
exports: [
@@ -32,7 +36,9 @@ import { ResendEmailVerificationComponent } from './resend-email-verification/re
3236
RegisterComponent,
3337
EmailVerificationComponent,
3438
ResetPasswordComponent,
35-
ResendEmailVerificationComponent
39+
ResendEmailVerificationComponent,
40+
IfUserDirective,
41+
IfUserInGroupDirective,
3642
],
3743
providers: [Stormpath, StormpathConfiguration, LoginService,
3844
{

src/stormpath/stormpath.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ export class Stormpath {
194194
.catch(this.errorTranslator);
195195
}
196196

197+
isInGroup(authorities: any, groups: Array<any>): boolean {
198+
// if at least one authority matches, allow
199+
return authorities.filter(authority => this.inGroup(authority, groups)).length > 0;
200+
}
201+
202+
private inGroup(groupName: string, groups: Array<any>): boolean {
203+
return groups.filter(group => group.name == groupName).length > 0
204+
};
205+
197206
/**
198207
* Returns the JSON error from an HTTP response, or a generic error if the
199208
* response is not a JSON error
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Directive, ElementRef, Input, Renderer, OnInit } from '@angular/core';
2+
import { Stormpath } from '../stormpath/index';
3+
import { IfUserDirective } from './if-user.directive';
4+
5+
@Directive({
6+
selector: '[ifUserInGroup]'
7+
})
8+
export class IfUserInGroupDirective extends IfUserDirective {
9+
@Input() ifUserInGroup: string;
10+
private authority: string[];
11+
12+
ngOnInit(): void {
13+
this.authority = this.ifUserInGroup.replace(/\s+/g, '').split(',');
14+
this.stormpath.user$.subscribe(response => this.setVisibility(response));
15+
}
16+
17+
protected setVisibility(account: any): void {
18+
if (account === false) {
19+
this.setHidden();
20+
// use == instead of === here because triple doesn't detect undefined
21+
} else if (account && account['groups'] == null) {
22+
// handle the fact that /login doesn't result groups
23+
this.stormpath.getAccount().subscribe(response => {
24+
return this.setVisibility(response);
25+
});
26+
} else {
27+
let result = this.stormpath.isInGroup(this.authority, account['groups'].items);
28+
super.setVisibility(result);
29+
}
30+
}
31+
}

src/user/if-user.directive.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Directive, ElementRef, Renderer, OnInit } from '@angular/core';
2+
import { Stormpath } from '../stormpath/index';
3+
4+
@Directive({
5+
selector: '[ifUser]'
6+
})
7+
export class IfUserDirective implements OnInit {
8+
9+
constructor(protected stormpath: Stormpath, protected el: ElementRef, protected renderer: Renderer) {
10+
}
11+
12+
ngOnInit(): void {
13+
this.stormpath.user$.subscribe(response => this.setVisibility(response));
14+
}
15+
16+
protected setVisible(): void {
17+
this.renderer.setElementClass(this.el.nativeElement, 'hidden', false);
18+
}
19+
20+
protected setHidden(): void {
21+
this.renderer.setElementClass(this.el.nativeElement, 'hidden', true);
22+
}
23+
24+
protected setVisibility(result: any): void {
25+
if (result) {
26+
this.setVisible();
27+
} else {
28+
this.setHidden();
29+
}
30+
}
31+
}

src/user/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './if-user.directive'
2+
export * from './if-user-in-group.directive'

0 commit comments

Comments
 (0)