Guest User

Untitled

a guest
Feb 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import {Directive, Input, TemplateRef, ViewContainerRef} from "@angular/core";
  2. import {AuthService} from "../services/auth.service";
  3.  
  4. @Directive({
  5. selector:"[rbacShow]"
  6. })
  7. export class RbacShowDirective {
  8.  
  9. allowedRoles:string[];
  10. constructor(
  11. private templateRef: TemplateRef<any>,
  12. private viewContainer: ViewContainerRef,
  13. private authService: AuthService) {
  14. }
  15.  
  16. @Input()
  17. set rbacShow(allowedRoles: string[]) {
  18. this.allowedRoles = allowedRoles;
  19. if (!this.allowedRoles || this.allowedRoles.length === 0 ||
  20. !this.authService.currentUser) {
  21. this.viewContainer.clear();
  22. return;
  23. }
  24.  
  25. const allowed:boolean = this.authService.currentUser.roles.filter(
  26. role=>this.allowedRoles.includes(role)).length > 0;
  27.  
  28. if (allowed) {
  29. this.viewContainer.createEmbeddedView(this.templateRef);
  30. }
  31. else {
  32. this.viewContainer.clear();
  33. }
  34. }
  35. }
Add Comment
Please, Sign In to add comment