import { Directive , ElementRef , Input , TemplateRef , ViewContainerRef } from '@angular/core' ; import { PermissionService } from '../services/permission.service' ; import { AclPermissionService } from '../services/acl-permission.service' ; import { AccessControlList } from '../../acl/acl.types' ; @ Directive ( { selector : '[permissionLink]' , exportAs : 'permissionLink' } ) export class PermissionLinkDirective { private rightNames : string [ ] = [ ] ; private requirements : string [ ] | string ; private acl : AccessControlList ; constructor ( private element , private templateRef , private viewContainer , private permissionService , private aclPermissionService ) { } @ Input ( 'permissionLinkRequirements' ) set permissionLinkRequirements ( requirements ) { this . requirements = requirements ; } @ Input ( 'permissionLink' ) set permissionLink ( rightNames ) { if ( rightNames !== undefined ) { if ( Array . isArray ( rightNames ) ) { this . rightNames = rightNames ; } else { this . rightNames = [ ] ; this . rightNames . push ( rightNames ) ; } } this . updateView ( ) ; } @ Input ( 'permissionLinkAcl' ) set permissionLinkType ( acl ) { this . acl = acl ; this . updateView ( ) ; } @ Input ( ) routerLink : string = undefined ; private checkPermission ( ) { let hasPermission = false ; for ( const right of this . rightNames ) { if ( this . permissionService . hasRight ( right ) || this . permissionService . hasExtendedRight ( right ) ) { hasPermission = true ; } else { hasPermission = false ; break ; } } if ( this . requirements && this . acl && hasPermission ) { const aclperms = this . aclPermissionService . checkRights ( this . acl , this . requirements ) ; if ( aclperms !== null ) { hasPermission = aclperms ; } } return hasPermission ; } private updateView ( ) { this . viewContainer . clear ( ) ; if ( this . checkPermission ( ) ) { this . viewContainer . createEmbeddedView ( this . templateRef ) ; } } }