]>
Commit | Line | Data |
---|---|---|
7ca86c86 | 1 | import { Injectable } from '@angular/core' |
67ed6552 | 2 | import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router, RouterStateSnapshot } from '@angular/router' |
f8b2c1b4 | 3 | import { AuthService } from '../auth/auth.service' |
7ca86c86 C |
4 | |
5 | @Injectable() | |
954605a8 | 6 | export class UserRightGuard implements CanActivate, CanActivateChild { |
7ca86c86 | 7 | |
f47bf2e1 C |
8 | constructor ( |
9 | private router: Router, | |
10 | private auth: AuthService | |
11 | ) {} | |
7ca86c86 C |
12 | |
13 | canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | |
954605a8 C |
14 | const user = this.auth.getUser() |
15 | if (user) { | |
16 | const neededUserRight = route.data.userRight | |
17 | ||
18 | if (user.hasRight(neededUserRight)) return true | |
19 | } | |
f47bf2e1 C |
20 | |
21 | this.router.navigate([ '/login' ]) | |
22 | return false | |
7ca86c86 C |
23 | } |
24 | ||
25 | canActivateChild (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | |
26 | return this.canActivate(route, state) | |
27 | } | |
28 | } |