From f9d0e2aee08f93c11f07cb52542a346f60f81676 Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Tue, 23 Jun 2020 17:51:47 +0200 Subject: [PATCH] Implemented hasclaim directive --- projects/common/src/fm/common.module.ts | 4 +++ .../has-claim/has-claim.directive.ts | 30 +++++++++++++++++++ projects/common/src/fm/models/user.ts | 11 +++---- .../src/fm/reducers/app-common.reducer.ts | 7 ++++- 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 projects/common/src/fm/components/has-claim/has-claim.directive.ts diff --git a/projects/common/src/fm/common.module.ts b/projects/common/src/fm/common.module.ts index 9d4fc1c..fb8e444 100644 --- a/projects/common/src/fm/common.module.ts +++ b/projects/common/src/fm/common.module.ts @@ -31,6 +31,7 @@ import { TimespanComponent } from './components/timespan/timespan.component'; import { TagInputComponent } from './components/tag-input/tag-input.component'; import { MenuBackgroundComponent } from './components/menu-background/menu-background.component'; import { HasPackageDirective} from './components/has-package/has-package.directive'; +import { HasClaimDirective} from './components/has-claim/has-claim.directive'; import { UserMenuComponent} from './components/user-menu/user-menu.component'; import { Alert } from './enumerations/alert.enum'; import { IEventMessage } from './models/event.message'; @@ -63,6 +64,7 @@ export { TagInputComponent, UserMenuComponent, HasPackageDirective, + HasClaimDirective, Alert, IEventMessage, IItem, @@ -112,6 +114,7 @@ export { SessionClearedComponent, MenuBackgroundComponent, HasPackageDirective, + HasClaimDirective, UserMenuComponent ], exports: [ @@ -131,6 +134,7 @@ export { SessionClearedComponent, MenuBackgroundComponent, HasPackageDirective, + HasClaimDirective, UserMenuComponent ] }) diff --git a/projects/common/src/fm/components/has-claim/has-claim.directive.ts b/projects/common/src/fm/components/has-claim/has-claim.directive.ts new file mode 100644 index 0000000..597e38d --- /dev/null +++ b/projects/common/src/fm/components/has-claim/has-claim.directive.ts @@ -0,0 +1,30 @@ +import { Directive, ViewContainerRef,TemplateRef,OnInit,Input,OnDestroy } from '@angular/core'; +import { Store} from '@ngrx/store'; +import * as appCommonReducer from '../../reducers/app-common.reducer' +import { IPackages } from '../../models/package'; +import { Observable, Subscription } from 'rxjs'; +import { skip } from 'rxjs/operators'; +import {OAuthService } from 'angular-oauth2-oidc'; +import { IUser } from '../../models/user'; + +@Directive({ + selector: '[fm-hasclaim]', +}) +export class HasClaimDirective implements OnInit{ + @Input('fm-hasclaim') claim:string; + + constructor(private templateRef$: TemplateRef,private viewContainerRef$: ViewContainerRef,private store$: Store) { } + private user$:Observable = this.store$.select(appCommonReducer.SelectGetUser).pipe(skip(1)); + private hasView = false; + ngOnInit() { + this.user$.subscribe((user) => { + if (user.claims[this.claim]) { + this.viewContainerRef$.createEmbeddedView(this.templateRef$); + this.hasView=true; + } else if (this.hasView) { + this.viewContainerRef$.clear(); + this.hasView = false; + } + }); + } +} \ No newline at end of file diff --git a/projects/common/src/fm/models/user.ts b/projects/common/src/fm/models/user.ts index 80e6efe..fd41620 100644 --- a/projects/common/src/fm/models/user.ts +++ b/projects/common/src/fm/models/user.ts @@ -1,5 +1,6 @@ -export interface IUser { - code?: string; - name?: string; - email?: string; -} +export interface IUser { + code?: string; + name?: string; + email?: string; + claims: any; +} diff --git a/projects/common/src/fm/reducers/app-common.reducer.ts b/projects/common/src/fm/reducers/app-common.reducer.ts index ec47ac0..426f523 100644 --- a/projects/common/src/fm/reducers/app-common.reducer.ts +++ b/projects/common/src/fm/reducers/app-common.reducer.ts @@ -36,10 +36,15 @@ export function reducer(state = initialState, action: appCommonActions.Actions ) switch (action.type) { case appCommonActions.INITUSERSUCCESS: { let a = action as appCommonActions.InitUserSuccess; + let claims = {} + Object.getOwnPropertyNames(a.userinfo).forEach((k) => { + claims[k] = a.userinfo[k]; + }); var user:IUser = { code:a.user.code, email:a.userinfo["email"], - name:a.userinfo["name"] + name:a.userinfo["name"], + claims:claims }; return tassign(state, { user: user,initialized: true }); }