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