-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
151 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
CanActivate, | ||
Router, | ||
ActivatedRouteSnapshot, | ||
RouterStateSnapshot | ||
} from '@angular/router'; | ||
|
||
import { ConfigService } from '@igo2/core'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class AdminGuard implements CanActivate { | ||
constructor( | ||
private authService: AuthService, | ||
private config: ConfigService, | ||
private router: Router | ||
) {} | ||
|
||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
const token = this.authService.decodeToken(); | ||
if (token && token.user && token.user.admin) { | ||
return true; | ||
} | ||
|
||
this.authService.redirectUrl = state.url; | ||
|
||
const authConfig = this.config.getConfig('auth'); | ||
if (authConfig && authConfig.loginRoute) { | ||
this.router.navigateByUrl(authConfig.loginRoute); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
CanActivate, | ||
Router, | ||
ActivatedRouteSnapshot, | ||
RouterStateSnapshot | ||
} from '@angular/router'; | ||
|
||
import { ConfigService } from '@igo2/core'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class LoggedGuard implements CanActivate { | ||
constructor( | ||
private authService: AuthService, | ||
private config: ConfigService, | ||
private router: Router | ||
) {} | ||
|
||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
if (this.authService.logged) { | ||
return true; | ||
} | ||
|
||
this.authService.redirectUrl = state.url; | ||
|
||
const authConfig = this.config.getConfig('auth'); | ||
if (authConfig && authConfig.loginRoute) { | ||
this.router.navigateByUrl(authConfig.loginRoute); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
CanActivate, | ||
Router, | ||
ActivatedRouteSnapshot, | ||
RouterStateSnapshot | ||
} from '@angular/router'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
import { ConfigService } from '@igo2/core'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ProfilsGuard implements CanActivate { | ||
constructor( | ||
private authService: AuthService, | ||
private config: ConfigService, | ||
private router: Router | ||
) {} | ||
|
||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
return this.authService.getProfils().pipe( | ||
map((profils: string[]) => { | ||
const authConfig = this.config.getConfig('auth'); | ||
|
||
if ( | ||
profils && | ||
profils.some(v => authConfig.profilsGuard.indexOf(v) !== -1) | ||
) { | ||
return true; | ||
} | ||
|
||
this.authService.redirectUrl = state.url; | ||
|
||
if (authConfig && authConfig.loginRoute) { | ||
this.router.navigateByUrl(authConfig.loginRoute); | ||
} | ||
|
||
return false; | ||
}) | ||
); | ||
} | ||
} |