Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

[aca-4419] add new event once user is logged in #1105

Merged
merged 10 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/test-performance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd $DIR/..

./scripts/test-build.sh

#Test Angular build size
cd $DIR/../test/performance/test-angular
npm install
Expand Down
47 changes: 42 additions & 5 deletions src/alfrescoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class AlfrescoApi implements Emitter {
on: EmitterMethod;
off: EmitterMethod;
once: EmitterMethod;

bufferEvents: string[] = [];

emit: (type: string, ...args: any[]) => void;

username: string;
Expand Down Expand Up @@ -69,7 +72,16 @@ export class AlfrescoApi implements Emitter {
this.processClient = new ProcessClient(this.config);

this.errorListeners();
this.initAuth(config);

if(this.isLoggedIn()){
this.emitBuffer('logged-in');
}

return config;
}

private initAuth(config: AlfrescoApiConfig): void {
if (this.isOauthConfiguration()) {

if (!this.oauth2Auth) {
Expand All @@ -78,6 +90,10 @@ export class AlfrescoApi implements Emitter {
this.oauth2Auth.setConfig(this.config, this);
}

this.oauth2Auth?.on('logged-in', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By this time, can't we be sure, that we have this.oauth2Auth?

this.emitBuffer('logged-in');
});

this.setAuthenticationClientECMBPM(this.oauth2Auth.getAuthentication(), this.oauth2Auth.getAuthentication());
} else {

Expand All @@ -87,16 +103,23 @@ export class AlfrescoApi implements Emitter {
this.processAuth.setConfig(this.config);
}

this.processAuth?.on('logged-in', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By this time, can't we be sure, that we have this.processAuth?

this.emitBuffer('logged-in');
});

if (!this.contentAuth) {
this.contentAuth = new ContentAuth(this.config, this);
} else {
this.contentAuth.setConfig(config);
}

this.contentAuth?.on('logged-in', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By this time, can't we be sure, that we have this.contentAuth?

this.emitBuffer('logged-in');
});

this.setAuthenticationClientECMBPM(this.contentAuth.getAuthentication(), this.processAuth.getAuthentication());
}

return config;
}

