diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-27 16:55:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-27 16:55:03 +0200 |
commit | 954605a804da399317ca62afa2fb9244afa11ebf (patch) | |
tree | de6ee69280bfb928bc01c29430e13d5b820e921a /client/src/app/core/routing | |
parent | e02573ad67626210ed279bad321ee139094921a1 (diff) | |
download | PeerTube-954605a804da399317ca62afa2fb9244afa11ebf.tar.gz PeerTube-954605a804da399317ca62afa2fb9244afa11ebf.tar.zst PeerTube-954605a804da399317ca62afa2fb9244afa11ebf.zip |
Support roles with rights and add moderator role
Diffstat (limited to 'client/src/app/core/routing')
-rw-r--r-- | client/src/app/core/routing/index.ts | 2 | ||||
-rw-r--r-- | client/src/app/core/routing/login-guard.service.ts | 30 | ||||
-rw-r--r-- | client/src/app/core/routing/user-right-guard.service.ts | 35 |
3 files changed, 67 insertions, 0 deletions
diff --git a/client/src/app/core/routing/index.ts b/client/src/app/core/routing/index.ts index 17f3ee833..d1b982834 100644 --- a/client/src/app/core/routing/index.ts +++ b/client/src/app/core/routing/index.ts | |||
@@ -1 +1,3 @@ | |||
1 | export * from './login-guard.service' | ||
2 | export * from './user-right-guard.service' | ||
1 | export * from './preload-selected-modules-list' | 3 | export * from './preload-selected-modules-list' |
diff --git a/client/src/app/core/routing/login-guard.service.ts b/client/src/app/core/routing/login-guard.service.ts new file mode 100644 index 000000000..18bc41ca6 --- /dev/null +++ b/client/src/app/core/routing/login-guard.service.ts | |||
@@ -0,0 +1,30 @@ | |||
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/auth.service' | ||
11 | |||
12 | @Injectable() | ||
13 | export class LoginGuard implements CanActivate, CanActivateChild { | ||
14 | |||
15 | constructor ( | ||
16 | private router: Router, | ||
17 | private auth: AuthService | ||
18 | ) {} | ||
19 | |||
20 | canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
21 | if (this.auth.isLoggedIn() === true) return true | ||
22 | |||
23 | this.router.navigate([ '/login' ]) | ||
24 | return false | ||
25 | } | ||
26 | |||
27 | canActivateChild (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
28 | return this.canActivate(route, state) | ||
29 | } | ||
30 | } | ||
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 | } | ||