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

feat(directives): Add ifUserInGroup and ifUser directives #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion demo/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import { Account, Stormpath } from 'angular-stormpath';
</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 @@ -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: [
Expand All @@ -26,7 +28,9 @@ import { CookieService } from 'angular2-cookie/core';
RegisterComponent,
EmailVerificationComponent,
ResetPasswordComponent,
ResendEmailVerificationComponent
ResendEmailVerificationComponent,
IfUserDirective,
IfUserInGroupDirective
],
imports: [CommonModule, FormsModule, HttpModule, Ng2Webstorage],
exports: [
Expand All @@ -36,7 +40,9 @@ import { CookieService } from 'angular2-cookie/core';
RegisterComponent,
EmailVerificationComponent,
ResetPasswordComponent,
ResendEmailVerificationComponent
ResendEmailVerificationComponent,
IfUserDirective,
IfUserInGroupDirective,
],
providers: [
EventManager, LocalStorageTokenStoreManager, CookieTokenStoreManager, CookieService,
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 @@ -241,6 +241,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'