feat(authorization): Implemented guards to avoid unauthorized access. (TV-515)
Squashed commit of the following: commit 86aa3af3f54be4ef5bfb99baece6654a7fba204f Merge: f3258e81e45fb5Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Thu Sep 9 05:42:46 2021 +0200 Merge branch 'develop' into feature/TV-515-authorization-flow commit f3258e8c6e3d51f21ec619e09c82b2d0f581bde9 Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 8 16:43:44 2021 +0200 Fixed tests commit 91bfea1baa297f34769a33972fd61481dfa31197 Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 8 15:55:13 2021 +0200 Removed unused pages commit d4a92fbde9d6255d8406abc23fe1479658035787 Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 8 15:51:25 2021 +0200 Updated some styling commit dc75656ff96ff0358a2dd0a8b090b4b4938b8323 Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 8 15:35:04 2021 +0200 Refactured guards by separating organizations into its own guard commit 24f3a0a2d821930bd682b854f98e1c9816ece08c Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 8 15:33:53 2021 +0200 Readded search on employees commit f1890b104c48d6dd6e263b730dbdafbc2a6fbf0f Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 8 14:59:24 2021 +0200 Added RoleGuard to pages needing a guard commit ef4b37e3dcc8fe26eef1bb813cfb35727ba691be Merge: 07bca2ab06436aAuthor: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 8 14:06:34 2021 +0200 Merge branch 'develop' into feature/TV-515-authorization-flow commit 07bca2a84d0ec970188c284ba4b950312cec57cb Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 8 13:26:50 2021 +0200 Added check for navigation
This commit is contained in:
@@ -2,7 +2,6 @@ import { Injectable } from '@angular/core';
|
||||
import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
|
||||
import { environment } from '@msfa-environment';
|
||||
import { AuthenticationService } from '@msfa-services/api/authentication.service';
|
||||
import { UserService } from '@msfa-services/api/user.service';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
|
||||
@@ -10,24 +9,13 @@ import { map, switchMap } from 'rxjs/operators';
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(
|
||||
private authenticationService: AuthenticationService,
|
||||
private router: Router,
|
||||
private userService: UserService
|
||||
) {}
|
||||
constructor(private authenticationService: AuthenticationService, private router: Router) {}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
|
||||
return this.authenticationService.isLoggedIn$.pipe(
|
||||
switchMap(loggedIn => {
|
||||
if (loggedIn) {
|
||||
return this.userService.selectedOrganization$.pipe(
|
||||
map(organization => {
|
||||
if (!organization) {
|
||||
void this.router.navigateByUrl(`/organization-picker`);
|
||||
}
|
||||
return true;
|
||||
})
|
||||
);
|
||||
return of(true);
|
||||
} else if (route.queryParams.code) {
|
||||
return this.authenticationService.login$(route.queryParams.code).pipe(map(result => !!result));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate, Router } from '@angular/router';
|
||||
import { UserService } from '@msfa-services/api/user.service';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class OrganizationGuard implements CanActivate {
|
||||
constructor(private router: Router, private userService: UserService) {}
|
||||
|
||||
canActivate(): Observable<boolean> {
|
||||
return this.userService.selectedOrganization$.pipe(
|
||||
map(organization => {
|
||||
if (!organization) {
|
||||
void this.router.navigateByUrl(`/organization-picker`);
|
||||
}
|
||||
return true;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
|
||||
import { RoleEnum } from '@msfa-enums/role.enum';
|
||||
import { UserService } from '@msfa-services/api/user.service';
|
||||
import { Observable } from 'rxjs';
|
||||
import { filter, map } from 'rxjs/operators';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class RoleGuard implements CanActivate {
|
||||
constructor(private router: Router, private userService: UserService) {}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
|
||||
const expectedRole: RoleEnum = route.data.expectedRole as RoleEnum;
|
||||
|
||||
return this.userService.user$.pipe(
|
||||
filter(user => !!user),
|
||||
map(({ roles }) => {
|
||||
const userHasRole = roles.some(role => role.type === expectedRole);
|
||||
|
||||
if (userHasRole) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void this.router.navigateByUrl('/obehorig');
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user