]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/routing/user-right-guard.service.ts
Refactor login redirection/button links
[github/Chocobozzz/PeerTube.git] / client / src / app / core / routing / user-right-guard.service.ts
CommitLineData
7ca86c86 1import { Injectable } from '@angular/core'
98bd5e22 2import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, RouterStateSnapshot } from '@angular/router'
f8b2c1b4 3import { AuthService } from '../auth/auth.service'
98bd5e22 4import { RedirectService } from './redirect.service'
7ca86c86
C
5
6@Injectable()
954605a8 7export class UserRightGuard implements CanActivate, CanActivateChild {
7ca86c86 8
f47bf2e1 9 constructor (
98bd5e22 10 private redirectService: RedirectService,
f47bf2e1
C
11 private auth: AuthService
12 ) {}
7ca86c86
C
13
14 canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
954605a8
C
15 const user = this.auth.getUser()
16 if (user) {
17 const neededUserRight = route.data.userRight
18
19 if (user.hasRight(neededUserRight)) return true
20 }
f47bf2e1 21
98bd5e22 22 this.redirectService.redirectToLogin()
f47bf2e1 23 return false
7ca86c86
C
24 }
25
26 canActivateChild (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
27 return this.canActivate(route, state)
28 }
29}