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

View File

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

View File

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

View File

@@ -81,6 +81,9 @@ export class AlfrescoAuthenticationService extends AlfrescoAuthenticationBase {
return Observable.create(observer => { return Observable.create(observer => {
Observable.forkJoin(observableBatch).subscribe( Observable.forkJoin(observableBatch).subscribe(
(response: any[]) => { (response: any[]) => {
response.forEach((res) => {
this.performeSaveToken(res.type, res.ticket);
});
observer.next(response); observer.next(response);
}, },
(err: any) => { (err: any) => {
@@ -117,6 +120,18 @@ export class AlfrescoAuthenticationService extends AlfrescoAuthenticationBase {
return ''; 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 * The method remove the ticket from the local storage
* @returns {Observable<T>} * @returns {Observable<T>}

View File

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