Skip to content

Commit

Permalink
feat: add NgxSecurityGuardOptions.resourceResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
mselerin committed Mar 29, 2023
1 parent ac5432e commit 3dae346
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
16 changes: 11 additions & 5 deletions projects/ngx-security/src/lib/guards/ngx-security.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class NgxSecurityGuard implements CanLoad, CanActivate, CanActivateChild
}

handle(guardOptions: NgxSecurityGuardOptions, route?: CurrentRoute, state?: RouteUrl): Observable<boolean | UrlTree> {
return this.checkAccess(guardOptions).pipe(
return this.checkAccess(guardOptions, route, state).pipe(
map(access => {
let returnValue: boolean | UrlTree = access;

Expand All @@ -55,7 +55,7 @@ export class NgxSecurityGuard implements CanLoad, CanActivate, CanActivateChild
}


protected checkAccess(guardOptions: NgxSecurityGuardOptions): Observable<boolean> {
protected checkAccess(guardOptions: NgxSecurityGuardOptions, route?: CurrentRoute, state?: RouteUrl): Observable<boolean> {
let allObs$: Observable<boolean>[] = [];

if (guardOptions.isAuthenticated === true) {
Expand All @@ -65,6 +65,7 @@ export class NgxSecurityGuard implements CanLoad, CanActivate, CanActivateChild
allObs$.push(this.security.isAuthenticated().pipe(map(auth => !auth)));
}


if (guardOptions.hasAllRoles) {
allObs$.push(this.security.hasAllRoles(guardOptions.hasAllRoles));
}
Expand All @@ -91,16 +92,21 @@ export class NgxSecurityGuard implements CanLoad, CanActivate, CanActivateChild
}


let resource = guardOptions.resource;
if (guardOptions.resourceResolver) {
resource = guardOptions.resourceResolver(route, state);
}

if (guardOptions.hasAllPermissions) {
allObs$.push(this.security.hasAllPermissions(guardOptions.hasAllPermissions, guardOptions.resource));
allObs$.push(this.security.hasAllPermissions(guardOptions.hasAllPermissions, resource));
}

if (guardOptions.hasAnyPermissions) {
allObs$.push(this.security.hasAnyPermissions(guardOptions.hasAnyPermissions, guardOptions.resource));
allObs$.push(this.security.hasAnyPermissions(guardOptions.hasAnyPermissions, resource));
}

if (guardOptions.hasNotPermissions) {
allObs$.push(this.security.hasNotPermissions(guardOptions.hasNotPermissions, guardOptions.resource));
allObs$.push(this.security.hasNotPermissions(guardOptions.hasNotPermissions, resource));
}


Expand Down
1 change: 1 addition & 0 deletions projects/ngx-security/src/lib/models/ngx-security.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface NgxSecurityGuardOptions {
hasNotPermissions?: string | string[];

resource?: any;
resourceResolver?: (route?: CurrentRoute, state?: RouteUrl) => any;

redirectTo?: string;
authorizedHandler?: (route?: CurrentRoute, state?: RouteUrl) => void;
Expand Down

0 comments on commit 3dae346

Please sign in to comment.