2017-03-14 17:19:53 +00:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
2018-04-20 14:38:10 +01:00
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import { empty } from '@ember/object/computed';
|
2017-11-16 13:28:05 +00:00
|
|
|
import { set } from '@ember/object';
|
|
|
|
import { copy } from '@ember/object/internals';
|
|
|
|
import { inject as service } from '@ember/service';
|
2018-06-05 14:04:14 +01:00
|
|
|
import Notifier from '../../mixins/notifier';
|
2017-03-21 11:57:42 +00:00
|
|
|
import encoding from '../../utils/encoding';
|
2018-06-05 14:04:14 +01:00
|
|
|
import Component from '@ember/component';
|
2017-03-14 17:19:53 +00:00
|
|
|
|
2018-06-05 14:04:14 +01:00
|
|
|
export default Component.extend(Notifier, {
|
2017-11-16 13:28:05 +00:00
|
|
|
appMeta: service(),
|
2018-04-20 14:38:10 +01:00
|
|
|
isDocumizeProvider: computed('authProvider', function() {
|
|
|
|
return this.get('authProvider') === this.get('constants').AuthProvider.Documize;
|
|
|
|
}),
|
|
|
|
isKeycloakProvider: computed('authProvider', function() {
|
|
|
|
return this.get('authProvider') === this.get('constants').AuthProvider.Keycloak;
|
|
|
|
}),
|
2017-11-16 13:28:05 +00:00
|
|
|
KeycloakUrlError: empty('keycloakConfig.url'),
|
|
|
|
KeycloakRealmError: empty('keycloakConfig.realm'),
|
|
|
|
KeycloakClientIdError: empty('keycloakConfig.clientId'),
|
|
|
|
KeycloakPublicKeyError: empty('keycloakConfig.publicKey'),
|
|
|
|
KeycloakAdminUserError: empty('keycloakConfig.adminUser'),
|
|
|
|
KeycloakAdminPasswordError: empty('keycloakConfig.adminPassword'),
|
2018-06-12 14:07:50 +01:00
|
|
|
keycloakFailure: '',
|
2018-01-22 10:31:03 +00:00
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.keycloakConfig = {
|
|
|
|
url: '',
|
|
|
|
realm: '',
|
|
|
|
clientId: '',
|
|
|
|
publicKey: '',
|
|
|
|
adminUser: '',
|
|
|
|
adminPassword: '',
|
|
|
|
group: '',
|
|
|
|
disableLogout: false,
|
|
|
|
defaultPermissionAddSpace: false
|
|
|
|
};
|
2017-03-16 11:46:09 +00:00
|
|
|
},
|
2017-03-14 17:19:53 +00:00
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
let provider = this.get('authProvider');
|
2018-04-20 14:38:10 +01:00
|
|
|
let constants = this.get('constants');
|
2017-03-14 17:19:53 +00:00
|
|
|
|
|
|
|
switch (provider) {
|
|
|
|
case constants.AuthProvider.Documize:
|
2017-03-19 11:37:18 +00:00
|
|
|
// nothing to do
|
2017-03-14 17:19:53 +00:00
|
|
|
break;
|
2017-03-19 11:37:18 +00:00
|
|
|
case constants.AuthProvider.Keycloak: // eslint-disable-line no-case-declarations
|
2017-03-16 11:46:09 +00:00
|
|
|
let config = this.get('authConfig');
|
|
|
|
|
|
|
|
if (is.undefined(config) || is.null(config) || is.empty(config) ) {
|
|
|
|
config = {};
|
|
|
|
} else {
|
|
|
|
config = JSON.parse(config);
|
2017-03-17 08:46:33 +00:00
|
|
|
config.publicKey = encoding.Base64.decode(config.publicKey);
|
2017-04-16 14:56:00 +01:00
|
|
|
config.defaultPermissionAddSpace = config.hasOwnProperty('defaultPermissionAddSpace') ? config.defaultPermissionAddSpace : false;
|
|
|
|
config.disableLogout = config.hasOwnProperty('disableLogout') ? config.disableLogout : true;
|
2017-03-16 11:46:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.set('keycloakConfig', config);
|
2017-03-14 17:19:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
onDocumize() {
|
2018-04-20 14:38:10 +01:00
|
|
|
let constants = this.get('constants');
|
2017-03-14 17:19:53 +00:00
|
|
|
this.set('authProvider', constants.AuthProvider.Documize);
|
|
|
|
},
|
|
|
|
|
|
|
|
onKeycloak() {
|
2018-04-20 14:38:10 +01:00
|
|
|
let constants = this.get('constants');
|
2017-03-14 17:19:53 +00:00
|
|
|
this.set('authProvider', constants.AuthProvider.Keycloak);
|
|
|
|
},
|
|
|
|
|
|
|
|
onSave() {
|
2018-04-20 14:38:10 +01:00
|
|
|
let constants = this.get('constants');
|
2017-03-14 17:19:53 +00:00
|
|
|
let provider = this.get('authProvider');
|
|
|
|
let config = this.get('authConfig');
|
|
|
|
|
2018-06-12 14:07:50 +01:00
|
|
|
this.set('keycloakFailure', '');
|
|
|
|
|
2017-03-14 17:19:53 +00:00
|
|
|
switch (provider) {
|
|
|
|
case constants.AuthProvider.Documize:
|
|
|
|
config = {};
|
|
|
|
break;
|
|
|
|
case constants.AuthProvider.Keycloak:
|
2017-03-16 11:46:09 +00:00
|
|
|
if (this.get('KeycloakUrlError')) {
|
|
|
|
this.$("#keycloak-url").focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.get('KeycloakRealmError')) {
|
|
|
|
this.$("#keycloak-realm").focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.get('KeycloakClientIdError')) {
|
|
|
|
this.$("#keycloak-clientId").focus();
|
|
|
|
return;
|
|
|
|
}
|
2017-03-16 13:33:34 +00:00
|
|
|
if (this.get('KeycloakPublicKeyError')) {
|
|
|
|
this.$("#keycloak-publicKey").focus();
|
|
|
|
return;
|
|
|
|
}
|
2017-03-17 19:01:32 +00:00
|
|
|
if (this.get('KeycloakAdminUserError')) {
|
|
|
|
this.$("#keycloak-admin-user").focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.get('KeycloakAdminPasswordError')) {
|
|
|
|
this.$("#keycloak-admin-password").focus();
|
|
|
|
return;
|
|
|
|
}
|
2017-03-16 11:46:09 +00:00
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
config = copy(this.get('keycloakConfig'));
|
2017-03-28 10:25:32 +01:00
|
|
|
config.url = config.url.trim();
|
|
|
|
config.realm = config.realm.trim();
|
|
|
|
config.clientId = config.clientId.trim();
|
|
|
|
config.publicKey = config.publicKey.trim();
|
|
|
|
config.group = is.undefined(config.group) ? '' : config.group.trim();
|
|
|
|
config.adminUser = config.adminUser.trim();
|
|
|
|
config.adminPassword = config.adminPassword.trim();
|
2017-04-16 14:56:00 +01:00
|
|
|
config.defaultPermissionAddSpace = config.hasOwnProperty('defaultPermissionAddSpace') ? config.defaultPermissionAddSpace : true;
|
|
|
|
config.disableLogout = config.hasOwnProperty('disableLogout') ? config.disableLogout : true;
|
2017-03-28 10:25:32 +01:00
|
|
|
|
|
|
|
if (is.endWith(config.url, '/')) {
|
|
|
|
config.url = config.url.substring(0, config.url.length-1);
|
|
|
|
}
|
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
set(config, 'publicKey', encoding.Base64.encode(this.get('keycloakConfig.publicKey')));
|
2017-03-14 17:19:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-11-28 12:49:48 +00:00
|
|
|
|
2018-06-05 14:04:14 +01:00
|
|
|
this.showWait();
|
|
|
|
|
2017-03-28 10:25:32 +01:00
|
|
|
let data = { authProvider: provider, authConfig: JSON.stringify(config) };
|
2017-03-14 17:19:53 +00:00
|
|
|
|
2017-03-28 10:25:32 +01:00
|
|
|
this.get('onSave')(data).then(() => {
|
|
|
|
if (data.authProvider === constants.AuthProvider.Keycloak) {
|
|
|
|
this.get('onSync')().then((response) => {
|
|
|
|
if (response.isError) {
|
2018-06-12 14:07:50 +01:00
|
|
|
this.set('keycloakFailure', response.message);
|
2018-04-23 12:46:34 +01:00
|
|
|
console.log(response.message); // eslint-disable-line no-console
|
2017-03-28 10:25:32 +01:00
|
|
|
data.authProvider = constants.AuthProvider.Documize;
|
2018-06-12 14:07:50 +01:00
|
|
|
this.get('onSave')(data).then(() => {});
|
2017-03-28 10:25:32 +01:00
|
|
|
} else {
|
|
|
|
if (data.authProvider === this.get('appMeta.authProvider')) {
|
2018-04-23 12:46:34 +01:00
|
|
|
console.log(response.message); // eslint-disable-line no-console
|
2017-03-28 10:25:32 +01:00
|
|
|
} else {
|
|
|
|
this.get('onChange')(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-06-05 14:04:14 +01:00
|
|
|
this.showDone();
|
2017-03-14 17:19:53 +00:00
|
|
|
});
|
2017-03-19 14:25:21 +00:00
|
|
|
}
|
2017-03-14 17:19:53 +00:00
|
|
|
}
|
|
|
|
});
|