diff options
author | 杨宇千 <crupest@outlook.com> | 2019-03-14 16:51:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-14 16:51:46 +0800 |
commit | c65db42c9ce3532509bbd0b4abfc500692cfe4d3 (patch) | |
tree | 9e6a91af019e14fa3fb40e03bf84507a98a33d4c /Timeline/ClientApp/src/app/user/auth.guard.ts | |
parent | 4535d1fd30eb02b3fe60718235a0725e3b30049e (diff) | |
parent | 7b929db4fcfa34acc82885b75a573e6e4bd40465 (diff) | |
download | timeline-c65db42c9ce3532509bbd0b4abfc500692cfe4d3.tar.gz timeline-c65db42c9ce3532509bbd0b4abfc500692cfe4d3.tar.bz2 timeline-c65db42c9ce3532509bbd0b4abfc500692cfe4d3.zip |
Merge pull request #13 from crupest/auth-guard
Add auth guard.
Diffstat (limited to 'Timeline/ClientApp/src/app/user/auth.guard.ts')
-rw-r--r-- | Timeline/ClientApp/src/app/user/auth.guard.ts | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/Timeline/ClientApp/src/app/user/auth.guard.ts b/Timeline/ClientApp/src/app/user/auth.guard.ts new file mode 100644 index 00000000..561a0c53 --- /dev/null +++ b/Timeline/ClientApp/src/app/user/auth.guard.ts @@ -0,0 +1,79 @@ +import { Injectable } from '@angular/core'; +import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router'; +import { Observable } from 'rxjs'; + +import { InternalUserService } from './internal-user-service/internal-user.service'; + +export type AuthStrategy = 'all' | 'requirelogin' | 'requirenologin' | string[]; + +export abstract class AuthGuard implements CanActivate { + + constructor(protected internalUserService: InternalUserService) { } + + onAuthFailed() { } + + abstract get authStrategy(): AuthStrategy; + + canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): + Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { + + const { authStrategy } = this; + + if (authStrategy === 'all') { + return true; + } + + const { currentUserInfo } = this.internalUserService; + + if (currentUserInfo === null) { + if (authStrategy === 'requirenologin') { + return true; + } + } else { + if (authStrategy === 'requirelogin') { + return true; + } else if (authStrategy instanceof Array) { + const { roles } = currentUserInfo; + if (authStrategy.every(value => roles.includes(value))) { + return true; + } + } + } + + // reach here means auth fails + this.onAuthFailed(); + return false; + } +} + +@Injectable({ + providedIn: 'root' +}) +export class RequireLoginGuard extends AuthGuard { + readonly authStrategy: AuthStrategy = 'requirelogin'; + + // never remove this constructor or you will get an injection error. + constructor(internalUserService: InternalUserService) { + super(internalUserService); + } + + onAuthFailed() { + this.internalUserService.userRouteNavigate(['login']); + } +} + +@Injectable({ + providedIn: 'root' +}) +export class RequireNoLoginGuard extends AuthGuard { + readonly authStrategy: AuthStrategy = 'requirenologin'; + + // never remove this constructor or you will get an injection error. + constructor(internalUserService: InternalUserService) { + super(internalUserService); + } + + onAuthFailed() { + this.internalUserService.userRouteNavigate(['success']); + } +} |