private clientsFactory() {
Expand Down Expand Up @@ -144,6 +167,7 @@ export class AlfrescoApi implements Emitter {
}

errorListeners() {

this.contentClient.off('error', () => {
});

Expand Down Expand Up @@ -199,7 +223,7 @@ export class AlfrescoApi implements Emitter {
this.invalidateSession();
}

this.emit('error', error);
this.emitBuffer('error', error);
}

changeWithCredentialsConfig(withCredentials: boolean) {
Expand Down Expand Up @@ -251,7 +275,7 @@ export class AlfrescoApi implements Emitter {

oauth2AuthPromise.then((accessToken) => {
this.config.accessToken = accessToken;
}, (e) => {
}, (e) => {
console.log('login OAUTH error', e);
});

Expand All @@ -264,7 +288,7 @@ export class AlfrescoApi implements Emitter {

processPromise.then((ticketBpm) => {
this.config.ticketBpm = ticketBpm;
}, () => {
}, () => {
console.log('login BPM error');
});

Expand All @@ -276,7 +300,7 @@ export class AlfrescoApi implements Emitter {
this.setAuthenticationClientECMBPM(this.contentAuth.getAuthentication(), null);

this.config.ticketEcm = ticketEcm;
}, () => {
}, () => {
console.log('login ECM error');
});

Expand Down Expand Up @@ -559,4 +583,17 @@ export class AlfrescoApi implements Emitter {
isEcmBpmConfiguration(): boolean {
return this.config.provider && this.config.provider.toUpperCase() === 'ALL';
}

private emitBuffer(event: string, callback ?: any): void {
this.emit(event, callback);
this.bufferEvents.push(event);
}

reply(event: string, callback ?: any): void {
if (this.bufferEvents.indexOf(event) >= 0) {
Function.prototype.apply.call(callback, this, arguments);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback?.apply(this, arguments)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this reply method for?

} else {
this.on(event, callback);
}
}
}
2 changes: 1 addition & 1 deletion src/alfrescoApiCompatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class AlfrescoApiCompatibility extends AlfrescoApi {
moduleCopy[currentClass] = module[currentClass];
let obj = this._stringToObject(currentClass, module);
let nameObj = this._lowerFirst(currentClass);
obj.init(this);
obj?.init(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this, if 2 lines above, we explicitly assigned a value to this variable? Is the returned interface of _stringToObject amboguous?

moduleCopy[nameObj] = obj;
});
}
Expand Down
8 changes: 2 additions & 6 deletions src/api/content-custom-api/api/classes.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ export class ClassesApi extends BaseApi {

private path: string = '/api/classes';

private get contextRoot(): string {
return this.apiClient.config.contextRoot + '/s';
}

/**
* Function to receive the result of the getClass operation.
* @param {String} error Error message, if any.
Expand Down Expand Up @@ -75,7 +71,7 @@ export class ClassesApi extends BaseApi {
return this.apiClient.callApi(
this.path + '/{className}', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, ClassDescription, this.contextRoot
contentTypes, accepts, ClassDescription, this.apiClient.config.contextRoot + '/s'
);
}

Expand All @@ -101,7 +97,7 @@ export class ClassesApi extends BaseApi {
return this.apiClient.callApi(
`${this.path}/{className}/subclasses`, 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, ClassDescription, this.contextRoot
contentTypes, accepts, ClassDescription, this.apiClient.config.contextRoot + '/s'
);
}
}
2 changes: 2 additions & 0 deletions src/authentication/contentAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class ContentAuth extends AlfrescoApiClient {
this.saveUsername(username);
this.setTicket(data.entry.id);
promise.emit('success');
this.emit('logged-in');
resolve(data.entry.id);
})
.catch((error) => {
Expand Down Expand Up @@ -121,6 +122,7 @@ export class ContentAuth extends AlfrescoApiClient {
this.authApi.validateTicket().then((data: any) => {
this.setTicket(data.entry.id);
promise.emit('success');
this.emit('logged-in');
resolve(data.entry.id);
},
(error) => {
Expand Down
1 change: 1 addition & 0 deletions src/authentication/oauth2Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ export class Oauth2Auth extends AlfrescoApiClient {

if (token) {
this.emit('token_issued');
this.emit('logged-in');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/authentication/oauth2Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface Oauth2Config {
secret?: string;
host: string;
scope: string;
implicitFlow: boolean;
implicitFlow?: boolean;
redirectUri: string;
refreshTokenTimeout?: number;
silentLogin?: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/authentication/processAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export class ProcessAuth extends AlfrescoApiClient {
let ticket = this.basicAuth( this.authentications.basicAuth.username, this.authentications.basicAuth.password);
this.setTicket(ticket);
promise.emit('success');
this.emit('logged-in');
resolve(ticket);
},
(error) => {
Expand Down
87 changes: 87 additions & 0 deletions test/alfrescoApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { AlfrescoApi } from '../src/alfrescoApi';

const expect = require('chai').expect;
const AuthEcmMock = require('../test/mockObjects/mockAlfrescoApi').Auth;
const AuthBpmMock = require('../test/mockObjects/mockAlfrescoApi').ActivitiMock.Auth;
const Oauth2Mock = require('../test/mockObjects/mockAlfrescoApi').Oauth2Mock.Auth;

describe('Basic configuration test', () => {

Expand Down Expand Up @@ -176,6 +179,90 @@ describe('Basic configuration test', () => {

expect(error).equal('missing username or password');
});

it('Should logged-in be emitted when log in ECM', (done) => {
const hostEcm = 'http://127.0.0.1:8080';

const authEcmMock = new AuthEcmMock(hostEcm);

const alfrescoJsApi = new AlfrescoApi({
hostEcm,
provider: 'ECM'
});

authEcmMock.get201Response();

alfrescoJsApi.on('logged-in', () => {
done();
});

alfrescoJsApi.login('admin', 'admin')
});

it('Should logged-in be emitted when log in BPM', (done) => {
const hostBpm = 'http://127.0.0.1:9999';
const authBpmMock = new AuthBpmMock(hostBpm);

authBpmMock.get200Response();

const alfrescoJsApi = new AlfrescoApi({
hostBpm: hostBpm,
contextRootBpm: 'activiti-app',
provider: 'BPM'
});

alfrescoJsApi.on('logged-in', () => {
done();
});

alfrescoJsApi.login('admin', 'admin')
});

it('Should logged-in be emitted when log in OAUTH', (done) => {
const oauth2Mock = new Oauth2Mock('http://myOauthUrl:30081');

oauth2Mock.get200Response();

const alfrescoJsApi = new AlfrescoApi({
oauth2: {
'host': 'http://myOauthUrl:30081/auth/realms/springboot',
'clientId': 'activiti',
'scope': 'openid',
'secret': '',
'redirectUri': '/',
'redirectUriLogout': '/logout'
},
authType: 'OAUTH'
});

alfrescoJsApi.on('logged-in', () => {
done();
});

alfrescoJsApi.login('admin', 'admin')
});

it('Should logged-in be emitted when the ticket is in the store', (done) => {
const hostBpm = 'http://127.0.0.1:9999';
const authBpmMock = new AuthBpmMock(hostBpm);

authBpmMock.get200Response();

const alfrescoJsApi = new AlfrescoApi({
hostBpm: hostBpm,
contextRootBpm: 'activiti-app',
provider: 'BPM'
});

alfrescoJsApi.login('admin', 'admin').then(()=>{

alfrescoJsApi.reply('logged-in', () => {
done();
});
});

});

});

});
4 changes: 0 additions & 4 deletions test/performance/test-angular-old/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"sourceRoot": "../src",
"paths": {
"@alfresco/js-api": ["../../../"],
"@alfresco/js-api/*": ["../../../*"]
},
"target": "es5",
"typeRoots": [
"node_modules/@types"
Expand Down
4 changes: 0 additions & 4 deletions test/performance/test-angular/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"sourceRoot": "../src",
"paths": {
"@alfresco/js-api": ["../../../"],
"@alfresco/js-api/*": ["../../../*"]
},
"target": "es5",
"typeRoots": [
"node_modules/@types"
Expand Down
4 changes: 0 additions & 4 deletions test/performance/test-webpack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
"allowSyntheticDefaultImports": true,
"target": "es5",
"sourceRoot": "../src",
"paths": {
"@alfresco/js-api": ["../../../"],
"@alfresco/js-api/*": ["../../../*"]
},
"typeRoots": [
"node_modules/@types"
],
Expand Down