diff --git a/ng2-components/ng2-alfresco-core/src/services/auth-guard-bpm.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/auth-guard-bpm.service.spec.ts index 1b4cd7b25e..da11bd03a1 100644 --- a/ng2-components/ng2-alfresco-core/src/services/auth-guard-bpm.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/auth-guard-bpm.service.spec.ts @@ -66,7 +66,7 @@ describe('AuthGuardService BPM', () => { return true; }; - expect(auth.canActivate()).toBeTruthy(); + expect(auth.canActivate(null, { url: '' })).toBeTruthy(); expect(router.navigate).not.toHaveBeenCalled(); })) ); @@ -80,9 +80,21 @@ describe('AuthGuardService BPM', () => { return false; }; - expect(auth.canActivate()).toBeFalsy(); + expect(auth.canActivate(null, { url: '' })).toBeFalsy(); expect(router.navigate).toHaveBeenCalled(); })) ); + it('should set redirect url', + async(inject([AuthGuardBpm, Router, AuthenticationService], (auth, router, authService) => { + const state = { url: 'some-url' }; + + spyOn(router, 'navigate'); + spyOn(authService, 'setRedirectUrl'); + + auth.canActivate(null , state); + + expect(authService.setRedirectUrl).toHaveBeenCalledWith(state.url); + })) + ); }); diff --git a/ng2-components/ng2-alfresco-core/src/services/auth-guard-bpm.service.ts b/ng2-components/ng2-alfresco-core/src/services/auth-guard-bpm.service.ts index f80006f8ca..9f302bf93d 100644 --- a/ng2-components/ng2-alfresco-core/src/services/auth-guard-bpm.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/auth-guard-bpm.service.ts @@ -29,24 +29,20 @@ export class AuthGuardBpm implements CanActivate, CanActivateChild { constructor(private authService: AuthenticationService, private router: Router) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { - // let url: string = state.url; - - return this.checkLogin(); + return this.checkLogin(state.url); } canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { return this.canActivate(route, state); } - checkLogin(): boolean { + checkLogin(redirectUrl: string): boolean { if (this.authService.isBpmLoggedIn()) { return true; } - // Store the attempted URL for redirecting - // this.authService.redirectUrl = url; + this.authService.setRedirectUrl(redirectUrl); - // Navigate to the login page with extras this.router.navigate(['/login']); return false; } diff --git a/ng2-components/ng2-alfresco-core/src/services/auth-guard-ecm.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/auth-guard-ecm.service.spec.ts index a6b5feead1..59ebbfccd4 100644 --- a/ng2-components/ng2-alfresco-core/src/services/auth-guard-ecm.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/auth-guard-ecm.service.spec.ts @@ -19,6 +19,7 @@ import { async, inject, TestBed } from '@angular/core/testing'; import { Router } from '@angular/router'; import { AlfrescoApiService } from './alfresco-api.service'; import { AuthGuardEcm } from './auth-guard-ecm.service'; +import { AuthenticationService } from './authentication.service'; class RouterProvider { navigate: Function = jasmine.createSpy('RouterProviderNavigate'); @@ -60,9 +61,14 @@ class AlfrescoApiServiceProvider { } } +class AuthenticationServiceProvider { + setRedirectUrl: Function = jasmine.createSpy('setRedirectUrl'); +} + class TestConfig { router: any; guard: any; + auth: any; private settings: any = { validateTicket: true, @@ -76,13 +82,15 @@ class TestConfig { providers: [ this.routerProvider, this.alfrescoApiServiceProvider, + this.authenticationProvider, AuthGuardEcm ] }); - inject([ AuthGuardEcm, Router ], (guard: AuthGuardEcm, router: Router) => { + inject([ AuthGuardEcm, Router, AuthenticationService ], (guard: AuthGuardEcm, router: Router, auth: AuthenticationService) => { this.guard = guard; this.router = router; + this.auth = auth; })(); } @@ -93,6 +101,13 @@ class TestConfig { }; } + private get authenticationProvider() { + return { + provide: AuthenticationService, + useValue: new AuthenticationServiceProvider() + }; + } + private get alfrescoApiServiceProvider () { const { validateTicket, isLoggedIn } = this.settings; @@ -119,7 +134,7 @@ describe('CanActivateLoggedIn', () => { const { guard, router } = this.test; - guard.canActivate().then((activate) => { + guard.canActivate(null, { url: '' }).then((activate) => { this.activate = activate; this.navigateSpy = router.navigate; }); @@ -143,7 +158,7 @@ describe('CanActivateLoggedIn', () => { const { guard, router } = this.test; - guard.canActivate().then((activate) => { + guard.canActivate(null, { url: '' }).then((activate) => { this.activate = activate; this.navigateSpy = router.navigate; }); @@ -167,7 +182,7 @@ describe('CanActivateLoggedIn', () => { const { guard, router } = this.test; - guard.canActivate().then((activate) => { + guard.canActivate(null, { url: '' }).then((activate) => { this.activate = activate; this.navigateSpy = router.navigate; }); @@ -181,4 +196,22 @@ describe('CanActivateLoggedIn', () => { expect(this.navigateSpy).not.toHaveBeenCalled(); }); }); + + describe('redirect url', () => { + beforeEach(async(() => { + this.test = new TestConfig({ + isLoggedIn: false + }); + + const { guard, auth } = this.test; + + guard.canActivate(null, { url: 'some-url' }).then((activate) => { + this.auth = auth; + }); + })); + + it('should set redirect url', () => { + expect(this.auth.setRedirectUrl).toHaveBeenCalledWith('some-url'); + }); + }); }); diff --git a/ng2-components/ng2-alfresco-core/src/services/auth-guard-ecm.service.ts b/ng2-components/ng2-alfresco-core/src/services/auth-guard-ecm.service.ts index ff30ca3382..9bcf07dc6b 100644 --- a/ng2-components/ng2-alfresco-core/src/services/auth-guard-ecm.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/auth-guard-ecm.service.ts @@ -16,13 +16,14 @@ */ import { Injectable } from '@angular/core'; -import { CanActivate, Router } from '@angular/router'; - +import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; import { AlfrescoApiService } from './alfresco-api.service'; +import { AuthenticationService } from './authentication.service'; @Injectable() export class AuthGuardEcm implements CanActivate { constructor( + private authService: AuthenticationService, private apiService: AlfrescoApiService, private router: Router) { } @@ -42,9 +43,11 @@ export class AuthGuardEcm implements CanActivate { .catch(() => false); } - canActivate(): Promise { + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise { + return this.isLoggedIn().then(isLoggedIn => { if (!isLoggedIn) { + this.authService.setRedirectUrl(state.url); this.router.navigate([ '/login' ]); } diff --git a/ng2-components/ng2-alfresco-core/src/services/auth-guard.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/auth-guard.service.spec.ts index 7becef283d..84b70f1776 100644 --- a/ng2-components/ng2-alfresco-core/src/services/auth-guard.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/auth-guard.service.spec.ts @@ -33,6 +33,7 @@ import { AlfrescoTranslateLoader } from './translate-loader.service'; import { UserPreferencesService } from './user-preferences.service'; describe('AuthGuardService', () => { + let state; beforeEach(async(() => { TestBed.configureTestingModule({ @@ -59,6 +60,10 @@ describe('AuthGuardService', () => { }).compileComponents(); })); + beforeEach(() => { + state = { url: '' }; + }); + it('if the alfresco js api is logged in should canActivate be true', async(inject([AuthGuard, Router, AlfrescoSettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => { spyOn(router, 'navigate'); @@ -67,7 +72,7 @@ describe('AuthGuardService', () => { return true; }; - expect(auth.canActivate()).toBeTruthy(); + expect(auth.canActivate(null, state)).toBeTruthy(); expect(router.navigate).not.toHaveBeenCalled(); })) ); @@ -81,9 +86,21 @@ describe('AuthGuardService', () => { return false; }; - expect(auth.canActivate()).toBeFalsy(); + expect(auth.canActivate(null, state)).toBeFalsy(); expect(router.navigate).toHaveBeenCalled(); })) ); + it('should set redirect url', + async(inject([AuthGuard, Router, AuthenticationService], (auth, router, authService) => { + state.url = 'some-url'; + + spyOn(router, 'navigate'); + spyOn(authService, 'setRedirectUrl'); + + auth.canActivate(null , state); + + expect(authService.setRedirectUrl).toHaveBeenCalledWith(state.url); + })) + ); }); diff --git a/ng2-components/ng2-alfresco-core/src/services/auth-guard.service.ts b/ng2-components/ng2-alfresco-core/src/services/auth-guard.service.ts index 2eedeec6af..d10cd781d4 100644 --- a/ng2-components/ng2-alfresco-core/src/services/auth-guard.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/auth-guard.service.ts @@ -29,24 +29,22 @@ export class AuthGuard implements CanActivate, CanActivateChild { constructor(private authService: AuthenticationService, private router: Router) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { - // let url: string = state.url; + const redirectUrl = state.url; - return this.checkLogin(); + return this.checkLogin(redirectUrl); } canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { return this.canActivate(route, state); } - checkLogin(): boolean { + checkLogin(redirectUrl: string): boolean { if (this.authService.isLoggedIn()) { return true; } - // Store the attempted URL for redirecting - // this.authService.redirectUrl = url; + this.authService.setRedirectUrl(redirectUrl); - // Navigate to the login page with extras this.router.navigate(['/login']); return false; } diff --git a/ng2-components/ng2-alfresco-core/src/services/authentication.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/authentication.service.spec.ts index 4afe04bf6b..081c938a04 100644 --- a/ng2-components/ng2-alfresco-core/src/services/authentication.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/authentication.service.spec.ts @@ -464,4 +464,10 @@ describe('AuthenticationService', () => { }); }); }); + + it('should set/get redirectUrl', () => { + authService.setRedirectUrl('some-url'); + + expect(authService.getRedirectUrl()).toBe('some-url'); + }); }); diff --git a/ng2-components/ng2-alfresco-core/src/services/authentication.service.ts b/ng2-components/ng2-alfresco-core/src/services/authentication.service.ts index 72808dc933..af73e6af53 100644 --- a/ng2-components/ng2-alfresco-core/src/services/authentication.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/authentication.service.ts @@ -28,6 +28,7 @@ const REMEMBER_ME_UNTIL = 1000 * 60 * 60 * 24 * 30 ; @Injectable() export class AuthenticationService { + private redirectUrl: string = ''; onLogin: Subject = new Subject(); onLogout: Subject = new Subject(); @@ -228,6 +229,14 @@ export class AuthenticationService { return this.alfrescoApi.getInstance().bpmAuth.username; } + setRedirectUrl(url: string) { + this.redirectUrl = url; + } + + getRedirectUrl(): string { + return this.redirectUrl; + } + /** * The method write the error in the console browser * @param error diff --git a/ng2-components/ng2-alfresco-login/src/assets/authentication.service.mock.ts b/ng2-components/ng2-alfresco-login/src/assets/authentication.service.mock.ts index f029e4e5d7..37b4178867 100644 --- a/ng2-components/ng2-alfresco-login/src/assets/authentication.service.mock.ts +++ b/ng2-components/ng2-alfresco-login/src/assets/authentication.service.mock.ts @@ -19,6 +19,15 @@ import { Observable } from 'rxjs/Rx'; // TODO: should be extending AlfrescoAuthenticationService export class AuthenticationMock /*extends AlfrescoAuthenticationService*/ { + private redirectUrl: string = ''; + + setRedirectUrl(url: string) { + this.redirectUrl = url; + } + + getRedirectUrl(): string { + return this.redirectUrl; + } // TODO: real auth service returns Observable login(username: string, password: string): Observable<{ type: string, ticket: any }> { diff --git a/ng2-components/ng2-alfresco-login/src/components/login.component.spec.ts b/ng2-components/ng2-alfresco-login/src/components/login.component.spec.ts index 6507c6ced3..4d06055eee 100644 --- a/ng2-components/ng2-alfresco-login/src/components/login.component.spec.ts +++ b/ng2-components/ng2-alfresco-login/src/components/login.component.spec.ts @@ -114,6 +114,17 @@ describe('LoginComponent', () => { expect(router.navigate).toHaveBeenCalledWith([redirect]); }); + it('should redirect to previous route state on successful login', () => { + const redirect = '/home'; + component.successRoute = redirect; + authService.setRedirectUrl('redirect-url'); + + spyOn(router, 'navigate'); + + loginWithCredentials('fake-username', 'fake-password'); + expect(router.navigate).toHaveBeenCalledWith(['redirect-url']); + }); + it('should update user preferences upon login', (done) => { spyOn(userPreferences, 'setStoragePrefix').and.callThrough(); diff --git a/ng2-components/ng2-alfresco-login/src/components/login.component.ts b/ng2-components/ng2-alfresco-login/src/components/login.component.ts index 8770106034..aefe723e65 100644 --- a/ng2-components/ng2-alfresco-login/src/components/login.component.ts +++ b/ng2-components/ng2-alfresco-login/src/components/login.component.ts @@ -183,9 +183,18 @@ export class LoginComponent implements OnInit { this.authService.login(values.username, values.password, this.rememberMe) .subscribe( (token: any) => { + const redirectUrl = this.authService.getRedirectUrl(); + this.actualLoginStep = LoginSteps.Welcome; this.userPreferences.setStoragePrefix(values.username); this.success.emit(new LoginSuccessEvent(token, values.username, values.password)); + + if (redirectUrl) { + this.authService.setRedirectUrl(null); + this.router.navigate([redirectUrl]); + return false; + } + if (this.successRoute) { this.router.navigate([this.successRoute]); }