2019-05-30 07:04:34 +02:00
|
|
|
import { ajax } from 'discourse/lib/ajax';
|
|
|
|
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
|
|
|
import CustomWizardApi from '../models/custom-wizard-api';
|
2019-06-02 12:54:31 +02:00
|
|
|
import { default as computed, observes } from 'ember-addons/ember-computed-decorators';
|
|
|
|
import DiscourseURL from 'discourse/lib/url';
|
2019-05-30 07:04:34 +02:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
2019-06-02 12:54:31 +02:00
|
|
|
queryParams: ['refresh_list'],
|
2019-05-30 07:04:34 +02:00
|
|
|
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'],
|
2019-06-02 12:54:31 +02:00
|
|
|
showRemove: Ember.computed.not('isNew'),
|
|
|
|
|
2019-06-03 01:40:54 +02:00
|
|
|
@computed('saveDisabled', 'api.authType', 'api.authUrl')
|
2019-06-02 12:54:31 +02:00
|
|
|
authDisabled(saveDisabled, authType, authUrl) {
|
|
|
|
return saveDisabled || !authType || !authUrl;
|
|
|
|
},
|
|
|
|
|
2019-06-03 01:40:54 +02:00
|
|
|
@computed('api.name', 'api.authType')
|
|
|
|
saveDisabled(name, authType) {
|
|
|
|
return !name || !authType;
|
|
|
|
},
|
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
@observes('api.title')
|
|
|
|
titleWatcher() {
|
|
|
|
const title = this.get('api.title');
|
|
|
|
|
|
|
|
if (this.get('originalTitle')) {
|
|
|
|
this.set('originalTitle', title);
|
|
|
|
}
|
|
|
|
},
|
2019-05-30 07:04:34 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
2019-05-30 07:04:34 +02:00
|
|
|
authorize() {
|
|
|
|
const api = this.get('api');
|
|
|
|
const { authType, authUrl, authParams } = api;
|
2019-06-03 01:40:54 +02:00
|
|
|
|
|
|
|
if (authType !== 'oauth') return;
|
|
|
|
|
2019-05-30 07:04:34 +02:00
|
|
|
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-05-30 07:04:34 +02:00
|
|
|
|
2019-06-03 01:40:54 +02:00
|
|
|
if (authParams) {
|
|
|
|
authParams.forEach(p => {
|
|
|
|
query += `&${p.key}=${encodeURIComponent(p.value)}`;
|
|
|
|
});
|
2019-05-30 07:04:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
window.location.href = authUrl + query;
|
|
|
|
},
|
|
|
|
|
|
|
|
save() {
|
|
|
|
const api = this.get('api');
|
2019-06-02 12:54:31 +02:00
|
|
|
const name = api.name;
|
2019-06-03 01:40:54 +02:00
|
|
|
const authType = api.authType;
|
2019-06-02 12:54:31 +02:00
|
|
|
let refreshList = false;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-06-03 01:40:54 +02:00
|
|
|
if (!name || !authType) return;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
let data = {
|
2019-06-03 01:40:54 +02:00
|
|
|
auth_type: authType
|
2019-06-02 12:54:31 +02:00
|
|
|
};
|
|
|
|
|
2019-06-03 01:40:54 +02:00
|
|
|
if (api.title) data['title'] = api.title;
|
|
|
|
|
|
|
|
if (api.get('isNew') || (api.title !== this.get('originalTitle'))) {
|
2019-06-02 12:54:31 +02:00
|
|
|
refreshList = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (api.get('isNew')) {
|
|
|
|
data['new'] = true;
|
|
|
|
};
|
|
|
|
|
2019-06-03 01:40:54 +02:00
|
|
|
if (authType === 'oauth') {
|
|
|
|
data['auth_url'] = api.authUrl;
|
2019-05-31 23:32:24 +02:00
|
|
|
data['client_id'] = api.clientId;
|
|
|
|
data['client_secret'] = api.clientSecret;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-05-31 23:32:24 +02:00
|
|
|
let params = api.authParams;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
|
|
|
if (params) {
|
|
|
|
data['auth_params'] = JSON.stringify(params);
|
|
|
|
}
|
|
|
|
|
2019-05-31 23:32:24 +02:00
|
|
|
data['token_url'] = api.tokenUrl;
|
2019-06-03 01:40:54 +02:00
|
|
|
} else if (authType === 'basic') {
|
2019-05-31 23:32:24 +02:00
|
|
|
data['username'] = api.username;
|
|
|
|
data['password'] = api.password;
|
2019-05-30 07:04:34 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 23:32:24 +02:00
|
|
|
const endpoints = api.endpoints;
|
2019-06-03 01:40:54 +02:00
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
if (endpoints.length) {
|
|
|
|
data['endpoints'] = JSON.stringify(endpoints);
|
2019-05-31 09:54:11 +02:00
|
|
|
}
|
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
this.set('updating', true);
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
ajax(`/admin/wizards/apis/${name.underscore()}`, {
|
2019-05-30 07:04:34 +02:00
|
|
|
type: 'PUT',
|
|
|
|
data
|
|
|
|
}).catch(popupAjaxError)
|
|
|
|
.then(result => {
|
|
|
|
if (result.success) {
|
2019-06-02 12:54:31 +02:00
|
|
|
if (refreshList) {
|
|
|
|
this.transitionToRoute('adminWizardsApi', result.api.name.dasherize()).then(() => {
|
|
|
|
this.send('refreshModel');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.set('api', CustomWizardApi.create(result.api));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).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) {
|
|
|
|
DiscourseURL.routeTo('/admin/wizards/apis?refresh=true');
|
2019-05-30 07:04:34 +02:00
|
|
|
}
|
2019-06-02 12:54:31 +02:00
|
|
|
}).finally(() => this.set('updating', false));
|
2019-05-30 07:04:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|