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-05-31 09:54:11 +02:00
|
|
|
endpointMethods: ['GET', 'PUT', 'POST', 'PATCH', 'DELETE'],
|
2019-06-02 12:54:31 +02:00
|
|
|
saveDisabled: Ember.computed.empty('api.name'),
|
|
|
|
showRemove: Ember.computed.not('isNew'),
|
|
|
|
|
|
|
|
@computed('saveDisabled', 'authType', 'authUrl')
|
|
|
|
authDisabled(saveDisabled, authType, authUrl) {
|
|
|
|
return saveDisabled || !authType || !authUrl;
|
|
|
|
},
|
|
|
|
|
|
|
|
@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;
|
|
|
|
let query = '?';
|
|
|
|
|
|
|
|
if (authType === 'oauth') {
|
2019-05-31 23:32:24 +02:00
|
|
|
query += `client_id=${api.clientId}&redirect_uri=${encodeURIComponent(api.redirectUri)}&response_type=code`;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
|
|
|
if (authParams) {
|
|
|
|
authParams.forEach(p => {
|
|
|
|
query += `&${p.key}=${encodeURIComponent(p.value)}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2019-06-03 00:59:35 +02:00
|
|
|
// basic auth - no need to authorize separately
|
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;
|
|
|
|
let refreshList = false;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
if (!name ) return;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
let data = {
|
|
|
|
title: api.title
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.get('isNew') || (api.title !== this.get('originalTitle'))) {
|
|
|
|
refreshList = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (api.get('isNew')) {
|
|
|
|
data['new'] = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (api.authType) data['auth_type'] = api.authType;
|
|
|
|
if (api.authUrl) data['auth_url'] = api.authUrl;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
|
|
|
if (data.auth_type === 'oauth') {
|
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-05-30 07:04:34 +02:00
|
|
|
} else {
|
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 00:59:35 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|