Skip to content

Commit

Permalink
feat: expose NgxSecurityGuard.canAccess + add `NgxSecurityGuard.han…
Browse files Browse the repository at this point in the history
…dle`
  • Loading branch information
mselerin committed Mar 16, 2023
1 parent 0b3c54a commit 92d506d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
38 changes: 22 additions & 16 deletions projects/ngx-security/src/lib/guards/ngx-security.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,56 @@ import {
CanLoad,
Route,
Router,
RouterStateSnapshot
RouterStateSnapshot, UrlTree
} from '@angular/router';
import {NgxSecurityService} from '../services/ngx-security.service';
import {NgxSecurityGuardOptions} from '../models/ngx-security.model';
import {merge, Observable, of} from 'rxjs';
import {every, map, take, tap} from 'rxjs/operators';
import {CurrentRoute, NgxSecurityGuardOptions, RouteUrl} from '../models/ngx-security.model';
import {merge, Observable} from 'rxjs';
import {every, map, take} from 'rxjs/operators';

@Injectable({ providedIn: 'root' })
export class NgxSecurityGuard implements CanLoad, CanActivate, CanActivateChild
{
export class NgxSecurityGuard implements CanLoad, CanActivate, CanActivateChild {
constructor(
protected readonly security: NgxSecurityService,
protected readonly router: Router
) {}

canLoad(route: Route): Observable<boolean | UrlTree> { return this.canAccess(route); }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> { return this.canAccess(route, state); }
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> { return this.canAccess(route, state); }

canLoad(route: Route): Observable<boolean> { return this.canAccess(route); }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { return this.canAccess(route, state); }
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { return this.canAccess(route, state); }


protected canAccess(route: Route | ActivatedRouteSnapshot, state?: RouterStateSnapshot): Observable<boolean> {
canAccess(route: CurrentRoute, state?: RouteUrl): Observable<boolean | UrlTree> {
const guardOptions = !!route && route.data ? route.data['security'] as NgxSecurityGuardOptions : {};
return this.handle(guardOptions, route, state);
}

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

if (!access) {
if (guardOptions.unauthorizedHandler) {
guardOptions.unauthorizedHandler(route, state);
}

if (guardOptions.redirectTo)
this.router.navigateByUrl(guardOptions.redirectTo);
if (guardOptions.redirectTo) {
returnValue = this.router.parseUrl(guardOptions.redirectTo);
}
}
else {
if (guardOptions.authorizedHandler) {
guardOptions.authorizedHandler(route, state);
}
}

return returnValue;
})
);
}


private checkAccess(guardOptions: NgxSecurityGuardOptions): Observable<boolean> {
protected checkAccess(guardOptions: NgxSecurityGuardOptions): Observable<boolean> {
let allObs$: Observable<boolean>[] = [];

if (guardOptions.isAuthenticated === true) {
Expand Down
9 changes: 6 additions & 3 deletions projects/ngx-security/src/lib/models/ngx-security.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ActivatedRouteSnapshot, Route, RouterStateSnapshot } from '@angular/router';
import { ActivatedRouteSnapshot, Route, RouterStateSnapshot, UrlSegment } from '@angular/router';
import { Observable } from 'rxjs';

export type CurrentRoute = Route | ActivatedRouteSnapshot;
export type RouteUrl = RouterStateSnapshot | UrlSegment[];

export interface NgxSecurityState {
authenticationChecker: () => Observable<boolean>;
rolesChecker: (name: string) => Observable<boolean>;
Expand All @@ -25,6 +28,6 @@ export interface NgxSecurityGuardOptions {
hasNotPermissions?: string | string[];

redirectTo?: string;
authorizedHandler?: (route: Route | ActivatedRouteSnapshot, state?: RouterStateSnapshot) => void;
unauthorizedHandler?: (route: Route | ActivatedRouteSnapshot, state?: RouterStateSnapshot) => void;
authorizedHandler?: (route?: CurrentRoute, state?: RouteUrl) => void;
unauthorizedHandler?: (route?: CurrentRoute, state?: RouteUrl) => void;
}

0 comments on commit 92d506d

Please sign in to comment.