diff --git a/projects/common/src/fm/common.module.ts b/projects/common/src/fm/common.module.ts index 587ca1e..d70175a 100644 --- a/projects/common/src/fm/common.module.ts +++ b/projects/common/src/fm/common.module.ts @@ -33,7 +33,8 @@ 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 { PackageExistsDirective} from './components/package-exists/package-exists.directive'; -import { HasClaimDirective} from './components/has-claim/has-claim.directive'; +import { HasClaimDirective } from './components/has-claim/has-claim.directive'; +import { HasRoleDirective } from './components/has-role/has-role.directive'; import { UserMenuComponent} from './components/user-menu/user-menu.component'; import { ThumbnailComponent } from './components/thumbnail/thumbnail.component'; import { Alert } from './enumerations/alert.enum'; @@ -85,6 +86,7 @@ export { HasPackageDirective, PackageExistsDirective, HasClaimDirective, + HasRoleDirective, Alert, IEventMessage, IItem, @@ -148,6 +150,7 @@ export { HasPackageDirective, PackageExistsDirective, HasClaimDirective, + HasRoleDirective, UserMenuComponent, GradientComponent, GradientSelectComponent, @@ -179,6 +182,7 @@ export { HasPackageDirective, PackageExistsDirective, HasClaimDirective, + HasRoleDirective, UserMenuComponent, GradientComponent, GradientSelectComponent, diff --git a/projects/common/src/fm/components/has-role/has-role.directive.ts b/projects/common/src/fm/components/has-role/has-role.directive.ts new file mode 100644 index 0000000..bd14c00 --- /dev/null +++ b/projects/common/src/fm/components/has-role/has-role.directive.ts @@ -0,0 +1,33 @@ +import { Directive, ViewContainerRef,TemplateRef,OnInit,Input, OnDestroy } from '@angular/core'; +import { Store} from '@ngrx/store'; +import * as appCommonReducer from '../../reducers/app-common.reducer' +import { Observable, Subscription } from 'rxjs'; +import { IUser } from '../../models/user'; + +@Directive({ + selector: '[fmHasRole]', +}) +export class HasRoleDirective implements OnInit, OnDestroy{ + @Input('fmHasRole') role:string; + + constructor(private templateRef$: TemplateRef,private viewContainerRef$: ViewContainerRef,private store$: Store) { } + private user$:Observable = this.store$.select(appCommonReducer.SelectGetUser); + private hasView = false; + private roleClaim = 'role'; + private sub: Subscription; + ngOnInit() { + this.sub = this.user$.subscribe((user) => { + console.log('fmHasRole', this.role); + if (user && user.claims[this.roleClaim]?.includes(this.role)) { + this.viewContainerRef$.createEmbeddedView(this.templateRef$); + this.hasView=true; + } else if (this.hasView) { + this.viewContainerRef$.clear(); + this.hasView = false; + } + }); + } + ngOnDestroy() { + if (this.sub) {this.sub.unsubscribe() }; + } +} \ No newline at end of file diff --git a/src/app/menu/menu.component.html b/src/app/menu/menu.component.html index 4508368..ada21fd 100644 --- a/src/app/menu/menu.component.html +++ b/src/app/menu/menu.component.html @@ -14,6 +14,7 @@