diff options
Diffstat (limited to 'client/src/app/core/routing/user-right-guard.service.ts')
-rw-r--r-- | client/src/app/core/routing/user-right-guard.service.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/client/src/app/core/routing/user-right-guard.service.ts b/client/src/app/core/routing/user-right-guard.service.ts new file mode 100644 index 000000000..65d029977 --- /dev/null +++ b/client/src/app/core/routing/user-right-guard.service.ts | |||
@@ -0,0 +1,35 @@ | |||
1 | import { Injectable } from '@angular/core' | ||
2 | import { | ||
3 | ActivatedRouteSnapshot, | ||
4 | CanActivateChild, | ||
5 | RouterStateSnapshot, | ||
6 | CanActivate, | ||
7 | Router | ||
8 | } from '@angular/router' | ||
9 | |||
10 | import { AuthService } from '../auth' | ||
11 | |||
12 | @Injectable() | ||
13 | export class UserRightGuard implements CanActivate, CanActivateChild { | ||
14 | |||
15 | constructor ( | ||
16 | private router: Router, | ||
17 | private auth: AuthService | ||
18 | ) {} | ||
19 | |||
20 | canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
21 | const user = this.auth.getUser() | ||
22 | if (user) { | ||
23 | const neededUserRight = route.data.userRight | ||
24 | |||
25 | if (user.hasRight(neededUserRight)) return true | ||
26 | } | ||
27 | |||
28 | this.router.navigate([ '/login' ]) | ||
29 | return false | ||
30 | } | ||
31 | |||
32 | canActivateChild (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
33 | return this.canActivate(route, state) | ||
34 | } | ||
35 | } | ||