diff --git a/ng2-components/ng2-alfresco-core/src/interface/authentication.interface.ts b/ng2-components/ng2-alfresco-core/src/interface/authentication.interface.ts index 582e82bb56..8d274a1b56 100644 --- a/ng2-components/ng2-alfresco-core/src/interface/authentication.interface.ts +++ b/ng2-components/ng2-alfresco-core/src/interface/authentication.interface.ts @@ -26,8 +26,7 @@ export interface AbstractAuthentication { isLoggedIn(): boolean ; - getToken(): string; - - saveToken(): void; + getTicket(): string; + saveTicket(ticket: any): void; } diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBPM.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBPM.service.ts index b1ea5f0ac4..f379ad71fd 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBPM.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBPM.service.ts @@ -24,7 +24,6 @@ import { AlfrescoSettingsService } from './AlfrescoSettingsService.service'; export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implements AbstractAuthentication { TYPE: string = 'BPM'; - private token: string; constructor(private alfrescoSettingsService: AlfrescoSettingsService, private http: Http) { @@ -41,9 +40,8 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem login(username: string, password: string): Observable { return Observable.fromPromise(this.apiActivitiLogin(username, password)) .map((response: any) => { - this.token = response.status; - return this.token; - // return {name: this.TYPE, token: response.status}; + this.saveTicket(response.status); + return response.status; }) .catch(this.handleError); } @@ -57,7 +55,7 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem return Observable.fromPromise(this.apiActivitiLogout()) .map(res => res) .do(response => { - this.removeToken(this.TYPE); + this.removeTicket(this.TYPE); }) .catch(this.handleError); } @@ -67,7 +65,7 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem * @returns {boolean} */ isLoggedIn(): boolean { - return !!this.getToken(); + return !!this.getTicket(); } private apiActivitiLogin(username: string, password: string) { @@ -91,17 +89,17 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem return this.http.get(url).toPromise(); } - public getToken (): string { - return localStorage.getItem(`token-${this.TYPE}`); + public getTicket(): string { + return localStorage.getItem(`ticket-${this.TYPE}`); } /** - * The method save the toke in the localStorage - * @param token + * The method save the ticket in the localStorage + * @param ticket */ - public saveToken(): void { - if (this.token) { - super.saveToken(this.TYPE, this.token); + public saveTicket(ticket): void { + if (ticket) { + super.saveTicket(this.TYPE, ticket); } } diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBase.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBase.service.ts index ddf41b4799..ef278a3127 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBase.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBase.service.ts @@ -40,19 +40,19 @@ export class AlfrescoAuthenticationBase { /** * The method save the toke in the localStorage - * @param token + * @param ticket */ - public saveToken(provider:string, token: string): void { - if (token) { - localStorage.setItem(`token-${provider}`, token); + public saveTicket(provider:string, ticket: string): void { + if (ticket) { + localStorage.setItem(`ticket-${provider}`, ticket); } } /** - * Remove the login token from localStorage + * Remove the login ticket from localStorage */ - public removeToken(provider:string): void { - localStorage.removeItem(`token-${provider}`); + public removeTicket(provider:string): void { + localStorage.removeItem(`ticket-${provider}`); } /** diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationECM.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationECM.service.ts index af83523b21..e3a0cca119 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationECM.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationECM.service.ts @@ -26,84 +26,97 @@ declare let AlfrescoApi: any; export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implements AbstractAuthentication { TYPE: string = 'ECM'; - private token: string; + alfrescoApi: any; + /** + * Constructor + * @param alfrescoSettingsService + */ constructor(private alfrescoSettingsService: AlfrescoSettingsService, private http: Http) { super(alfrescoSettingsService, http); + + if (!this.isLoggedIn) { + this.alfrescoApi = new AlfrescoApi({ + host: this.getBaseUrl() + }); + } else { + this.alfrescoApi = new AlfrescoApi({ + ticket: this.getTicket(), + host: this.getBaseUrl() + }); + } + } + + getBaseUrl(): string { + return this.alfrescoSettingsService.host; + } + + getAlfrescoApi(): any { + return this.alfrescoApi; } /** - * Perform a login on behalf of the user and store the ticket returned - * + * 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|Observable} */ - login(username: string, password: string): Observable { - return Observable.fromPromise(this.getCreateTicketPromise(username, password)) - .map((response: any) => { - this.token = response.entry.id; - return this.token; - // return {name: this.TYPE, token: response.entry.id}; + login(username: string, password: string) { + this.alfrescoApi = new AlfrescoApi({ + username: username, + password: password, + host: this.getBaseUrl() + }); + + return Observable.fromPromise(this.alfrescoApi.login()) + .map(res => res) + .do(response => { + this.saveTicket(response); + return response; }) .catch(this.handleError); } /** - * Delete the current login ticket from the server + * The method remove the ticket from the local storage * * @returns {Observable|Observable} */ - logout() { - return Observable.fromPromise(this.getDeleteTicketPromise()) + public logout() { + return Observable.fromPromise(this.alfrescoApi.logout()) .map(res => res) .do(response => { - this.removeToken(this.TYPE); + this.removeTicket(this.TYPE); + return response; }) .catch(this.handleError); } + /** - * The method return true if the user is logged in - * @returns {boolean} + * The method return the ticket stored in the localStorage + * @returns ticket */ - isLoggedIn(): boolean { - return !!this.getToken(); - } - - private getAlfrescoClient() { - return AlfrescoApi.getClientWithTicket(this.getBaseUrl(), this.getToken()); - } - - private getCreateTicketPromise(username: string, password: string) { - let apiInstance = new AlfrescoApi.Auth.AuthenticationApi(this.getAlfrescoClient()); - let loginRequest = new AlfrescoApi.Auth.LoginRequest(); - loginRequest.userId = username; - loginRequest.password = password; - return apiInstance.createTicket(loginRequest); - } - - private getDeleteTicketPromise() { - let apiInstance = new AlfrescoApi.Auth.AuthenticationApi(this.getAlfrescoClient()); - return apiInstance.deleteTicket(); + public getTicket(): string { + return localStorage.getItem(`ticket-${this.TYPE}`); } /** - * The method return the token stored in the localStorage - * @param token + * The method save the ticket in the localStorage + * @param ticket */ - public getToken (): string { - return localStorage.getItem(`token-${this.TYPE}`); - } - - /** - * The method save the toke in the localStorage - * @param token - */ - public saveToken(): void { - if (this.token) { - super.saveToken(this.TYPE, this.token); + public saveTicket(ticket): void { + if (ticket) { + super.saveTicket(this.TYPE, ticket); } } diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoContentService.spec.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoContentService.spec.ts index 760ed8be9e..1d704db753 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoContentService.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoContentService.spec.ts @@ -35,7 +35,7 @@ describe('AlfrescoContentService', () => { AlfrescoSettingsService ]); spyOn(localStorage, 'getItem').and.callFake(function (key) { - return 'myToken'; + return 'myTicket'; }); service = injector.get(AlfrescoContentService); authService = injector.get(AlfrescoAuthenticationService); @@ -49,7 +49,7 @@ describe('AlfrescoContentService', () => { })).toBe( AlfrescoSettingsService.DEFAULT_HOST_ADDRESS + AlfrescoSettingsService.DEFAULT_CONTEXT_PATH + AlfrescoSettingsService.DEFAULT_BASE_API_PATH + '/nodes/' + nodeId + '/content' + - '?attachment=false&alf_ticket=' + authService.getToken() + '?attachment=false&alf_ticket=' + authService.getTicket() ); }); @@ -61,7 +61,7 @@ describe('AlfrescoContentService', () => { })).toBe( AlfrescoSettingsService.DEFAULT_HOST_ADDRESS + AlfrescoSettingsService.DEFAULT_CONTEXT_PATH + AlfrescoSettingsService.DEFAULT_BASE_API_PATH + '/nodes/' + nodeId + '/renditions/doclib/content' + - '?attachment=false&alf_ticket=' + authService.getToken() + '?attachment=false&alf_ticket=' + authService.getTicket() ); }); }); diff --git a/ng2-components/ng2-alfresco-login/demo/src/main.ts b/ng2-components/ng2-alfresco-login/demo/src/main.ts index 008231c276..875220271a 100644 --- a/ng2-components/ng2-alfresco-login/demo/src/main.ts +++ b/ng2-components/ng2-alfresco-login/demo/src/main.ts @@ -28,27 +28,30 @@ import { @Component({ selector: 'my-app', - template: `
-

-
-

- -

-

- -

-
- {{ status }} -
+ template: ` +
+

+
+

+ +

+

+ +

+
+ {{ status }} +
- `, + `, directives: [AlfrescoLoginComponent] }) export class AppComponent { diff --git a/ng2-components/ng2-alfresco-login/demo/tslint.json b/ng2-components/ng2-alfresco-login/demo/tslint.json index 8c48e76469..e550ac11d4 100644 --- a/ng2-components/ng2-alfresco-login/demo/tslint.json +++ b/ng2-components/ng2-alfresco-login/demo/tslint.json @@ -26,7 +26,7 @@ "label-undefined": true, "max-line-length": [ true, - 140 + 180 ], "member-ordering": [ true,