1
0
Fork 0
discourse-custom-wizard-unl.../assets/javascripts/discourse/controllers/admin-wizards-api.js.es6

171 Zeilen
4,7 KiB
Text

import { ajax } from 'discourse/lib/ajax';
import { popupAjaxError } from 'discourse/lib/ajax-error';
import CustomWizardApi from '../models/custom-wizard-api';
import { default as computed } from 'ember-addons/ember-computed-decorators';
export default Ember.Controller.extend({
queryParams: ['refresh_list'],
loadingSubscriptions: false,
notAuthorized: Ember.computed.not('api.authorized'),
authorizationTypes: ['oauth', 'basic'],
isOauth: Ember.computed.equal('api.authType', 'oauth'),
2019-06-03 01:40:54 +02:00
isBasicAuth: Ember.computed.equal('api.authType', 'basic'),
2019-05-31 09:54:11 +02:00
endpointMethods: ['GET', 'PUT', 'POST', 'PATCH', 'DELETE'],
showRemove: Ember.computed.not('isNew'),
responseIcon: null,
@computed('saveDisabled', 'api.authType', 'api.authUrl', 'api.clientId', 'api.clientSecret')
authDisabled(saveDisabled, authType, authUrl, clientId, clientSecret) {
return saveDisabled || !authType || !authUrl || !clientId || !clientSecret;
},
2019-06-03 01:40:54 +02:00
@computed('api.name', 'api.authType')
saveDisabled(name, authType) {
return !name || !authType;
},
actions: {
addParam() {
this.get('api.authParams').pushObject({});
},
removeParam(param) {
this.get('api.authParams').removeObject(param);
},
2019-05-31 09:54:11 +02:00
addEndpoint() {
this.get('api.endpoints').pushObject({});
},
removeEndpoint(endpoint) {
this.get('api.endpoints').removeObject(endpoint);
},
authorize() {
const api = this.get('api');
const { authType, authUrl, authParams } = api;
2019-06-03 01:40:54 +02:00
if (authType !== 'oauth') return;
let query = '?';
2019-06-03 01:40:54 +02:00
query += `client_id=${api.clientId}`;
query += `&redirect_uri=${encodeURIComponent(api.redirectUri)}`;
query += `&response_type=code`;
2019-06-03 01:40:54 +02:00
if (authParams) {
authParams.forEach(p => {
query += `&${p.key}=${encodeURIComponent(p.value)}`;
});
}
window.location.href = authUrl + query;
},
save() {
const api = this.get('api');
const name = api.name;
2019-06-03 01:40:54 +02:00
const authType = api.authType;
let refreshList = false;
let error;
2019-06-03 01:40:54 +02:00
if (!name || !authType) return;
let data = {
2019-06-03 01:40:54 +02:00
auth_type: authType
};
2019-06-03 01:40:54 +02:00
if (api.title) data['title'] = api.title;
const originalTitle = this.get('api.originalTitle');
if (api.get('isNew') || (originalTitle && (api.title !== originalTitle))) {
refreshList = true;
}
if (api.get('isNew')) {
data['new'] = true;
};
let requiredParams;
if (authType === 'oauth') {
requiredParams = ['authUrl', 'tokenUrl', 'clientId', 'clientSecret'];
} else if (authType === 'basic') {
requiredParams = ['username', 'password'];
}
for (let rp of requiredParams) {
if (!api[rp]) {
let key = rp.replace('auth', '');
error = `${I18n.t(`admin.wizard.api.auth.${key.underscore()}`)} is required for ${authType}`;
break;
}
data[rp.underscore()] = api[rp];
}
const params = api.authParams;
if (params.length) {
data['auth_params'] = JSON.stringify(params);
}
const endpoints = api.endpoints;
if (endpoints.length) {
for (let e of endpoints) {
if (!e.name) {
error = 'Every endpoint must have a name';
break;
}
}
data['endpoints'] = JSON.stringify(endpoints);
2019-05-31 09:54:11 +02:00
}
if (error) {
this.set('error', error);
setTimeout(() => {
this.set('error', '');
}, 6000);
return;
}
this.set('updating', true);
ajax(`/admin/wizards/apis/${name.underscore()}`, {
type: 'PUT',
data
}).catch(popupAjaxError)
.then(result => {
if (result.success) {
if (refreshList) {
this.transitionToRoute('adminWizardsApi', result.api.name.dasherize()).then(() => {
this.send('refreshModel');
});
} else {
this.set('api', CustomWizardApi.create(result.api));
this.set('responseIcon', 'check');
}
} else {
this.set('responseIcon', 'times');
}
}).finally(() => this.set('updating', false));
},
remove() {
const name = this.get('api.name');
if (!name) return;
this.set('updating', true);
ajax(`/admin/wizards/apis/${name.underscore()}`, {
type: 'DELETE'
}).catch(popupAjaxError)
.then(result => {
if (result.success) {
this.transitionToRoute('adminWizardsApis').then(() => {
this.send('refreshModel');
});
}
}).finally(() => this.set('updating', false));
}
}
});