/** Angular Imports */ import { Directive , Input , TemplateRef , ViewContainerRef } from '@angular/core' ; /** Custom Services */ import { AuthenticationService } from '../../core/authentication/authentication.service' ; /**
 * Has Permission Directive
 */ @ Directive ( { selector : '[mifosxHasPermission]' } ) export class HasPermissionDirective { /** User Permissions */ private userPermissions : any [ ] ; param { TemplateRef } templateRef param { ViewContainerRef } viewContainer param { AuthenticationService } authenticationService constructor ( private templateRef , private viewContainer , private authenticationService ) { const savedCredentials = this . authenticationService . getCredentials ( ) ; this . userPermissions = savedCredentials . permissions ; } /**
     * Evaluates the condition to show template.
     */ @ Input ( ) set mifosxHasPermission ( permission ) { if ( typeof permission !== 'string' ) { throw new Error ( 'hasPermission value must be a string' ) ; } /** Clear the template beforehand to prevent overlap OnChanges. */ this . viewContainer . clear ( ) ; /** Shows Template if user has permission */ if ( this . hasPermission ( permission ) ) { this . viewContainer . createEmbeddedView ( this . templateRef ) ; } } param { string } permission returns { true } returns { false } private hasPermission ( permission ) { permission = permission . trim ( ) ; if ( this . userPermissions . includes ( 'ALL_FUNCTIONS' ) ) { return true ; } else if ( permission !== '' ) { if ( permission . substring ( 0 , 5 ) === 'READ_' && this . userPermissions . includes ( 'ALL_FUNCTIONS_READ' ) ) { return true ; } else if ( this . userPermissions . includes ( permission ) ) { return true ; } else { return false ; } } else { return false ; } } }