Merge branch 'development' into dev-denys-440

This commit is contained in:
Denys Vuika
2016-07-18 14:29:04 +01:00
5 changed files with 59 additions and 31 deletions

View File

@@ -40,8 +40,7 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem
login(username: string, password: string): Observable<any> {
return Observable.fromPromise(this.apiActivitiLogin(username, password))
.map((response: any) => {
this.saveTicket(response.status);
return response.status;
return {type: this.TYPE, ticket: response.status};
})
.catch(this.handleError);
}
@@ -97,7 +96,7 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem
* The method save the ticket in the localStorage
* @param ticket
*/
public saveTicket(ticket): void {
public saveTicket(ticket: string): void {
if (ticket) {
super.saveTicket(this.TYPE, ticket);
}

View File

@@ -54,10 +54,6 @@ export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implem
return this.alfrescoSettingsService.host;
}
getAlfrescoApi(): any {
return this.alfrescoApi;
}
/**
* The method return tru if the user is logged in
* @returns {boolean}
@@ -73,19 +69,27 @@ export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implem
* @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.getBaseUrl()
});
return Observable.fromPromise(this.alfrescoApi.login())
.map(res => <any> res)
.do(response => {
this.saveTicket(response);
return response;
})
.catch(this.handleError);
return this.alfrescoApi.login();
}
/**
@@ -94,7 +98,7 @@ export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implem
* @returns {Observable<R>|Observable<T>}
*/
public logout() {
return Observable.fromPromise(this.alfrescoApi.logout())
return Observable.fromPromise(this.callApiLogout())
.map(res => <any> res)
.do(response => {
this.removeTicket(this.TYPE);
@@ -103,6 +107,14 @@ export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implem
.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

View File

@@ -33,12 +33,9 @@ describe('AlfrescoAuthentication', () => {
service;
fakePromiseECM = new Promise(function (resolve, reject) {
resolve({
entry: {
userId: 'fake-username',
id: 'fake-post-ticket-ECM'
}
});
resolve(
'fake-post-ticket-ECM'
);
reject({
response: {
error: 'fake-error'
@@ -96,7 +93,7 @@ describe('AlfrescoAuthentication', () => {
alfSetting.providers = providers;
service = injector.get(AlfrescoAuthenticationService);
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM);
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
service.login('fake-username', 'fake-password', providers)
.subscribe(() => {
@@ -115,7 +112,7 @@ describe('AlfrescoAuthentication', () => {
alfSetting.providers = providers;
service = injector.get(AlfrescoAuthenticationService);
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM);
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
service.login('fake-username', 'fake-password', providers)
.subscribe(() => {
@@ -132,7 +129,7 @@ describe('AlfrescoAuthentication', () => {
alfSetting.providers = providers;
service = injector.get(AlfrescoAuthenticationService);
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise')
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin')
.and.returnValue(Promise.reject('fake invalid credentials'));
service.login('fake-wrong-username', 'fake-wrong-password', providers)
@@ -194,7 +191,7 @@ describe('AlfrescoAuthentication', () => {
service = injector.get(AlfrescoAuthenticationService);
localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM');
service.createProviderInstance(providers);
spyOn(AlfrescoAuthenticationECM.prototype, 'getDeleteTicketPromise').and.returnValue(fakePromiseECM);
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogout').and.returnValue(fakePromiseECM);
service.logout()
.subscribe(() => {
@@ -345,7 +342,7 @@ describe('AlfrescoAuthentication', () => {
alfSetting.providers = providers;
service = injector.get(AlfrescoAuthenticationService);
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM);
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
service.login('fake-username', 'fake-password', providers)
@@ -367,7 +364,7 @@ describe('AlfrescoAuthentication', () => {
alfSetting.providers = providers;
service = injector.get(AlfrescoAuthenticationService);
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM);
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
service.login('fake-username', 'fake-password', providers)
@@ -387,7 +384,7 @@ describe('AlfrescoAuthentication', () => {
alfSetting.providers = providers;
service = injector.get(AlfrescoAuthenticationService);
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM);
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)
@@ -411,7 +408,7 @@ describe('AlfrescoAuthentication', () => {
alfSetting.providers = providers;
service = injector.get(AlfrescoAuthenticationService);
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise')
spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin')
.and.returnValue(Promise.reject('fake invalid credentials'));
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);

View File

@@ -81,6 +81,9 @@ export class AlfrescoAuthenticationService extends AlfrescoAuthenticationBase {
return Observable.create(observer => {
Observable.forkJoin(observableBatch).subscribe(
(response: any[]) => {
response.forEach((res) => {
this.performeSaveToken(res.type, res.ticket);
});
observer.next(response);
},
(err: any) => {
@@ -117,6 +120,18 @@ export class AlfrescoAuthenticationService extends AlfrescoAuthenticationBase {
return '';
}
/**
* Save the token calling the method of the specific provider type
* @param type - providerName
* @param ticket
*/
private performeSaveToken(type: string, ticket: string) {
let auth: AbstractAuthentication = this.findProviderInstance(type);
if (auth) {
auth.saveTicket(ticket);
}
}
/**
* The method remove the ticket from the local storage
* @returns {Observable<T>}

View File

@@ -79,6 +79,10 @@ Also make sure you include these dependencies in your `index.html` file:
```ts
import { Component } from '@angular/core';
import {
CONTEXT_MENU_DIRECTIVES,
CONTEXT_MENU_PROVIDERS
} from 'ng2-alfresco-core';
import {
ALFRESCO_DATATABLE_DIRECTIVES,
ObjectDataTableAdapter
@@ -88,7 +92,8 @@ import {
selector: 'my-view',
template: `<alfresco-datatable [data]="data">
</alfresco-datatable>`,
directives: [ALFRESCO_DATATABLE_DIRECTIVES]
directives: [ALFRESCO_DATATABLE_DIRECTIVES, CONTEXT_MENU_DIRECTIVES],
providers: [CONTEXT_MENU_PROVIDERS]
})
export class MyView {
data: ObjectDataTableAdapter;