integrate new login bpm modify and change token to ticket

This commit is contained in:
Eugenio Romano 2016-07-11 16:17:20 +01:00
parent e57ff04e4f
commit 8c84bc9be5
7 changed files with 107 additions and 94 deletions

View File

@ -26,8 +26,7 @@ export interface AbstractAuthentication {
isLoggedIn(): boolean ; isLoggedIn(): boolean ;
getToken(): string; getTicket(): string;
saveToken(): void;
saveTicket(ticket: any): void;
} }

View File

@ -24,7 +24,6 @@ import { AlfrescoSettingsService } from './AlfrescoSettingsService.service';
export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implements AbstractAuthentication { export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implements AbstractAuthentication {
TYPE: string = 'BPM'; TYPE: string = 'BPM';
private token: string;
constructor(private alfrescoSettingsService: AlfrescoSettingsService, constructor(private alfrescoSettingsService: AlfrescoSettingsService,
private http: Http) { private http: Http) {
@ -41,9 +40,8 @@ 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.token = response.status; this.saveTicket(response.status);
return this.token; return response.status;
// return {name: this.TYPE, token: response.status};
}) })
.catch(this.handleError); .catch(this.handleError);
} }
@ -57,7 +55,7 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem
return Observable.fromPromise(this.apiActivitiLogout()) return Observable.fromPromise(this.apiActivitiLogout())
.map(res => <any> res) .map(res => <any> res)
.do(response => { .do(response => {
this.removeToken(this.TYPE); this.removeTicket(this.TYPE);
}) })
.catch(this.handleError); .catch(this.handleError);
} }
@ -67,7 +65,7 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem
* @returns {boolean} * @returns {boolean}
*/ */
isLoggedIn(): boolean { isLoggedIn(): boolean {
return !!this.getToken(); return !!this.getTicket();
} }
private apiActivitiLogin(username: string, password: string) { private apiActivitiLogin(username: string, password: string) {
@ -91,17 +89,17 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem
return this.http.get(url).toPromise(); return this.http.get(url).toPromise();
} }
public getToken (): string { public getTicket(): string {
return localStorage.getItem(`token-${this.TYPE}`); return localStorage.getItem(`ticket-${this.TYPE}`);
} }
/** /**
* The method save the toke in the localStorage * The method save the ticket in the localStorage
* @param token * @param ticket
*/ */
public saveToken(): void { public saveTicket(ticket): void {
if (this.token) { if (ticket) {
super.saveToken(this.TYPE, this.token); super.saveTicket(this.TYPE, ticket);
} }
} }

View File

