Skip to content

Commit

Permalink
feat: permission checker is optional for permission directive
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Jul 29, 2024
1 parent ee95fd5 commit c41e316
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TemplateRef,
ViewContainerRef,
} from '@angular/core'
import { UserService } from '@onecx/angular-integration-interface'

export interface HasPermissionChecker {
hasPermission(permissionKey: string): boolean
Expand All @@ -20,7 +21,7 @@ export interface HasPermissionChecker {
*/
export class AlwaysGrantPermissionChecker implements HasPermissionChecker {
hasPermission(_permissionKey: string): boolean {
return true;
return true
}
}

Expand All @@ -42,16 +43,25 @@ export class IfPermissionDirective implements OnInit {
this.ocxIfPermissionPermissions = value
}

private permissionChecker: HasPermissionChecker | undefined
negate = false

constructor(
private renderer: Renderer2,
private el: ElementRef,
private viewContainer: ViewContainerRef,
@Optional()
@Inject(HAS_PERMISSION_CHECKER)
private permissionChecker: HasPermissionChecker,
@Optional() private templateRef?: TemplateRef<any>
) {}
private hasPermissionChecker?: HasPermissionChecker,
@Optional() private templateRef?: TemplateRef<any>,
@Optional() private userService?: UserService
) {
if (!(hasPermissionChecker || userService)) {
throw 'IfPermission requires UserService or HasPermissionChecker to be provided!'
}

this.permissionChecker = hasPermissionChecker ?? userService
}

ngOnInit() {
if (this.permission) {
Expand All @@ -77,6 +87,6 @@ export class IfPermissionDirective implements OnInit {
}
return result
}
return this.permissionChecker.hasPermission(permission)
return this.permissionChecker?.hasPermission(permission)
}
}

0 comments on commit c41e316

Please sign in to comment.