From 77f6c51dc25ca88df71bfeed30a00ca0d994ddf4 Mon Sep 17 00:00:00 2001 From: Vito Date: Thu, 25 Jan 2018 12:48:47 +0100 Subject: [PATCH] [ADF-2200] fixed problem with redirection url (#2877) * [ADF-2200] fixed wrong handling of redirectUrl * [ADF-2200] fixed problem with redirection url * [ADF-2200] fixed redirection config path * [ADF-2200] fixed wrong fdescribe test * [ADF-2200] removed authserviceMock and fixed some tests --- demo-shell/src/app.config.json | 2 +- demo-shell/src/app/app.component.ts | 6 +- .../login/components/login.component.spec.ts | 27 +++++- lib/core/login/components/login.component.ts | 2 +- lib/core/models/public-api.ts | 1 + lib/core/models/redirection.model.ts | 33 ++++++++ .../services/auth-guard-bpm.service.spec.ts | 83 ++++++++++++------- lib/core/services/auth-guard-bpm.service.ts | 20 +++-- .../services/auth-guard-ecm.service.spec.ts | 4 +- lib/core/services/auth-guard-ecm.service.ts | 17 +++- lib/core/services/auth-guard.service.spec.ts | 70 +++++++++------- lib/core/services/auth-guard.service.ts | 22 +++-- .../services/authentication.service.spec.ts | 51 ++++++++++-- lib/core/services/authentication.service.ts | 17 +++- 14 files changed, 255 insertions(+), 100 deletions(-) create mode 100644 lib/core/models/redirection.model.ts diff --git a/demo-shell/src/app.config.json b/demo-shell/src/app.config.json index ccb85fd352..7e8727d03f 100644 --- a/demo-shell/src/app.config.json +++ b/demo-shell/src/app.config.json @@ -2,7 +2,7 @@ "ecmHost": "http://{hostname}:{port}", "bpmHost": "http://{hostname}:{port}", "application": { - "name": "Alfresco ADF Appplication" + "name": "Alfresco ADF Application" }, "languages": [ { diff --git a/demo-shell/src/app/app.component.ts b/demo-shell/src/app/app.component.ts index 955a939ccd..1354f1cdf9 100644 --- a/demo-shell/src/app/app.component.ts +++ b/demo-shell/src/app/app.component.ts @@ -16,7 +16,7 @@ */ import { Component, ViewEncapsulation } from '@angular/core'; -import { ActivatedRoute, Router } from '@angular/router'; +import { ActivatedRoute } from '@angular/router'; import { SettingsService, PageTitleService, StorageService, TranslationService } from '@alfresco/adf-core'; @Component({ @@ -26,14 +26,12 @@ import { SettingsService, PageTitleService, StorageService, TranslationService } encapsulation: ViewEncapsulation.None }) export class AppComponent { - searchTerm = ''; constructor(private settingsService: SettingsService, private storage: StorageService, translationService: TranslationService, pageTitleService: PageTitleService, - route: ActivatedRoute, - router: Router) { + route: ActivatedRoute) { this.setProvider(); pageTitleService.setTitle(); } diff --git a/lib/core/login/components/login.component.spec.ts b/lib/core/login/components/login.component.spec.ts index 27345bf47e..59e197a643 100644 --- a/lib/core/login/components/login.component.spec.ts +++ b/lib/core/login/components/login.component.spec.ts @@ -26,8 +26,8 @@ import { AuthenticationService } from '../../services/authentication.service'; import { MaterialModule } from '../../material.module'; import { LoginErrorEvent } from '../models/login-error.event'; import { LoginSuccessEvent } from '../models/login-success.event'; -import { AuthenticationMock } from './../../mock/authentication.service.mock'; import { LoginComponent } from './login.component'; +import { Observable } from 'rxjs/Observable'; describe('LoginComponent', () => { let component: LoginComponent; @@ -64,7 +64,7 @@ describe('LoginComponent', () => { LoginComponent ], providers: [ - {provide: AuthenticationService, useClass: AuthenticationMock} + AuthenticationService ] }).compileComponents(); })); @@ -101,6 +101,7 @@ describe('LoginComponent', () => { } it('should redirect to route on successful login', () => { + spyOn(authService, 'login').and.returnValue(Observable.of({ type: 'type', ticket: 'ticket'})); const redirect = '/home'; component.successRoute = redirect; spyOn(router, 'navigate'); @@ -109,9 +110,10 @@ describe('LoginComponent', () => { }); it('should redirect to previous route state on successful login', () => { + spyOn(authService, 'login').and.returnValue(Observable.of({ type: 'type', ticket: 'ticket'})); const redirect = '/home'; component.successRoute = redirect; - authService.setRedirectUrl('redirect-url'); + authService.setRedirectUrl({ provider: 'ECM', url: 'redirect-url' } ); spyOn(router, 'navigate'); @@ -152,12 +154,14 @@ describe('LoginComponent', () => { }); it('should be changed back to the default after a failed login attempt', () => { + spyOn(authService, 'login').and.returnValue(Observable.throw('Fake server error')); loginWithCredentials('fake-wrong-username', 'fake-wrong-password'); expect(getLoginButtonText()).toEqual('LOGIN.BUTTON.LOGIN'); }); it('should be changed to the "welcome key" after a successful login attempt', () => { + spyOn(authService, 'login').and.returnValue(Observable.of({ type: 'type', ticket: 'ticket'})); loginWithCredentials('fake-username', 'fake-password'); expect(getLoginButtonText()).toEqual('LOGIN.BUTTON.WELCOME'); @@ -436,6 +440,12 @@ describe('LoginComponent', () => { })); it('should return CORS error when server CORS error occurs', async(() => { + spyOn(authService, 'login').and.returnValue(Observable.throw({ + error: { + crossDomain: true, + message: 'ERROR: the network is offline, Origin is not allowed by Access-Control-Allow-Origin' + } + })); component.providers = 'ECM'; component.error.subscribe(() => { @@ -450,6 +460,8 @@ describe('LoginComponent', () => { })); it('should return CSRF error when server CSRF error occurs', async(() => { + spyOn(authService, 'login') + .and.returnValue(Observable.throw({ message: 'ERROR: Invalid CSRF-token', status: 403 })); component.providers = 'ECM'; component.error.subscribe(() => { @@ -464,6 +476,14 @@ describe('LoginComponent', () => { })); it('should return ECOM read-oly error when error occurs', async(() => { + spyOn(authService, 'login') + .and.returnValue( + Observable.throw( + { + message: 'ERROR: 00170728 Access Denied. The system is currently in read-only mode', + status: 403 + } + )); component.providers = 'ECM'; component.error.subscribe(() => { @@ -493,6 +513,7 @@ describe('LoginComponent', () => { })); it('should emit error event after the login has failed', async(() => { + spyOn(authService, 'login').and.returnValue(Observable.throw('Fake server error')); component.providers = 'ECM'; component.error.subscribe((error) => { diff --git a/lib/core/login/components/login.component.ts b/lib/core/login/components/login.component.ts index 628c6d7054..08743f4dd6 100644 --- a/lib/core/login/components/login.component.ts +++ b/lib/core/login/components/login.component.ts @@ -213,7 +213,7 @@ export class LoginComponent implements OnInit { this.authService.login(values.username, values.password, this.rememberMe) .subscribe( (token: any) => { - const redirectUrl = this.authService.getRedirectUrl(); + const redirectUrl = this.authService.getRedirectUrl(this.providers); this.actualLoginStep = LoginSteps.Welcome; this.userPreferences.setStoragePrefix(values.username); diff --git a/lib/core/models/public-api.ts b/lib/core/models/public-api.ts index 26b5c93443..e2e64f651e 100644 --- a/lib/core/models/public-api.ts +++ b/lib/core/models/public-api.ts @@ -21,3 +21,4 @@ export * from './product-version.model'; export * from './user-process.model'; export * from './comment-process.model'; export * from './ecm-company.model'; +export * from './redirection.model'; diff --git a/lib/core/models/redirection.model.ts b/lib/core/models/redirection.model.ts new file mode 100644 index 0000000000..c44359d843 --- /dev/null +++ b/lib/core/models/redirection.model.ts @@ -0,0 +1,33 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This object represent the user redirection guard.* + */ + +export class RedirectionModel { + provider: string; + url?: string; + + constructor(obj?: any) { + if (obj) { + this.provider = obj.provider; + this.url = obj.url || null; + } + } + +} diff --git a/lib/core/services/auth-guard-bpm.service.spec.ts b/lib/core/services/auth-guard-bpm.service.spec.ts index 0ec1f504e0..86e8c20b70 100644 --- a/lib/core/services/auth-guard-bpm.service.spec.ts +++ b/lib/core/services/auth-guard-bpm.service.spec.ts @@ -15,14 +15,14 @@ * limitations under the License. */ -import { async, inject, TestBed } from '@angular/core/testing'; -import { Router } from '@angular/router'; +import { async, TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { TranslateLoader, TranslateModule } from '@ngx-translate/core'; import { CookieServiceMock } from './../mock/cookie.service.mock'; import { AlfrescoApiService } from './alfresco-api.service'; import { SettingsService } from './settings.service'; import { AppConfigModule } from '../app-config/app-config.module'; +import { AppConfigService } from '../app-config/app-config.service'; import { AuthGuardBpm } from './auth-guard-bpm.service'; import { AuthenticationService } from './authentication.service'; import { CookieService } from './cookie.service'; @@ -30,9 +30,15 @@ import { LogService } from './log.service'; import { StorageService } from './storage.service'; import { TranslateLoaderService } from './translate-loader.service'; import { UserPreferencesService } from './user-preferences.service'; +import { RouterStateSnapshot, Router } from '@angular/router'; describe('AuthGuardService BPM', () => { + let authGuard: AuthGuardBpm; + let authService: AuthenticationService; + let routerService: Router; + let appConfigService: AppConfigService; + beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ @@ -58,43 +64,58 @@ describe('AuthGuardService BPM', () => { }).compileComponents(); })); - it('if the alfresco js api is logged in should canActivate be true', - async(inject([AuthGuardBpm, Router, SettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => { - spyOn(router, 'navigate'); + beforeEach(() => { + authService = TestBed.get(AuthenticationService); + authGuard = TestBed.get(AuthGuardBpm); + routerService = TestBed.get(Router); + appConfigService = TestBed.get(AppConfigService); + }); - authService.isBpmLoggedIn = () => { - return true; - }; + it('if the alfresco js api is logged in should canActivate be true', async(() => { + spyOn(authService, 'isBpmLoggedIn').and.returnValue(true); + const router: RouterStateSnapshot = {url : ''}; - expect(auth.canActivate(null, { url: '' })).toBeTruthy(); - expect(router.navigate).not.toHaveBeenCalled(); - })) - ); + expect(authGuard.canActivate(null, router)).toBeTruthy(); + })); - it('if the alfresco js api is NOT logged in should canActivate be false', - async(inject([AuthGuardBpm, Router, SettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => { + it('if the alfresco js api is NOT logged in should canActivate be false', async(() => { + spyOn(authService, 'isBpmLoggedIn').and.returnValue(false); + spyOn(routerService, 'navigate').and.stub(); + const router: RouterStateSnapshot = { url: '' }; - spyOn(router, 'navigate'); + expect(authGuard.canActivate(null, router)).toBeFalsy(); + })); - authService.isBpmLoggedIn = () => { - return false; - }; + it('if the alfresco js api is NOT logged in should trigger a redirect event', async(() => { + spyOn(routerService, 'navigate'); + spyOn(authService, 'isBpmLoggedIn').and.returnValue(false); + const router: RouterStateSnapshot = {url : ''}; - expect(auth.canActivate(null, { url: '' })).toBeFalsy(); - expect(router.navigate).toHaveBeenCalled(); - })) - ); + expect(authGuard.canActivate(null, router)).toBeFalsy(); + expect(routerService.navigate).toHaveBeenCalledWith(['/login']); + })); - it('should set redirect url', - async(inject([AuthGuardBpm, Router, AuthenticationService], (auth, router, authService) => { - const state = { url: 'some-url' }; + it('should set redirect url', async(() => { + spyOn(authService, 'setRedirectUrl').and.callThrough(); + spyOn(routerService, 'navigate').and.stub(); + const router: RouterStateSnapshot = { url: 'some-url' }; - spyOn(router, 'navigate'); - spyOn(authService, 'setRedirectUrl'); + authGuard.canActivate(null, router); - auth.canActivate(null , state); + expect(authService.setRedirectUrl).toHaveBeenCalledWith({provider: 'BPM', url: 'some-url' } ); + expect(authService.getRedirectUrl('BPM')).toBe('some-url'); + })); + + it('should get redirect url from config if there is one configured', async(() => { + appConfigService.config.loginRoute = 'fakeLoginRoute'; + spyOn(authService, 'setRedirectUrl').and.callThrough(); + spyOn(routerService, 'navigate').and.stub(); + const router: RouterStateSnapshot = { url: 'some-url' }; + + authGuard.canActivate(null, router); + + expect(authService.setRedirectUrl).toHaveBeenCalledWith({provider: 'BPM', url: 'some-url' } ); + expect(routerService.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']); + })); - expect(authService.setRedirectUrl).toHaveBeenCalledWith(state.url); - })) - ); }); diff --git a/lib/core/services/auth-guard-bpm.service.ts b/lib/core/services/auth-guard-bpm.service.ts index 9f302bf93d..7aa826e4e0 100644 --- a/lib/core/services/auth-guard-bpm.service.ts +++ b/lib/core/services/auth-guard-bpm.service.ts @@ -18,15 +18,16 @@ import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, - Router, - RouterStateSnapshot + RouterStateSnapshot, Router } from '@angular/router'; - +import { AppConfigService } from '../app-config/app-config.service'; import { AuthenticationService } from './authentication.service'; @Injectable() export class AuthGuardBpm implements CanActivate, CanActivateChild { - constructor(private authService: AuthenticationService, private router: Router) {} + constructor(private authService: AuthenticationService, + private router: Router, + private appConfig: AppConfigService) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { return this.checkLogin(state.url); @@ -41,9 +42,16 @@ export class AuthGuardBpm implements CanActivate, CanActivateChild { return true; } - this.authService.setRedirectUrl(redirectUrl); + this.authService.setRedirectUrl({ provider: 'BPM', url: redirectUrl }); + const pathToLogin = this.getRouteDestinationForLogin(); + this.router.navigate(['/' + pathToLogin]); - this.router.navigate(['/login']); return false; } + + private getRouteDestinationForLogin(): string { + return this.appConfig && + this.appConfig.get('loginRoute') ? + this.appConfig.get('loginRoute') : 'login'; + } } diff --git a/lib/core/services/auth-guard-ecm.service.spec.ts b/lib/core/services/auth-guard-ecm.service.spec.ts index 42a78586d9..7f02eeeaec 100644 --- a/lib/core/services/auth-guard-ecm.service.spec.ts +++ b/lib/core/services/auth-guard-ecm.service.spec.ts @@ -125,7 +125,7 @@ class TestConfig { } } -describe('CanActivateLoggedIn', () => { +describe('AuthGuardService ECM', () => { describe('user is not logged in', () => { beforeEach(async(() => { this.test = new TestConfig({ @@ -211,7 +211,7 @@ describe('CanActivateLoggedIn', () => { })); it('should set redirect url', () => { - expect(this.auth.setRedirectUrl).toHaveBeenCalledWith('some-url'); + expect(this.auth.setRedirectUrl).toHaveBeenCalledWith({ provider: 'ECM', url: 'some-url' }); }); }); diff --git a/lib/core/services/auth-guard-ecm.service.ts b/lib/core/services/auth-guard-ecm.service.ts index 3858d305b8..28ef5119ef 100644 --- a/lib/core/services/auth-guard-ecm.service.ts +++ b/lib/core/services/auth-guard-ecm.service.ts @@ -16,16 +16,18 @@ */ import { Injectable } from '@angular/core'; -import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; +import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, Router } from '@angular/router'; import { AlfrescoApiService } from './alfresco-api.service'; import { AuthenticationService } from './authentication.service'; +import { AppConfigService } from '../app-config/app-config.service'; @Injectable() export class AuthGuardEcm implements CanActivate { constructor( private authService: AuthenticationService, private apiService: AlfrescoApiService, - private router: Router) { + private router: Router, + private appConfig: AppConfigService) { } private get authApi() { @@ -51,11 +53,18 @@ export class AuthGuardEcm implements CanActivate { return this.isLoggedIn().then(isLoggedIn => { if (!isLoggedIn) { - this.authService.setRedirectUrl(state.url); - this.router.navigate([ '/login' ]); + this.authService.setRedirectUrl({ provider: 'ECM', url: state.url }); + const pathToLogin = this.getRouteDestinationForLogin(); + this.router.navigate(['/' + pathToLogin]); } return isLoggedIn; }); } + + private getRouteDestinationForLogin(): string { + return this.appConfig && + this.appConfig.get('loginRoute') ? + this.appConfig.get('loginRoute') : 'login'; + } } diff --git a/lib/core/services/auth-guard.service.spec.ts b/lib/core/services/auth-guard.service.spec.ts index a5f21bc583..35771a43a2 100644 --- a/lib/core/services/auth-guard.service.spec.ts +++ b/lib/core/services/auth-guard.service.spec.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { async, inject, TestBed } from '@angular/core/testing'; +import { async, TestBed } from '@angular/core/testing'; import { Router } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; import { TranslateLoader, TranslateModule } from '@ngx-translate/core'; @@ -24,6 +24,7 @@ import { CookieServiceMock } from './../mock/cookie.service.mock'; import { AlfrescoApiService } from './alfresco-api.service'; import { SettingsService } from './settings.service'; import { AppConfigModule } from '../app-config/app-config.module'; +import { AppConfigService } from '../app-config/app-config.service'; import { AuthGuard } from './auth-guard.service'; import { AuthenticationService } from './authentication.service'; import { CookieService } from './cookie.service'; @@ -34,6 +35,10 @@ import { UserPreferencesService } from './user-preferences.service'; describe('AuthGuardService', () => { let state; + let authService: AuthenticationService; + let router: Router; + let service: AuthGuard; + let appConfigService: AppConfigService; beforeEach(async(() => { TestBed.configureTestingModule({ @@ -62,45 +67,50 @@ describe('AuthGuardService', () => { beforeEach(() => { state = { url: '' }; + authService = TestBed.get(AuthenticationService); + router = TestBed.get(Router); + service = TestBed.get(AuthGuard); + appConfigService = TestBed.get(AppConfigService); }); - it('if the alfresco js api is logged in should canActivate be true', - async(inject([AuthGuard, Router, SettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => { - spyOn(router, 'navigate'); + it('if the alfresco js api is logged in should canActivate be true', async(() => { + spyOn(router, 'navigate'); + spyOn(authService, 'isLoggedIn').and.returnValue(true); - authService.isLoggedIn = () => { - return true; - }; + expect(service.canActivate(null, state)).toBeTruthy(); + expect(router.navigate).not.toHaveBeenCalled(); + })); - expect(auth.canActivate(null, state)).toBeTruthy(); - expect(router.navigate).not.toHaveBeenCalled(); - })) - ); + it('if the alfresco js api is NOT logged in should canActivate be false', async(() => { + spyOn(router, 'navigate'); + spyOn(authService, 'isLoggedIn').and.returnValue(false); - it('if the alfresco js api is NOT logged in should canActivate be false', - async(inject([AuthGuard, Router, SettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => { + expect(service.canActivate(null, state)).toBeFalsy(); + expect(router.navigate).toHaveBeenCalled(); + })); - spyOn(router, 'navigate'); + it('should set redirect url', async(() => { + state.url = 'some-url'; - authService.isLoggedIn = () => { - return false; - }; + spyOn(router, 'navigate'); + spyOn(authService, 'setRedirectUrl'); - expect(auth.canActivate(null, state)).toBeFalsy(); - expect(router.navigate).toHaveBeenCalled(); - })) - ); + service.canActivate(null, state); - it('should set redirect url', - async(inject([AuthGuard, Router, AuthenticationService], (auth, router, authService) => { - state.url = 'some-url'; + expect(authService.setRedirectUrl).toHaveBeenCalledWith({ provider: 'ALL', url: 'some-url' }); + expect(router.navigate).toHaveBeenCalledWith(['/login']); + })); - spyOn(router, 'navigate'); - spyOn(authService, 'setRedirectUrl'); + it('should get redirect url from config if there is one configured', async(() => { + state.url = 'some-url'; + appConfigService.config.loginRoute = 'fakeLoginRoute'; - auth.canActivate(null , state); + spyOn(router, 'navigate'); + spyOn(authService, 'setRedirectUrl'); - expect(authService.setRedirectUrl).toHaveBeenCalledWith(state.url); - })) - ); + service.canActivate(null, state); + + expect(authService.setRedirectUrl).toHaveBeenCalledWith({ provider: 'ALL', url: 'some-url' }); + expect(router.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']); + })); }); diff --git a/lib/core/services/auth-guard.service.ts b/lib/core/services/auth-guard.service.ts index d10cd781d4..ff6224e5f5 100644 --- a/lib/core/services/auth-guard.service.ts +++ b/lib/core/services/auth-guard.service.ts @@ -17,16 +17,17 @@ import { Injectable } from '@angular/core'; import { - ActivatedRouteSnapshot, CanActivate, CanActivateChild, - Router, - RouterStateSnapshot + ActivatedRouteSnapshot, CanActivate, + CanActivateChild, RouterStateSnapshot, Router } from '@angular/router'; - +import { AppConfigService } from '../app-config/app-config.service'; import { AuthenticationService } from './authentication.service'; @Injectable() export class AuthGuard implements CanActivate, CanActivateChild { - constructor(private authService: AuthenticationService, private router: Router) {} + constructor(private authService: AuthenticationService, + private router: Router, + private appConfig: AppConfigService) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { const redirectUrl = state.url; @@ -43,9 +44,16 @@ export class AuthGuard implements CanActivate, CanActivateChild { return true; } - this.authService.setRedirectUrl(redirectUrl); + this.authService.setRedirectUrl({ provider: 'ALL', url: redirectUrl } ); + const pathToLogin = this.getRouteDestinationForLogin(); + this.router.navigate(['/' + pathToLogin]); - this.router.navigate(['/login']); return false; } + + private getRouteDestinationForLogin(): string { + return this.appConfig && + this.appConfig.get('loginRoute') ? + this.appConfig.get('loginRoute') : 'login'; + } } diff --git a/lib/core/services/authentication.service.spec.ts b/lib/core/services/authentication.service.spec.ts index 1f0cf9e6a1..8f9e49efec 100644 --- a/lib/core/services/authentication.service.spec.ts +++ b/lib/core/services/authentication.service.spec.ts @@ -258,6 +258,18 @@ describe('AuthenticationService', () => { expect(authService.isLoggedIn()).toBe(false); expect(authService.isEcmLoggedIn()).toBe(false); }); + + it('[ECM] should set/get redirectUrl when provider is ECM', () => { + authService.setRedirectUrl({provider: 'ECM', url: 'some-url' } ); + + expect(authService.getRedirectUrl(preferences.authType)).toBe('some-url'); + }); + + it('[ECM] should set/get redirectUrl when provider is BPM', () => { + authService.setRedirectUrl({provider: 'BPM', url: 'some-url' } ); + + expect(authService.getRedirectUrl(preferences.authType)).toBeNull(); + }); }); describe('when the setting is BPM', () => { @@ -367,6 +379,18 @@ describe('AuthenticationService', () => { 'status': 403 }); }); + + it('[BPM] should set/get redirectUrl when provider is BPM', () => { + authService.setRedirectUrl({provider: 'BPM', url: 'some-url' } ); + + expect(authService.getRedirectUrl(preferences.authType)).toBe('some-url'); + }); + + it('[BPM] should set/get redirectUrl when provider is ECM', () => { + authService.setRedirectUrl({provider: 'ECM', url: 'some-url' } ); + + expect(authService.getRedirectUrl(preferences.authType)).toBeNull(); + }); }); describe('when the setting is both ECM and BPM ', () => { @@ -396,7 +420,7 @@ describe('AuthenticationService', () => { }); }); - xit('[ALL] should return login fail if only ECM call fail', (done) => { + it('[ALL] should return login fail if only ECM call fail', (done) => { authService.login('fake-username', 'fake-password').subscribe( (res) => { }, @@ -417,7 +441,7 @@ describe('AuthenticationService', () => { }); }); - xit('[ALL] should return login fail if only BPM call fail', (done) => { + it('[ALL] should return login fail if only BPM call fail', (done) => { authService.login('fake-username', 'fake-password').subscribe( (res) => { }, @@ -461,11 +485,24 @@ describe('AuthenticationService', () => { 'status': 403 }); }); + + it('[ALL] should set/get redirectUrl when provider is ALL', () => { + authService.setRedirectUrl({provider: 'ALL', url: 'some-url' } ); + + expect(authService.getRedirectUrl(preferences.authType)).toBe('some-url'); + }); + + it('[ALL] should set/get redirectUrl when provider is BPM', () => { + authService.setRedirectUrl({provider: 'BPM', url: 'some-url' } ); + + expect(authService.getRedirectUrl(preferences.authType)).toBe('some-url'); + }); + + it('[ALL] should set/get redirectUrl when provider is ECM', () => { + authService.setRedirectUrl({provider: 'ECM', url: 'some-url' } ); + + expect(authService.getRedirectUrl(preferences.authType)).toBe('some-url'); + }); }); - it('should set/get redirectUrl', () => { - authService.setRedirectUrl('some-url'); - - expect(authService.getRedirectUrl()).toBe('some-url'); - }); }); diff --git a/lib/core/services/authentication.service.ts b/lib/core/services/authentication.service.ts index 4ca675ee29..30b7eee641 100644 --- a/lib/core/services/authentication.service.ts +++ b/lib/core/services/authentication.service.ts @@ -26,13 +26,14 @@ import { UserPreferencesService } from './user-preferences.service'; import 'rxjs/add/observable/fromPromise'; import 'rxjs/add/operator/catch'; import 'rxjs/add/observable/throw'; +import { RedirectionModel } from '../models/redirection.model'; const REMEMBER_ME_COOKIE_KEY = 'ALFRESCO_REMEMBER_ME'; const REMEMBER_ME_UNTIL = 1000 * 60 * 60 * 24 * 30 ; @Injectable() export class AuthenticationService { - private redirectUrl: string = ''; + private redirectUrl: RedirectionModel = null; onLogin: Subject = new Subject(); onLogout: Subject = new Subject(); @@ -233,12 +234,20 @@ export class AuthenticationService { return this.alfrescoApi.getInstance().bpmAuth.username; } - setRedirectUrl(url: string) { + setRedirectUrl(url: RedirectionModel) { this.redirectUrl = url; } - getRedirectUrl(): string { - return this.redirectUrl; + getRedirectUrl(provider: string): string { + return this.hasValidRedirection(provider) ? this.redirectUrl.url : null; + } + + private hasValidRedirection(provider: string): boolean { + return this.redirectUrl && this.redirectUrl.provider === provider || this.hasSelectedProviderAll(provider); + } + + private hasSelectedProviderAll(provider: string): boolean { + return this.redirectUrl && this.redirectUrl.provider === 'ALL' || provider === 'ALL'; } /**