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