]>
Commit | Line | Data |
---|---|---|
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 | } |