[ADF-4183] improved auth redirection (#4399)

* base auth guard implementation

* remove code duplication

* unit test fixes
This commit is contained in:
Denys Vuika
2019-03-06 13:14:23 +00:00
committed by Eugenio Romano
parent bf331f1145
commit f6341e31a0
9 changed files with 216 additions and 173 deletions

View File

@@ -17,54 +17,33 @@
import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, Router
ActivatedRouteSnapshot, Router
} from '@angular/router';
import { AuthenticationService } from './authentication.service';
import { AppConfigService, AppConfigValues } from '../app-config/app-config.service';
import { OauthConfigModel } from '../models/oauth-config.model';
import { AppConfigService } from '../app-config/app-config.service';
import { AuthGuardBase } from './auth-guard-base';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthGuardEcm implements CanActivate {
export class AuthGuardEcm extends AuthGuardBase {
constructor(private authService: AuthenticationService,
private router: Router,
private appConfigService: AppConfigService) {
constructor(authenticationService: AuthenticationService,
router: Router,
appConfigService: AppConfigService) {
super(authenticationService, router, appConfigService);
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.checkLogin(state.url);
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.canActivate(route, state);
}
checkLogin(redirectUrl: string): boolean {
let withCredentialsMode = this.appConfigService.get<boolean>('auth.withCredentials', false);
if (this.authService.isEcmLoggedIn() || withCredentialsMode) {
checkLogin(activeRoute: ActivatedRouteSnapshot, redirectUrl: string): Observable<boolean> | Promise<boolean> | boolean {
if (this.authenticationService.isEcmLoggedIn() || this.withCredentials) {
return true;
}
if (!this.authService.isOauth() || this.isOAuthWithoutSilentLogin()) {
this.authService.setRedirect({ provider: 'ECM', url: redirectUrl });
const pathToLogin = this.getRouteDestinationForLogin();
this.router.navigate(['/' + pathToLogin]);
if (!this.authenticationService.isOauth() || this.isOAuthWithoutSilentLogin()) {
this.redirectToUrl('ECM', redirectUrl);
}
return false;
}
isOAuthWithoutSilentLogin() {
let oauth: OauthConfigModel = this.appConfigService.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null);
return this.authService.isOauth() && oauth.silentLogin === false;
}
private getRouteDestinationForLogin(): string {
return this.appConfigService &&
this.appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE) ?
this.appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE) : 'login';
}
}