aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/core/routing/user-right-guard.service.ts
blob: a2ce772dbc6cf9e280834987909dc48a34d8d53b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
                                          
                                                                                                                    
                                                  

             
                                                                      
 



                             

                                                                           





                                                     


                                      





                                                                                
import { Injectable } from '@angular/core'
import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router, RouterStateSnapshot } from '@angular/router'
import { AuthService } from '../auth/auth.service'

@Injectable()
export class UserRightGuard implements CanActivate, CanActivateChild {

  constructor (
    private router: Router,
    private auth: AuthService
  ) {}

  canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    const user = this.auth.getUser()
    if (user) {
      const neededUserRight = route.data.userRight

      if (user.hasRight(neededUserRight)) return true
    }

    this.router.navigate([ '/login' ])
    return false
  }

  canActivateChild (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    return this.canActivate(route, state)
  }
}