Skip to content

Commit

Permalink
feat: permission checker optional (#370)
Browse files Browse the repository at this point in the history
* feat: permission checker is optional for permission directive (#346)

* fix: package lock json version updated after npm i

---------

Co-authored-by: markuczy <[email protected]>
Co-authored-by: kim.tran <[email protected]>
  • Loading branch information
3 people authored Aug 6, 2024
1 parent 63ed32e commit 2e2a0c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 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)
}
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2e2a0c2

Please sign in to comment.