mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-19 17:14:57 +00:00
improved ecm auth guard (#2402)
This commit is contained in:
parent
7c1bd46642
commit
250aa360d2
@ -17,67 +17,169 @@
|
|||||||
|
|
||||||
import { async, inject, TestBed } from '@angular/core/testing';
|
import { async, inject, TestBed } from '@angular/core/testing';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { RouterTestingModule } from '@angular/router/testing';
|
import { AlfrescoApiService } from 'ng2-alfresco-core';
|
||||||
|
|
||||||
import { CookieServiceMock } from './../assets/cookie.service.mock';
|
|
||||||
import { AlfrescoApiService } from './alfresco-api.service';
|
|
||||||
import { AlfrescoSettingsService } from './alfresco-settings.service';
|
|
||||||
import { AppConfigModule } from './app-config.service';
|
|
||||||
import { AuthGuardEcm } from './auth-guard-ecm.service';
|
import { AuthGuardEcm } from './auth-guard-ecm.service';
|
||||||
import { AuthenticationService } from './authentication.service';
|
|
||||||
import { CookieService } from './cookie.service';
|
|
||||||
import { LogService } from './log.service';
|
|
||||||
import { StorageService } from './storage.service';
|
|
||||||
import { UserPreferencesService } from './user-preferences.service';
|
|
||||||
|
|
||||||
describe('AuthGuardService ECM', () => {
|
class RouterProvider {
|
||||||
|
navigate: Function = jasmine.createSpy('RouterProviderNavigate');
|
||||||
|
}
|
||||||
|
|
||||||
|
class AlfrescoApiServiceProvider {
|
||||||
|
private settings: any = {
|
||||||
|
validateTicket: true,
|
||||||
|
isLoggedIn: true
|
||||||
|
};
|
||||||
|
|
||||||
|
constructor(settings: any = {}) {
|
||||||
|
Object.assign(this.settings, settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
getInstance() {
|
||||||
|
return {
|
||||||
|
ecmAuth: this.ecmAuth
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private get ecmAuth() {
|
||||||
|
return {
|
||||||
|
validateTicket: this.validateTicket.bind(this),
|
||||||
|
isLoggedIn: this.isLoggedIn.bind(this)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private validateTicket() {
|
||||||
|
const { validateTicket } = this.settings;
|
||||||
|
|
||||||
|
return validateTicket
|
||||||
|
? Promise.resolve('Valid!')
|
||||||
|
: Promise.reject('Invalid');
|
||||||
|
}
|
||||||
|
|
||||||
|
private isLoggedIn() {
|
||||||
|
return this.settings.isLoggedIn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestConfig {
|
||||||
|
router: any;
|
||||||
|
guard: any;
|
||||||
|
|
||||||
|
private settings: any = {
|
||||||
|
validateTicket: true,
|
||||||
|
isLoggedIn: true
|
||||||
|
};
|
||||||
|
|
||||||
|
constructor(settings: any = {}) {
|
||||||
|
Object.assign(this.settings, settings);
|
||||||
|
|
||||||
beforeEach(async(() => {
|
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
imports: [
|
|
||||||
AppConfigModule,
|
|
||||||
RouterTestingModule
|
|
||||||
],
|
|
||||||
declarations: [
|
|
||||||
],
|
|
||||||
providers: [
|
providers: [
|
||||||
AuthGuardEcm,
|
this.routerProvider,
|
||||||
AlfrescoSettingsService,
|
this.alfrescoApiServiceProvider,
|
||||||
AlfrescoApiService,
|
AuthGuardEcm
|
||||||
AuthenticationService,
|
|
||||||
StorageService,
|
|
||||||
UserPreferencesService,
|
|
||||||
{ provide: CookieService, useClass: CookieServiceMock },
|
|
||||||
LogService
|
|
||||||
]
|
]
|
||||||
}).compileComponents();
|
});
|
||||||
}));
|
|
||||||
|
|
||||||
it('if the alfresco js api is logged in should canActivate be true',
|
inject([ AuthGuardEcm, Router ], (guard: AuthGuardEcm, router: Router) => {
|
||||||
async(inject([AuthGuardEcm, Router, AlfrescoSettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => {
|
this.guard = guard;
|
||||||
spyOn(router, 'navigate');
|
this.router = router;
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
authService.isEcmLoggedIn = () => {
|
private get routerProvider() {
|
||||||
return true;
|
return {
|
||||||
};
|
provide: Router,
|
||||||
|
useValue: new RouterProvider()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
expect(auth.canActivate()).toBeTruthy();
|
private get alfrescoApiServiceProvider () {
|
||||||
expect(router.navigate).not.toHaveBeenCalled();
|
const { validateTicket, isLoggedIn } = this.settings;
|
||||||
}))
|
|
||||||
);
|
|
||||||
|
|
||||||
it('if the alfresco js api is NOT logged in should canActivate be false',
|
return {
|
||||||
async(inject([AuthGuardEcm, Router, AlfrescoSettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => {
|
provide: AlfrescoApiService,
|
||||||
|
useValue: new AlfrescoApiServiceProvider({
|
||||||
|
validateTicket,
|
||||||
|
isLoggedIn
|
||||||
|
})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
spyOn(router, 'navigate');
|
get navigateSpy() {
|
||||||
|
return this.router.navigate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
authService.isEcmLoggedIn = () => {
|
describe('CanActivateLoggedIn', () => {
|
||||||
return false;
|
describe('user is not logged in', () => {
|
||||||
};
|
beforeEach(async(() => {
|
||||||
|
this.test = new TestConfig({
|
||||||
|
isLoggedIn: false
|
||||||
|
});
|
||||||
|
|
||||||
expect(auth.canActivate()).toBeFalsy();
|
const { guard, router } = this.test;
|
||||||
expect(router.navigate).toHaveBeenCalled();
|
|
||||||
}))
|
|
||||||
);
|
|
||||||
|
|
||||||
|
guard.canActivate().then((activate) => {
|
||||||
|
this.activate = activate;
|
||||||
|
this.navigateSpy = router.navigate;
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('does not allow route to activate', () => {
|
||||||
|
expect(this.activate).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('redirects to /login', () => {
|
||||||
|
expect(this.navigateSpy).toHaveBeenCalledWith([ '/login' ]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('user is logged in but ticket is invalid', () => {
|
||||||
|
beforeEach(async(() => {
|
||||||
|
this.test = new TestConfig({
|
||||||
|
isLoggedIn: true,
|
||||||
|
validateTicket: false
|
||||||
|
});
|
||||||
|
|
||||||
|
const { guard, router } = this.test;
|
||||||
|
|
||||||
|
guard.canActivate().then((activate) => {
|
||||||
|
this.activate = activate;
|
||||||
|
this.navigateSpy = router.navigate;
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('does not allow route to activate', () => {
|
||||||
|
expect(this.activate).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('redirects to /login', () => {
|
||||||
|
expect(this.navigateSpy).toHaveBeenCalledWith([ '/login' ]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('user is logged in and ticket is valid', () => {
|
||||||
|
beforeEach(async(() => {
|
||||||
|
this.test = new TestConfig({
|
||||||
|
isLoggedIn: true,
|
||||||
|
validateTicket: true
|
||||||
|
});
|
||||||
|
|
||||||
|
const { guard, router } = this.test;
|
||||||
|
|
||||||
|
guard.canActivate().then((activate) => {
|
||||||
|
this.activate = activate;
|
||||||
|
this.navigateSpy = router.navigate;
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('allows route to activate', () => {
|
||||||
|
expect(this.activate).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not redirect', () => {
|
||||||
|
expect(this.navigateSpy).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -16,38 +16,39 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import {
|
import { CanActivate, Router } from '@angular/router';
|
||||||
ActivatedRouteSnapshot, CanActivate, CanActivateChild,
|
|
||||||
Router,
|
|
||||||
RouterStateSnapshot
|
|
||||||
} from '@angular/router';
|
|
||||||
|
|
||||||
import { AuthenticationService } from './authentication.service';
|
import { AlfrescoApiService } from './alfresco-api.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthGuardEcm implements CanActivate, CanActivateChild {
|
export class AuthGuardEcm implements CanActivate {
|
||||||
constructor(private authService: AuthenticationService, private router: Router) {}
|
constructor(
|
||||||
|
private apiService: AlfrescoApiService,
|
||||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
|
private router: Router) {
|
||||||
// let url: string = state.url;
|
|
||||||
|
|
||||||
return this.checkLogin();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
|
private get authApi() {
|
||||||
return this.canActivate(route, state);
|
return this.apiService.getInstance().ecmAuth;
|
||||||
}
|
}
|
||||||
|
|
||||||
checkLogin(): boolean {
|
private isLoggedIn(): Promise<boolean> {
|
||||||
if (this.authService.isEcmLoggedIn()) {
|
if (!this.authApi.isLoggedIn()) {
|
||||||
return true;
|
return Promise.resolve(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the attempted URL for redirecting
|
return this.authApi
|
||||||
// this.authService.redirectUrl = url;
|
.validateTicket()
|
||||||
|
.then(() => true, () => false)
|
||||||
|
.catch(() => false);
|
||||||
|
}
|
||||||
|
|
||||||
// Navigate to the login page with extras
|
canActivate(): Promise<boolean> {
|
||||||
this.router.navigate(['/login']);
|
return this.isLoggedIn().then(isLoggedIn => {
|
||||||
return false;
|
if (!isLoggedIn) {
|
||||||
|
this.router.navigate([ '/login' ]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return isLoggedIn;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user