mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
core and login component
This commit is contained in:
@@ -15,9 +15,9 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Component } from '@angular/core';
|
import {Component} from '@angular/core';
|
||||||
import { AlfrescoLoginComponent } from 'ng2-alfresco-login';
|
import {AlfrescoLoginComponent} from 'ng2-alfresco-login';
|
||||||
import { ROUTER_DIRECTIVES, Router } from '@angular/router';
|
import {ROUTER_DIRECTIVES, Router} from '@angular/router';
|
||||||
|
|
||||||
declare let __moduleName: string;
|
declare let __moduleName: string;
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ declare let __moduleName: string;
|
|||||||
})
|
})
|
||||||
export class LoginDemoComponent {
|
export class LoginDemoComponent {
|
||||||
|
|
||||||
providers: string [] = ['ECM'];
|
providers: string = 'ECM';
|
||||||
|
|
||||||
constructor(public router: Router) {
|
constructor(public router: Router) {
|
||||||
}
|
}
|
||||||
@@ -45,28 +45,22 @@ export class LoginDemoComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
toggleECM(checked) {
|
toggleECM(checked) {
|
||||||
if (checked) {
|
if (checked && this.providers === 'BPM') {
|
||||||
this.providers.push('ECM');
|
this.providers = 'ALL';
|
||||||
|
} else if (checked) {
|
||||||
|
this.providers = 'ECM';
|
||||||
} else {
|
} else {
|
||||||
this.removeElement('ECM');
|
this.providers = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleBPM(checked) {
|
toggleBPM(checked) {
|
||||||
if (checked) {
|
if (checked && this.providers === 'ECM') {
|
||||||
this.providers.push('BPM');
|
this.providers = 'ALL';
|
||||||
|
} else if (checked) {
|
||||||
|
this.providers = 'BPM';
|
||||||
} else {
|
} else {
|
||||||
this.removeElement('BPM');
|
this.providers = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeElement(el: string) {
|
|
||||||
for (let i = 0; i < this.providers.length; i++) {
|
|
||||||
if (this.providers[i] === el) {
|
|
||||||
this.providers.splice(i, 1);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,37 +0,0 @@
|
|||||||
/*!
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { AbstractAuthentication } from '../interface/authentication.interface';
|
|
||||||
import { AlfrescoAuthenticationBPM } from '../services/AlfrescoAuthenticationBPM.service';
|
|
||||||
import { AlfrescoAuthenticationECM } from '../services/AlfrescoAuthenticationECM.service';
|
|
||||||
import { Http } from '@angular/http';
|
|
||||||
import { AlfrescoSettingsService } from '../services/AlfrescoSettingsService.service';
|
|
||||||
|
|
||||||
|
|
||||||
export class AuthenticationFactory {
|
|
||||||
|
|
||||||
public static createAuth(alfrescoSettingsService: AlfrescoSettingsService,
|
|
||||||
http: Http,
|
|
||||||
type: string): AbstractAuthentication {
|
|
||||||
if (type === 'ECM') {
|
|
||||||
return new AlfrescoAuthenticationECM(alfrescoSettingsService, http);
|
|
||||||
} else if (type === 'BPM') {
|
|
||||||
return new AlfrescoAuthenticationBPM(alfrescoSettingsService, http);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,116 +0,0 @@
|
|||||||
/*!
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { AbstractAuthentication } from '../interface/authentication.interface';
|
|
||||||
import { Http, Headers, RequestOptions } from '@angular/http';
|
|
||||||
import { Observable } from 'rxjs/Rx';
|
|
||||||
import { AlfrescoAuthenticationBase } from './AlfrescoAuthenticationBase.service';
|
|
||||||
import { AlfrescoSettingsService } from './AlfrescoSettingsService.service';
|
|
||||||
|
|
||||||
export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implements AbstractAuthentication {
|
|
||||||
|
|
||||||
TYPE: string = 'BPM';
|
|
||||||
|
|
||||||
constructor(alfrescoSetting: AlfrescoSettingsService,
|
|
||||||
http: Http) {
|
|
||||||
super(alfrescoSetting, http);
|
|
||||||
}
|
|
||||||
|
|
||||||
getHost(): string {
|
|
||||||
return this.alfrescoSetting.bpmHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a login on behalf of the user and store the ticket returned
|
|
||||||
*
|
|
||||||
* @param username
|
|
||||||
* @param password
|
|
||||||
* @returns {Observable<R>|Observable<T>}
|
|
||||||
*/
|
|
||||||
login(username: string, password: string): Observable<any> {
|
|
||||||
return Observable.fromPromise(this.apiActivitiLogin(username, password))
|
|
||||||
.map((response: any) => {
|
|
||||||
return {
|
|
||||||
type: this.TYPE,
|
|
||||||
ticket: 'Basic ' + btoa(`${username}:${password}`)
|
|
||||||
};
|
|
||||||
})
|
|
||||||
.catch(this.handleError);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete the current login ticket from the server
|
|
||||||
*
|
|
||||||
* @returns {Observable<R>|Observable<T>}
|
|
||||||
*/
|
|
||||||
logout() {
|
|
||||||
return Observable.fromPromise(this.apiActivitiLogout())
|
|
||||||
.map(res => <any> res)
|
|
||||||
.do(response => {
|
|
||||||
this.removeTicket(this.TYPE);
|
|
||||||
})
|
|
||||||
.catch(this.handleError);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The method return true if the user is logged in
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
isLoggedIn(): boolean {
|
|
||||||
return !!this.getTicket();
|
|
||||||
}
|
|
||||||
|
|
||||||
private apiActivitiLogin(username: string, password: string) {
|
|
||||||
let url = this.alfrescoSetting.getBPMApiBaseUrl() + '/app/authentication';
|
|
||||||
let headers = new Headers({
|
|
||||||
'Content-Type': 'application/x-www-form-urlencoded'
|
|
||||||
});
|
|
||||||
let options = new RequestOptions({headers: headers});
|
|
||||||
let data = 'j_username='
|
|
||||||
+ encodeURIComponent(username)
|
|
||||||
+ '&j_password='
|
|
||||||
+ encodeURIComponent(password)
|
|
||||||
+ '&_spring_security_remember_me=true&submit=Login';
|
|
||||||
|
|
||||||
return this.http
|
|
||||||
.post(url, data, options).toPromise();
|
|
||||||
}
|
|
||||||
|
|
||||||
private apiActivitiLogout() {
|
|
||||||
let url = this.alfrescoSetting.getBPMApiBaseUrl() + '/app/logout';
|
|
||||||
return this.http.get(url).toPromise();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The method return the ticket stored in the localStorage
|
|
||||||
* @returns ticket
|
|
||||||
*/
|
|
||||||
public getTicket(): string {
|
|
||||||
return localStorage.getItem(`ticket-${this.TYPE}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The method save the ticket in the localStorage
|
|
||||||
* @param ticket
|
|
||||||
*/
|
|
||||||
public saveTicket(ticket: string): void {
|
|
||||||
if (ticket) {
|
|
||||||
super.saveTicket(this.TYPE, ticket);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,64 +0,0 @@
|
|||||||
/*!
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { Http, Response } from '@angular/http';
|
|
||||||
import { AlfrescoSettingsService } from './AlfrescoSettingsService.service';
|
|
||||||
import { Observable } from 'rxjs/Rx';
|
|
||||||
|
|
||||||
declare let AlfrescoApi: any;
|
|
||||||
|
|
||||||
export class AlfrescoAuthenticationBase {
|
|
||||||
|
|
||||||
alfrescoApi: any;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
* @param alfrescoSettingsService
|
|
||||||
*/
|
|
||||||
constructor(public alfrescoSetting: AlfrescoSettingsService,
|
|
||||||
public http: Http) {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The method save the toke in the localStorage
|
|
||||||
* @param ticket
|
|
||||||
*/
|
|
||||||
public saveTicket(provider: string, ticket: string): void {
|
|
||||||
if (ticket) {
|
|
||||||
localStorage.setItem(`ticket-${provider}`, ticket);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove the login ticket from localStorage
|
|
||||||
*/
|
|
||||||
public removeTicket(provider: string): void {
|
|
||||||
localStorage.removeItem(`ticket-${provider}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The method write the error in the console browser
|
|
||||||
* @param error
|
|
||||||
* @returns {ErrorObservable}
|
|
||||||
*/
|
|
||||||
public handleError(error: Response): Observable<any> {
|
|
||||||
console.error('Error when logging in', error);
|
|
||||||
return Observable.throw(error || 'Server error');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,137 +0,0 @@
|
|||||||
/*!
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { AbstractAuthentication } from '../interface/authentication.interface';
|
|
||||||
import { Observable } from 'rxjs/Rx';
|
|
||||||
import { Http } from '@angular/http';
|
|
||||||
import { AlfrescoAuthenticationBase } from './AlfrescoAuthenticationBase.service';
|
|
||||||
import { AlfrescoSettingsService } from './AlfrescoSettingsService.service';
|
|
||||||
|
|
||||||
declare let AlfrescoApi: any;
|
|
||||||
|
|
||||||
export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implements AbstractAuthentication {
|
|
||||||
|
|
||||||
TYPE: string = 'ECM';
|
|
||||||
|
|
||||||
alfrescoApi: any;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
* @param alfrescoSetting
|
|
||||||
* @param http
|
|
||||||
*/
|
|
||||||
constructor(alfrescoSetting: AlfrescoSettingsService,
|
|
||||||
http: Http) {
|
|
||||||
super(alfrescoSetting, http);
|
|
||||||
|
|
||||||
if (!this.isLoggedIn()) {
|
|
||||||
this.alfrescoApi = new AlfrescoApi({
|
|
||||||
host: this.getHost()
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.alfrescoApi = new AlfrescoApi({
|
|
||||||
ticket: this.getTicket(),
|
|
||||||
host: this.getHost()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getHost(): string {
|
|
||||||
return this.alfrescoSetting.ecmHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The method return tru if the user is logged in
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
isLoggedIn(): boolean {
|
|
||||||
return !!this.getTicket();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method to delegate to POST login
|
|
||||||
* @param username
|
|
||||||
* @param password
|
|
||||||
* @returns {Observable<R>|Observable<T>}
|
|
||||||
*/
|
|
||||||
login(username: string, password: string) {
|
|
||||||
|
|
||||||
return Observable.fromPromise(this.callApiLogin(username, password))
|
|
||||||
.map((response: any) => {
|
|
||||||
return {type: this.TYPE, ticket: response};
|
|
||||||
})
|
|
||||||
.catch(this.handleError);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize the alfresco Api with user and password end call the login method
|
|
||||||
* @param username
|
|
||||||
* @param password
|
|
||||||
* @returns {*|Observable<any>}
|
|
||||||
*/
|
|
||||||
private callApiLogin(username: string, password: string) {
|
|
||||||
this.alfrescoApi = new AlfrescoApi({
|
|
||||||
username: username,
|
|
||||||
password: password,
|
|
||||||
host: this.getHost()
|
|
||||||
});
|
|
||||||
return this.alfrescoApi.login();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The method remove the ticket from the local storage
|
|
||||||
*
|
|
||||||
* @returns {Observable<R>|Observable<T>}
|
|
||||||
*/
|
|
||||||
public logout() {
|
|
||||||
return Observable.fromPromise(this.callApiLogout())
|
|
||||||
.map(res => <any> res)
|
|
||||||
.do(response => {
|
|
||||||
this.removeTicket(this.TYPE);
|
|
||||||
return response;
|
|
||||||
})
|
|
||||||
.catch(this.handleError);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @returns {*|Observable<string>|Observable<any>|Promise<T>}
|
|
||||||
*/
|
|
||||||
private callApiLogout(): Promise<any> {
|
|
||||||
return this.alfrescoApi.logout();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The method return the ticket stored in the localStorage
|
|
||||||
* @returns ticket
|
|
||||||
*/
|
|
||||||
public getTicket(): string {
|
|
||||||
return localStorage.getItem(`ticket-${this.TYPE}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The method save the ticket in the localStorage
|
|
||||||
* @param ticket
|
|
||||||
*/
|
|
||||||
public saveTicket(ticket): void {
|
|
||||||
if (ticket) {
|
|
||||||
super.saveTicket(this.TYPE, ticket);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -15,22 +15,17 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { it, describe } from '@angular/core/testing';
|
import {it, describe} from '@angular/core/testing';
|
||||||
import { ReflectiveInjector, provide } from '@angular/core';
|
import {ReflectiveInjector, provide} from '@angular/core';
|
||||||
import { AlfrescoSettingsService } from './AlfrescoSettingsService.service';
|
import {AlfrescoSettingsService} from './AlfrescoSettingsService.service';
|
||||||
import { AlfrescoAuthenticationService } from './AlfrescoAuthenticationService.service';
|
import {AlfrescoAuthenticationService} from './AlfrescoAuthenticationService.service';
|
||||||
import { AlfrescoAuthenticationECM } from './AlfrescoAuthenticationECM.service';
|
import {XHRBackend, HTTP_PROVIDERS} from '@angular/http';
|
||||||
import { AlfrescoAuthenticationBPM } from './AlfrescoAuthenticationBPM.service';
|
import {MockBackend} from '@angular/http/testing';
|
||||||
import { XHRBackend, HTTP_PROVIDERS } from '@angular/http';
|
|
||||||
import { MockBackend } from '@angular/http/testing';
|
|
||||||
|
|
||||||
declare var AlfrescoApi: any;
|
declare var AlfrescoApi: any;
|
||||||
|
|
||||||
describe('AlfrescoAuthentication', () => {
|
describe('AlfrescoAuthentication', () => {
|
||||||
let injector,
|
let injector, fakePromiseECM, fakePromiseBPM, service, fakePromiseBPMECM;
|
||||||
fakePromiseECM,
|
|
||||||
fakePromiseBPM,
|
|
||||||
service;
|
|
||||||
|
|
||||||
fakePromiseECM = new Promise(function (resolve, reject) {
|
fakePromiseECM = new Promise(function (resolve, reject) {
|
||||||
resolve(
|
resolve(
|
||||||
@@ -44,9 +39,18 @@ describe('AlfrescoAuthentication', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
fakePromiseBPM = new Promise(function (resolve, reject) {
|
fakePromiseBPM = new Promise(function (resolve, reject) {
|
||||||
resolve({
|
resolve(
|
||||||
status: 'fake-post-ticket-BPM'
|
'fake-post-ticket-BPM'
|
||||||
|
);
|
||||||
|
reject({
|
||||||
|
response: {
|
||||||
|
error: 'fake-error'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
fakePromiseBPMECM = new Promise(function (resolve, reject) {
|
||||||
|
resolve(['fake-post-ticket-ECM', 'fake-post-ticket-BPM']);
|
||||||
reject({
|
reject({
|
||||||
response: {
|
response: {
|
||||||
error: 'fake-error'
|
error: 'fake-error'
|
||||||
@@ -81,380 +85,176 @@ describe('AlfrescoAuthentication', () => {
|
|||||||
return keys[i] || null;
|
return keys[i] || null;
|
||||||
});
|
});
|
||||||
|
|
||||||
// service = injector.get(AlfrescoAuthenticationService);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when the setting is ECM', () => {
|
describe('when the setting is ECM', () => {
|
||||||
|
|
||||||
it('should create an AlfrescoAuthenticationECM instance', (done) => {
|
beforeEach(() => {
|
||||||
let providers = ['ECM'];
|
this.providers = 'ECM';
|
||||||
|
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
service = injector.get(AlfrescoAuthenticationService);
|
||||||
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
|
|
||||||
|
|
||||||
service.login('fake-username', 'fake-password', providers)
|
|
||||||
.subscribe(() => {
|
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(true);
|
|
||||||
expect(service.providersInstance).toBeDefined();
|
|
||||||
expect(service.providersInstance.length).toBe(1);
|
|
||||||
expect(service.providersInstance[0].TYPE).toEqual(providers[0]);
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an ECM ticket after the login done', (done) => {
|
it('should return an ECM ticket after the login done', (done) => {
|
||||||
let providers = ['ECM'];
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
service.login('fake-username', 'fake-password', this.providers).subscribe(() => {
|
||||||
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
|
expect(service.isLoggedIn()).toBe(true);
|
||||||
|
expect(service.getTicket()).toEqual('fake-post-ticket-ECM');
|
||||||
service.login('fake-username', 'fake-password', providers)
|
done();
|
||||||
.subscribe(() => {
|
});
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(true);
|
|
||||||
expect(service.getTicket(providers[0])).toEqual('fake-post-ticket-ECM');
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return ticket undefined when the credentials are wrong', (done) => {
|
it('should return ticket undefined when the credentials are wrong', (done) => {
|
||||||
let providers = ['ECM'];
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin')
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
|
||||||
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin')
|
|
||||||
.and.returnValue(Promise.reject('fake invalid credentials'));
|
.and.returnValue(Promise.reject('fake invalid credentials'));
|
||||||
|
|
||||||
service.login('fake-wrong-username', 'fake-wrong-password', providers)
|
service.login('fake-wrong-username', 'fake-wrong-password', this.providers).subscribe(
|
||||||
.subscribe(
|
|
||||||
(res) => {
|
(res) => {
|
||||||
done();
|
|
||||||
},
|
},
|
||||||
(err: any) => {
|
(err: any) => {
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(false);
|
expect(service.isLoggedIn()).toBe(false);
|
||||||
expect(service.getTicket(providers[0])).toBeUndefined();
|
expect(service.getTicket()).toBeUndefined();
|
||||||
done();
|
done();
|
||||||
}
|
});
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an error if no provider are defined calling the login', (done) => {
|
it('should login in the ECM if no provider are defined calling the login', (done) => {
|
||||||
let providers = [];
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
service.login('fake-username', 'fake-password').subscribe(() => {
|
||||||
service.login('fake-username', 'fake-password', providers)
|
service.TYPE = 'ECM';
|
||||||
.subscribe(
|
done();
|
||||||
(res) => {
|
});
|
||||||
done();
|
|
||||||
},
|
|
||||||
(err: any) => {
|
|
||||||
expect(err).toBeDefined();
|
|
||||||
expect(err).toEqual('No providers defined');
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return an error if an empty provider are defined calling the login', (done) => {
|
|
||||||
let providers = [''];
|
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
|
||||||
service.login('fake-username', 'fake-password', providers)
|
|
||||||
.subscribe(
|
|
||||||
(res) => {
|
|
||||||
done();
|
|
||||||
},
|
|
||||||
(err: any) => {
|
|
||||||
expect(err).toBeDefined();
|
|
||||||
expect(err.message).toEqual('Wrong provider defined');
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a ticket undefined after logout', (done) => {
|
it('should return a ticket undefined after logout', (done) => {
|
||||||
let providers = ['ECM'];
|
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
|
||||||
localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM');
|
localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM');
|
||||||
service.createProviderInstance(providers);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseECM);
|
||||||
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogout').and.returnValue(fakePromiseECM);
|
|
||||||
|
|
||||||
service.logout()
|
service.logout().subscribe(() => {
|
||||||
.subscribe(() => {
|
expect(service.isLoggedIn()).toBe(false);
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(false);
|
expect(service.getTicket()).toBeUndefined();
|
||||||
expect(service.getTicket(providers[0])).toBeUndefined();
|
expect(localStorage.getItem('ticket-ECM')).toBeUndefined();
|
||||||
expect(localStorage.getItem('ticket-ECM')).toBeUndefined();
|
done();
|
||||||
done();
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should logout only for if the provider is loggedin', (done) => {
|
it('should logout only if the provider is already logged in', (done) => {
|
||||||
let providers = ['BPM', 'ECM'];
|
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
|
||||||
localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM');
|
localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM');
|
||||||
service.createProviderInstance(providers);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseECM);
|
||||||
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogout').and.returnValue(fakePromiseECM);
|
service.saveTicket('fake-ticket-ECM');
|
||||||
service.performeSaveTicket('ECM', 'fake-ticket-ECM');
|
service.logout().subscribe(() => {
|
||||||
service.logout()
|
expect(service.isLoggedIn()).toBe(false);
|
||||||
.subscribe(() => {
|
expect(service.getTicket()).toBeUndefined();
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(false);
|
expect(localStorage.getItem('ticket-ECM')).toBeUndefined();
|
||||||
expect(service.getTicket(providers[0])).toBeUndefined();
|
done();
|
||||||
expect(localStorage.getItem('ticket-ECM')).toBeUndefined();
|
});
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
it('should return an error if no provider are defined calling the logout', (done) => {
|
|
||||||
let providers = [];
|
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
|
||||||
service.logout()
|
|
||||||
.subscribe(
|
|
||||||
(res) => {
|
|
||||||
done();
|
|
||||||
},
|
|
||||||
(err: any) => {
|
|
||||||
expect(err).toBeDefined();
|
|
||||||
expect(err).toEqual('No providers defined');
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if the user is not logged in', () => {
|
it('should return false if the user is not logged in', () => {
|
||||||
let providers = ['ECM'];
|
expect(service.isLoggedIn()).toBe(false);
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(false);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when the setting is BPM', () => {
|
describe('when the setting is BPM', () => {
|
||||||
|
|
||||||
it('should create an AlfrescoAuthenticationBPM instance', (done) => {
|
beforeEach(() => {
|
||||||
let providers = ['BPM'];
|
this.providers = 'BPM';
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
service = injector.get(AlfrescoAuthenticationService);
|
||||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
|
|
||||||
|
|
||||||
service.login('fake-username', 'fake-password', providers)
|
|
||||||
.subscribe(() => {
|
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(true);
|
|
||||||
expect(service.providersInstance).toBeDefined();
|
|
||||||
expect(service.providersInstance.length).toBe(1);
|
|
||||||
expect(service.providersInstance[0].TYPE).toEqual(providers[0]);
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should return an BPM ticket after the login done', (done) => {
|
it('should return an BPM ticket after the login done', (done) => {
|
||||||
let providers = ['BPM'];
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPM);
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
service.login('fake-username', 'fake-password', this.providers).subscribe(() => {
|
||||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
|
expect(service.isLoggedIn()).toBe(true);
|
||||||
|
expect(service.getTicket()).toEqual('fake-post-ticket-BPM');
|
||||||
let username = 'fake-username';
|
done();
|
||||||
let password = 'fake-password';
|
});
|
||||||
let token = 'Basic ' + btoa(`${username}:${password}`);
|
|
||||||
|
|
||||||
service.login(username, password, providers)
|
|
||||||
.subscribe(() => {
|
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(true);
|
|
||||||
expect(service.getTicket(providers[0])).toEqual(token);
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return ticket undefined when the credentials are wrong', (done) => {
|
it('should return ticket undefined when the credentials are wrong', (done) => {
|
||||||
let providers = ['BPM'];
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(Promise.reject('fake invalid credentials'));
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
service.login('fake-wrong-username', 'fake-wrong-password', this.providers).subscribe(
|
||||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(Promise.reject('fake invalid credentials'));
|
|
||||||
|
|
||||||
service.login('fake-wrong-username', 'fake-wrong-password', providers)
|
|
||||||
.subscribe(
|
|
||||||
(res) => {
|
(res) => {
|
||||||
done();
|
|
||||||
},
|
},
|
||||||
(err: any) => {
|
(err: any) => {
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(false);
|
expect(service.isLoggedIn()).toBe(false);
|
||||||
expect(service.getTicket(providers[0])).toBeUndefined();
|
expect(service.getTicket()).toBeUndefined();
|
||||||
done();
|
done();
|
||||||
}
|
});
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a ticket undefined after logout', (done) => {
|
it('should return a ticket undefined after logout', (done) => {
|
||||||
let providers = ['BPM'];
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPM);
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
service.login('fake-username', 'fake-password', this.providers).subscribe(() => {
|
||||||
localStorage.setItem('ticket-BPM', 'fake-post-ticket-BPM');
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseBPM);
|
||||||
service.createProviderInstance(providers);
|
|
||||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogout').and.returnValue(fakePromiseBPM);
|
|
||||||
|
|
||||||
service.logout()
|
service.logout().subscribe(() => {
|
||||||
.subscribe(() => {
|
expect(service.isLoggedIn()).toBe(false);
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(false);
|
expect(service.getTicket()).toBeUndefined();
|
||||||
expect(service.getTicket(providers[0])).toBeUndefined();
|
|
||||||
expect(localStorage.getItem('ticket-BPM')).toBeUndefined();
|
expect(localStorage.getItem('ticket-BPM')).toBeUndefined();
|
||||||
done();
|
done();
|
||||||
}
|
});
|
||||||
);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error when the logout return error', (done) => {
|
it('should return an error when the logout return error', (done) => {
|
||||||
let providers = ['BPM'];
|
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
|
||||||
localStorage.setItem('ticket-BPM', 'fake-post-ticket-BPM');
|
localStorage.setItem('ticket-BPM', 'fake-post-ticket-BPM');
|
||||||
service.createProviderInstance(providers);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(Promise.reject('fake logout error'));
|
||||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogout').and.returnValue(Promise.reject('fake logout error'));
|
|
||||||
|
|
||||||
service.logout()
|
service.logout().subscribe(
|
||||||
.subscribe(
|
|
||||||
(res) => {
|
(res) => {
|
||||||
done();
|
|
||||||
},
|
},
|
||||||
(err: any) => {
|
(err: any) => {
|
||||||
expect(err).toBeDefined();
|
expect(err).toBeDefined();
|
||||||
expect(err.message).toEqual('fake logout error');
|
|
||||||
expect(localStorage.getItem('ticket-BPM')).toEqual('fake-post-ticket-BPM');
|
expect(localStorage.getItem('ticket-BPM')).toEqual('fake-post-ticket-BPM');
|
||||||
done();
|
done();
|
||||||
}
|
});
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when the setting is both ECM and BPM ', () => {
|
describe('when the setting is both ECM and BPM ', () => {
|
||||||
|
|
||||||
it('should create both instances', (done) => {
|
beforeEach(() => {
|
||||||
let providers = ['ECM', 'BPM'];
|
this.providers = 'ALL';
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
service = injector.get(AlfrescoAuthenticationService);
|
||||||
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
|
});
|
||||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
|
|
||||||
|
|
||||||
service.login('fake-username', 'fake-password', providers)
|
it('should create both instances', (done) => {
|
||||||
.subscribe(() => {
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPMECM);
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(true);
|
|
||||||
expect(service.isLoggedIn(providers[1])).toBe(true);
|
service.login('fake-username', 'fake-password', this.providers).subscribe(() => {
|
||||||
expect(service.providersInstance).toBeDefined();
|
expect(service.isLoggedIn()).toBe(true);
|
||||||
expect(service.providersInstance.length).toBe(2);
|
expect(service.TYPE).toEqual(this.providers);
|
||||||
expect(service.providersInstance[0].TYPE).toEqual(providers[0]);
|
done();
|
||||||
expect(service.providersInstance[1].TYPE).toEqual(providers[1]);
|
});
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return both ECM and BPM tickets after the login done', (done) => {
|
it('should return both ECM and BPM tickets after the login done', (done) => {
|
||||||
let providers = ['ECM', 'BPM'];
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPMECM);
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
service.login('fake-username', 'fake-password', this.providers).subscribe(() => {
|
||||||
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
|
expect(service.isLoggedIn()).toBe(true);
|
||||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
|
expect(service.getTicket()).toEqual('fake-post-ticket-ECM,fake-post-ticket-BPM');
|
||||||
|
done();
|
||||||
let username = 'fake-username';
|
});
|
||||||
let password = 'fake-password';
|
|
||||||
let bpmToken = 'Basic ' + btoa(`${username}:${password}`);
|
|
||||||
|
|
||||||
service.login(username, password, providers)
|
|
||||||
.subscribe(() => {
|
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(true);
|
|
||||||
expect(service.isLoggedIn(providers[1])).toBe(true);
|
|
||||||
expect(service.getTicket(providers[0])).toEqual('fake-post-ticket-ECM');
|
|
||||||
expect(service.getTicket(providers[1])).toEqual(bpmToken);
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return ticket undefined when the credentials are correct for the ECM login but wrong for the BPM login', (done) => {
|
it('should return ticket undefined when the credentials are wrong', (done) => {
|
||||||
let providers = ['ECM', 'BPM'];
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(Promise.reject('fake invalid credentials'));
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
service.login('fake-username', 'fake-password', this.providers).subscribe(
|
||||||
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
|
|
||||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(Promise.reject('fake invalid credentials'));
|
|
||||||
|
|
||||||
service.login('fake-username', 'fake-password', providers)
|
|
||||||
.subscribe(
|
|
||||||
(res) => {
|
(res) => {
|
||||||
done();
|
|
||||||
},
|
},
|
||||||
(err: any) => {
|
(err: any) => {
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(false);
|
expect(service.isLoggedIn()).toBe(false);
|
||||||
expect(service.getTicket(providers[0])).toBeUndefined();
|
expect(service.getTicket()).toBeUndefined();
|
||||||
expect(service.isLoggedIn(providers[1])).toBe(false);
|
|
||||||
expect(service.getTicket(providers[1])).toBeUndefined();
|
|
||||||
done();
|
done();
|
||||||
}
|
});
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return ticket undefined when the credentials are correct for the BPM login but wrong for the ECM login', (done) => {
|
|
||||||
let providers = ['ECM', 'BPM'];
|
|
||||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
|
||||||
alfSetting.providers = providers;
|
|
||||||
|
|
||||||
service = injector.get(AlfrescoAuthenticationService);
|
|
||||||
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin')
|
|
||||||
.and.returnValue(Promise.reject('fake invalid credentials'));
|
|
||||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
|
|
||||||
|
|
||||||
service.login('fake-username', 'fake-password', providers)
|
|
||||||
.subscribe(
|
|
||||||
(res) => {
|
|
||||||
done();
|
|
||||||
},
|
|
||||||
(err: any) => {
|
|
||||||
expect(service.isLoggedIn(providers[0])).toBe(false);
|
|
||||||
expect(service.getTicket(providers[0])).toBeUndefined();
|
|
||||||
expect(service.isLoggedIn(providers[1])).toBe(false);
|
|
||||||
expect(service.getTicket(providers[1])).toBeUndefined();
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -15,13 +15,9 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Injectable } from '@angular/core';
|
import {Injectable} from '@angular/core';
|
||||||
import { Observable } from 'rxjs/Rx';
|
import {Observable} from 'rxjs/Rx';
|
||||||
import { Http } from '@angular/http';
|
import {AlfrescoSettingsService} from './AlfrescoSettingsService.service';
|
||||||
import { AlfrescoSettingsService } from './AlfrescoSettingsService.service';
|
|
||||||
import { AuthenticationFactory } from '../factory/AuthenticationFactory';
|
|
||||||
import { AbstractAuthentication } from '../interface/authentication.interface';
|
|
||||||
import { AlfrescoAuthenticationBase } from './AlfrescoAuthenticationBase.service';
|
|
||||||
|
|
||||||
declare let AlfrescoApi: any;
|
declare let AlfrescoApi: any;
|
||||||
|
|
||||||
@@ -29,177 +25,136 @@ declare let AlfrescoApi: any;
|
|||||||
* The AlfrescoAuthenticationService provide the login service and store the ticket in the localStorage
|
* The AlfrescoAuthenticationService provide the login service and store the ticket in the localStorage
|
||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AlfrescoAuthenticationService extends AlfrescoAuthenticationBase {
|
export class AlfrescoAuthenticationService {
|
||||||
|
|
||||||
private providersInstance: AbstractAuthentication[] = [];
|
alfrescoApi: any;
|
||||||
|
|
||||||
|
TYPE: string = 'ECM';
|
||||||
|
|
||||||
|
/**A
|
||||||
|
* Constructor
|
||||||
|
* @param alfrescoSetting
|
||||||
|
*/
|
||||||
|
constructor(public alfrescoSetting: AlfrescoSettingsService) {
|
||||||
|
|
||||||
|
if (!this.isLoggedIn()) {
|
||||||
|
this.alfrescoApi = new AlfrescoApi({
|
||||||
|
host: alfrescoSetting.ecmHost,
|
||||||
|
hostActiviti: alfrescoSetting.bpmHost
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.alfrescoApi = new AlfrescoApi({
|
||||||
|
ticket: this.getTicket(),
|
||||||
|
host: alfrescoSetting.ecmHost,
|
||||||
|
hostActiviti: alfrescoSetting.bpmHost
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* The method return tru if the user is logged in
|
||||||
* @param settingsService
|
* @returns {boolean}
|
||||||
* @param http
|
|
||||||
*/
|
*/
|
||||||
constructor(settingsService: AlfrescoSettingsService,
|
isLoggedIn(): boolean {
|
||||||
http: Http) {
|
return !!this.getTicket();
|
||||||
super(settingsService, http);
|
|
||||||
if (settingsService) {
|
|
||||||
this.createProviderInstance(settingsService.getProviders());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to delegate to POST login
|
* Method to delegate to POST login
|
||||||
* @param username
|
* @param username
|
||||||
* @param password
|
* @param password
|
||||||
* @param providers
|
* @param provider
|
||||||
* @returns {Observable<R>|Observable<T>}
|
* @returns {Observable<R>|Observable<T>}
|
||||||
*/
|
*/
|
||||||
login(username: string, password: string, providers: string []): Observable<string> {
|
login(username: string, password: string, provider: string) {
|
||||||
localStorage.clear();
|
|
||||||
if (providers.length === 0) {
|
this.TYPE = provider || this.TYPE;
|
||||||
return Observable.throw('No providers defined');
|
return Observable.fromPromise(this.callApiLogin(username, password, provider))
|
||||||
} else {
|
.map((response: any) => {
|
||||||
return this.performeLogin(username, password, providers);
|
this.saveTicket(response);
|
||||||
}
|
return {type: provider, ticket: response};
|
||||||
|
})
|
||||||
|
.catch(this.handleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform a login on behalf of the user for the different provider instance
|
* Initialize the alfresco Api with user and password end call the login method
|
||||||
*
|
|
||||||
* @param username
|
* @param username
|
||||||
* @param password
|
* @param password
|
||||||
* @param providers
|
* @param provider
|
||||||
* @returns {Observable<R>|Observable<T>}
|
* @returns {*|Observable<any>}
|
||||||
*/
|
*/
|
||||||
private performeLogin(username: string, password: string, providers: string []): Observable<any> {
|
private callApiLogin(username: string, password: string, provider: string) {
|
||||||
let observableBatch = [];
|
this.alfrescoApi = new AlfrescoApi({
|
||||||
providers.forEach((provider) => {
|
username: username,
|
||||||
let auth: AbstractAuthentication = this.findProviderInstance(provider);
|
password: password,
|
||||||
if (auth) {
|
host: this.alfrescoSetting.ecmHost,
|
||||||
observableBatch.push(auth.login(username, password));
|
hostActiviti: this.alfrescoSetting.bpmHost
|
||||||
} else {
|
|
||||||
observableBatch.push(Observable.throw('Wrong provider defined'));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return Observable.create(observer => {
|
|
||||||
Observable.forkJoin(observableBatch).subscribe(
|
|
||||||
(response: any[]) => {
|
|
||||||
response.forEach((res) => {
|
|
||||||
this.performeSaveTicket(res.type, res.ticket);
|
|
||||||
});
|
|
||||||
observer.next(response);
|
|
||||||
},
|
|
||||||
(err: any) => {
|
|
||||||
observer.error(new Error(err));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return this.alfrescoApi.login();
|
||||||
* The method return true if the user is logged in
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
isLoggedIn(type: string = 'ECM'): boolean {
|
|
||||||
let auth: AbstractAuthentication = this.findProviderInstance(type);
|
|
||||||
if (auth) {
|
|
||||||
return auth.isLoggedIn();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
getAlfrescoApi(): any {
|
|
||||||
return this.findProviderInstance('ECM').alfrescoApi;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the ticket stored in the localStorage of the specific provider type
|
|
||||||
* @param type
|
|
||||||
*/
|
|
||||||
public getTicket(type: string = 'ECM'): string {
|
|
||||||
let auth: AbstractAuthentication = this.findProviderInstance(type);
|
|
||||||
if (auth) {
|
|
||||||
return auth.getTicket();
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Save the token calling the method of the specific provider type
|
|
||||||
* @param type - providerName
|
|
||||||
* @param ticket
|
|
||||||
*/
|
|
||||||
private performeSaveTicket(type: string, ticket: string) {
|
|
||||||
let auth: AbstractAuthentication = this.findProviderInstance(type);
|
|
||||||
if (auth) {
|
|
||||||
auth.saveTicket(ticket);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The method remove the ticket from the local storage
|
* The method remove the ticket from the local storage
|
||||||
* @returns {Observable<T>}
|
|
||||||
*/
|
|
||||||
public logout(): Observable<string> {
|
|
||||||
if (this.providersInstance.length === 0) {
|
|
||||||
return Observable.throw('No providers defined');
|
|
||||||
} else {
|
|
||||||
return this.performLogout();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a logout on behalf of the user for the different provider instance
|
|
||||||
*
|
*
|
||||||
* @returns {Observable<R>|Observable<T>}
|
* @returns {Observable<R>|Observable<T>}
|
||||||
*/
|
*/
|
||||||
private performLogout(): Observable<any> {
|
public logout() {
|
||||||
let observableBatch = [];
|
return Observable.fromPromise(this.callApiLogout())
|
||||||
this.providersInstance.forEach((authInstance) => {
|
.map(res => <any> res)
|
||||||
if (authInstance.isLoggedIn()) {
|
.do(response => {
|
||||||
observableBatch.push(authInstance.logout());
|
this.removeTicket();
|
||||||
}
|
return response;
|
||||||
});
|
})
|
||||||
return Observable.create(observer => {
|
.catch(this.handleError);
|
||||||
Observable.forkJoin(observableBatch).subscribe(
|
|
||||||
(response: any[]) => {
|
|
||||||
observer.next(response);
|
|
||||||
},
|
|
||||||
(err: any) => {
|
|
||||||
observer.error(new Error(err));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the provider instance using a Factory
|
* Remove the login ticket from localStorage
|
||||||
* @param providers - list of the providers like ECM BPM
|
|
||||||
*/
|
*/
|
||||||
public createProviderInstance(providers: string []): void {
|
public removeTicket(): void {
|
||||||
if (this.providersInstance.length === 0) {
|
localStorage.removeItem(`ticket-${this.TYPE}`);
|
||||||
providers.forEach((provider) => {
|
}
|
||||||
let authInstance: AbstractAuthentication = AuthenticationFactory.createAuth(
|
|
||||||
this.alfrescoSetting, this.http, provider);
|
/**
|
||||||
if (authInstance) {
|
*
|
||||||
this.providersInstance.push(authInstance);
|
* @returns {*|Observable<string>|Observable<any>|Promise<T>}
|
||||||
}
|
*/
|
||||||
});
|
private callApiLogout(): Promise<any> {
|
||||||
|
return this.alfrescoApi.logout();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The method return the ticket stored in the localStorage
|
||||||
|
* @returns ticket
|
||||||
|
*/
|
||||||
|
public getTicket(): string {
|
||||||
|
return localStorage.getItem(`ticket-${this.TYPE}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The method save the ticket in the localStorage
|
||||||
|
* @param ticket
|
||||||
|
*/
|
||||||
|
public saveTicket(ticket): void {
|
||||||
|
if (ticket) {
|
||||||
|
localStorage.setItem(`ticket-${this.TYPE}`, ticket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the provider by type and return it
|
* The method write the error in the console browser
|
||||||
* @param type
|
* @param error
|
||||||
* @returns {AbstractAuthentication}
|
* @returns {ErrorObservable}
|
||||||
*/
|
*/
|
||||||
private findProviderInstance(type: string): AbstractAuthentication {
|
public handleError(error: any): Observable<any> {
|
||||||
let auth: AbstractAuthentication = null;
|
console.error('Error when logging in', error);
|
||||||
if (this.providersInstance && this.providersInstance.length !== 0) {
|
return Observable.throw(error || 'Server error');
|
||||||
this.providersInstance.forEach((provider) => {
|
|
||||||
if (provider.TYPE === type.toUpperCase()) {
|
|
||||||
auth = provider;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return auth;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getAlfrescoApi(): any {
|
||||||
|
return this.alfrescoApi;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -83,7 +83,7 @@ Also make sure you include these dependencies in your .html page:
|
|||||||
## Basic usage
|
## Basic usage
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<alfresco-login [providers]="['ECM','BPM']"></alfresco-login>
|
<alfresco-login [providers]="'ALL'"></alfresco-login>
|
||||||
```
|
```
|
||||||
|
|
||||||
Example of an App that use Alfresco login component :
|
Example of an App that use Alfresco login component :
|
||||||
@@ -105,7 +105,7 @@ import {
|
|||||||
selector: 'my-app',
|
selector: 'my-app',
|
||||||
template: '
|
template: '
|
||||||
<alfresco-login
|
<alfresco-login
|
||||||
providers=['ECM']
|
providers="'ALL'"
|
||||||
(onSuccess)="mySuccessMethod($event)"
|
(onSuccess)="mySuccessMethod($event)"
|
||||||
(onError)="myErrorMethod($event)">
|
(onError)="myErrorMethod($event)">
|
||||||
</alfresco-login>',
|
</alfresco-login>',
|
||||||
@@ -141,18 +141,17 @@ bootstrap(AppComponent, [
|
|||||||
| onSuccess | The event is emitted when the login is done |
|
| onSuccess | The event is emitted when the login is done |
|
||||||
| onError | The event is emitted when the login fails |
|
| onError | The event is emitted when the login fails |
|
||||||
|
|
||||||
|
Attribute | Description |
|
||||||
|
--- | --- |
|
||||||
|
`onSuccess` | The event is emitted when the login is done |
|
||||||
|
`onError` | The event is emitted when the login fails |
|
||||||
|
|
||||||
#### Options
|
#### Options
|
||||||
|
|
||||||
**providers**: { string[] } optional) default ECM.
|
Attribute | Options | Default | Description | Mandatory
|
||||||
|
--- | --- | --- | --- | ---
|
||||||
|
`providers` | *string* | ECM | Possible valid value are ECM, BPM or ALL. The default behaviour of this component will logged in only in the ECM . If you want log in in both system the correct value to use is ALL |
|
||||||
|
|
||||||
Using the providers attribute, you can specify in which system
|
|
||||||
(ECM or BPM) you want to be logged in.
|
|
||||||
By selecting one of the options only the relative components will be
|
|
||||||
accesible. For instance if you activate the ECM login then only the
|
|
||||||
ECM component will be visible,same behaviour for BPM selection.
|
|
||||||
You can also specify ECM and BPM, in this case both system components
|
|
||||||
are accessible.<br />
|
|
||||||
|
|
||||||
|
|
||||||
## Custom logo and background
|
## Custom logo and background
|
||||||
|
|
||||||
|
@@ -11,7 +11,7 @@
|
|||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the speific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@@ -48,7 +48,7 @@ export class AlfrescoLoginComponent {
|
|||||||
backgroundImageUrl: string;
|
backgroundImageUrl: string;
|
||||||
|
|
||||||
@Input()
|
@Input()
|
||||||
providers: string [] ;
|
providers: string ;
|
||||||
|
|
||||||
@Output()
|
@Output()
|
||||||
onSuccess = new EventEmitter();
|
onSuccess = new EventEmitter();
|
||||||
|
Reference in New Issue
Block a user