Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Directive, Input, TemplateRef, ViewContainerRef} from "@angular/core";
- import {AuthService} from "../services/auth.service";
- @Directive({
- selector:"[rbacShow]"
- })
- export class RbacShowDirective {
- allowedRoles:string[];
- constructor(
- private templateRef: TemplateRef<any>,
- private viewContainer: ViewContainerRef,
- private authService: AuthService) {
- }
- @Input()
- set rbacShow(allowedRoles: string[]) {
- this.allowedRoles = allowedRoles;
- if (!this.allowedRoles || this.allowedRoles.length === 0 ||
- !this.authService.currentUser) {
- this.viewContainer.clear();
- return;
- }
- const allowed:boolean = this.authService.currentUser.roles.filter(
- role=>this.allowedRoles.includes(role)).length > 0;
- if (allowed) {
- this.viewContainer.createEmbeddedView(this.templateRef);
- }
- else {
- this.viewContainer.clear();
- }
- }
- }
Add Comment
Please, Sign In to add comment