@ -40,19 +40,19 @@ export class AlfrescoAuthenticationBase {
/** /**
* The method save the toke in the localStorage * The method save the toke in the localStorage
* @param token * @param ticket
*/ */
public saveToken(provider:string, token: string): void { public saveTicket(provider:string, ticket: string): void {
if (token) { if (ticket) {
localStorage.setItem(`token-${provider}`, token); localStorage.setItem(`ticket-${provider}`, ticket);
} }
} }
/** /**
* Remove the login token from localStorage * Remove the login ticket from localStorage
*/ */
public removeToken(provider:string): void { public removeTicket(provider:string): void {
localStorage.removeItem(`token-${provider}`); localStorage.removeItem(`ticket-${provider}`);
} }
/** /**

View File

@ -26,84 +26,97 @@ declare let AlfrescoApi: any;
export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implements AbstractAuthentication { export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implements AbstractAuthentication {
TYPE: string = 'ECM'; TYPE: string = 'ECM';
private token: string;
alfrescoApi: any;
/**
* Constructor
* @param alfrescoSettingsService
*/
constructor(private alfrescoSettingsService: AlfrescoSettingsService, constructor(private alfrescoSettingsService: AlfrescoSettingsService,
private http: Http) { private http: Http) {
super(alfrescoSettingsService, 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 username
* @param password * @param password
* @returns {Observable<R>|Observable<T>} * @returns {Observable<R>|Observable<T>}
*/ */
login(username: string, password: string): Observable<any> { login(username: string, password: string) {
return Observable.fromPromise(this.getCreateTicketPromise(username, password)) this.alfrescoApi = new AlfrescoApi({
.map((response: any) => { username: username,
this.token = response.entry.id; password: password,
return this.token; host: this.getBaseUrl()
// return {name: this.TYPE, token: response.entry.id}; });
return Observable.fromPromise(this.alfrescoApi.login())
.map(res => <any> res)
.do(response => {
this.saveTicket(response);
return response;
}) })
.catch(this.handleError); .catch(this.handleError);
} }
/** /**
* Delete the current login ticket from the server * The method remove the ticket from the local storage
* *
* @returns {Observable<R>|Observable<T>} * @returns {Observable<R>|Observable<T>}
*/ */
logout() { public logout() {
return Observable.fromPromise(this.getDeleteTicketPromise()) return Observable.fromPromise(this.alfrescoApi.logout())
.map(res => <any> res) .map(res => <any> res)
.do(response => { .do(response => {
this.removeToken(this.TYPE); this.removeTicket(this.TYPE);
return response;
}) })
.catch(this.handleError); .catch(this.handleError);
} }
/** /**
* The method return true if the user is logged in * The method return the ticket stored in the localStorage
* @returns {boolean} * @returns ticket
*/ */
isLoggedIn(): boolean { public getTicket(): string {
return !!this.getToken(); return localStorage.getItem(`ticket-${this.TYPE}`);
}
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();
} }
/** /**
* The method return the token stored in the localStorage * The method save the ticket in the localStorage
* @param token * @param ticket
*/ */
public getToken (): string { public saveTicket(ticket): void {
return localStorage.getItem(`token-${this.TYPE}`); if (ticket) {
} super.saveTicket(this.TYPE, ticket);
/**
* The method save the toke in the localStorage
* @param token
*/
public saveToken(): void {
if (this.token) {
super.saveToken(this.TYPE, this.token);
} }
} }

View File

@ -35,7 +35,7 @@ describe('AlfrescoContentService', () => {
AlfrescoSettingsService AlfrescoSettingsService
]); ]);
spyOn(localStorage, 'getItem').and.callFake(function (key) { spyOn(localStorage, 'getItem').and.callFake(function (key) {
return 'myToken'; return 'myTicket';
}); });
service = injector.get(AlfrescoContentService); service = injector.get(AlfrescoContentService);
authService = injector.get(AlfrescoAuthenticationService); authService = injector.get(AlfrescoAuthenticationService);
@ -49,7 +49,7 @@ describe('AlfrescoContentService', () => {
})).toBe( })).toBe(
AlfrescoSettingsService.DEFAULT_HOST_ADDRESS + AlfrescoSettingsService.DEFAULT_CONTEXT_PATH + AlfrescoSettingsService.DEFAULT_HOST_ADDRESS + AlfrescoSettingsService.DEFAULT_CONTEXT_PATH +
AlfrescoSettingsService.DEFAULT_BASE_API_PATH + '/nodes/' + nodeId + '/content' + 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( })).toBe(
AlfrescoSettingsService.DEFAULT_HOST_ADDRESS + AlfrescoSettingsService.DEFAULT_CONTEXT_PATH + AlfrescoSettingsService.DEFAULT_HOST_ADDRESS + AlfrescoSettingsService.DEFAULT_CONTEXT_PATH +
AlfrescoSettingsService.DEFAULT_BASE_API_PATH + '/nodes/' + nodeId + '/renditions/doclib/content' + AlfrescoSettingsService.DEFAULT_BASE_API_PATH + '/nodes/' + nodeId + '/renditions/doclib/content' +
'?attachment=false&alf_ticket=' + authService.getToken() '?attachment=false&alf_ticket=' + authService.getTicket()
); );
}); });
}); });

View File

@ -28,18 +28,21 @@ import {
@Component({ @Component({
selector: 'my-app', selector: 'my-app',
template: `<label for="token"><b>Insert the ip of your Alfresco instance:</b></label><br> template: `
<label for="token"><b>Insert the ip of your Alfresco instance:</b></label><br>
<input id="token" type="text" size="48" (change)="updateHost()" [(ngModel)]="host"><br><br> <input id="token" type="text" size="48" (change)="updateHost()" [(ngModel)]="host"><br><br>
<div style="border-radius: 8px; position: absolute; background-color: papayawhip; color: cadetblue; left: 10px; top: 120px; z-index: 1;"> <div style="border-radius: 8px; position: absolute; background-color: papayawhip; color: cadetblue; left: 10px; top: 120px; z-index: 1;">
<p style="width:120px;margin: 20px;"> <p style="width:120px;margin: 20px;">
<label for="switch1" class="mdl-switch mdl-js-switch mdl-js-ripple-effect"> <label for="switch1" class="mdl-switch mdl-js-switch mdl-js-ripple-effect">
<input type="checkbox" id="switch1" class="mdl-switch__input" checked (click)="toggleECM(ecm.checked)" #ecm> <input type="checkbox" id="switch1" class="mdl-switch__input" checked
(click)="toggleECM(ecm.checked)" #ecm>
<span class="mdl-switch__label">ECM</span> <span class="mdl-switch__label">ECM</span>
</label> </label>
</p> </p>
<p style="width:120px;margin: 20px;"> <p style="width:120px;margin: 20px;">
<label for="switch2" class="mdl-switch mdl-js-switch mdl-js-ripple-effect"> <label for="switch2" class="mdl-switch mdl-js-switch mdl-js-ripple-effect">
<input type="checkbox" id="switch2" class="mdl-switch__input" (click)="toggleBPM(bpm.checked)" #bpm> <input type="checkbox" id="switch2" class="mdl-switch__input"
(click)="toggleBPM(bpm.checked)" #bpm>
<span class="mdl-switch__label">BPM</span> <span class="mdl-switch__label">BPM</span>
</label> </label>
</p> </p>

View File

@ -26,7 +26,7 @@
"label-undefined": true, "label-undefined": true,
"max-line-length": [ "max-line-length": [
true, true,
140 180
], ],
"member-ordering": [ "member-ordering": [
true, true,