0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-14 05:52:52 +01:00
discourse-custom-wizard/assets/javascripts/discourse/controllers/admin-wizards-api-show.js.es6

263 Zeilen
6,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 discourseComputed } from "discourse-common/utils/decorators";
2021-04-12 07:10:02 +02:00
import { and, equal, not } from "@ember/object/computed";
import { selectKitContent } from "../lib/wizard";
import { underscore } from "@ember/string";
2020-04-05 03:37:09 +02:00
import Controller from "@ember/controller";
2020-05-28 05:06:06 +02:00
import I18n from "I18n";
2023-09-14 15:59:22 +02:00
import { inject as service } from "@ember/service";
2020-04-05 03:37:09 +02:00
export default Controller.extend({
router: service(),
queryParams: ["refresh_list"],
loadingSubscriptions: false,
notAuthorized: not("api.authorized"),
endpointMethods: selectKitContent(["PUT", "POST", "PATCH", "DELETE"]),
showRemove: not("isNew"),
showRedirectUri: and("threeLeggedOauth", "api.name"),
responseIcon: null,
contentTypes: selectKitContent([
"application/json",
"application/x-www-form-urlencoded",
]),
successCodes: selectKitContent([
2023-07-10 08:55:54 +02:00
100, 101, 102, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301,
302, 303, 303, 304, 305, 306, 307, 308,
]),
@discourseComputed(
"saveDisabled",
"api.authType",
"api.authUrl",
"api.tokenUrl",
"api.clientId",
"api.clientSecret",
"threeLeggedOauth"
)
authDisabled(
saveDisabled,
authType,
authUrl,
tokenUrl,
clientId,
clientSecret,
threeLeggedOauth
) {
2021-04-12 08:12:20 +02:00
if (saveDisabled || !authType || !tokenUrl || !clientId || !clientSecret) {
return true;
}
if (threeLeggedOauth) {
return !authUrl;
}
return false;
},
@discourseComputed("api.name", "api.authType")
2019-06-03 01:40:54 +02:00
saveDisabled(name, authType) {
return !name || !authType;
},
authorizationTypes: selectKitContent(["none", "basic", "oauth_2", "oauth_3"]),
isBasicAuth: equal("api.authType", "basic"),
@discourseComputed("api.authType")
isOauth(authType) {
return authType && authType.indexOf("oauth") > -1;
},
twoLeggedOauth: equal("api.authType", "oauth_2"),
threeLeggedOauth: equal("api.authType", "oauth_3"),
2022-06-02 16:45:20 +02:00
@discourseComputed("api.isNew")
nameClass(isNew) {
return isNew ? "new" : "saved";
},
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({});
2019-05-31 09:54:11 +02:00
},
removeEndpoint(endpoint) {
this.get("api.endpoints").removeObject(endpoint);
2019-05-31 09:54:11 +02:00
},
authorize() {
const api = this.get("api");
const { name, authType, authUrl, authParams } = api;
this.set("authErrorMessage", "");
if (authType === "oauth_2") {
this.set("authorizing", true);
ajax(`/admin/wizards/apis/${underscore(name)}/authorize`)
.catch(popupAjaxError)
.then((result) => {
if (result.success) {
this.set("api", CustomWizardApi.create(result.api));
} else if (result.failed && result.message) {
this.set("authErrorMessage", result.message);
} else {
this.set("authErrorMessage", "Authorization Failed");
}
setTimeout(() => {
this.set("authErrorMessage", "");
}, 6000);
})
.finally(() => this.set("authorizing", false));
} else if (authType === "oauth_3") {
let query = "?";
query += `client_id=${api.clientId}`;
query += `&redirect_uri=${encodeURIComponent(api.redirectUri)}`;
query += `&response_type=code`;
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 error;
2021-04-12 08:12:20 +02:00
if (!name || !authType) {
return;
}
let data = {
auth_type: authType,
};
2021-04-12 08:12:20 +02:00
if (api.title) {
data["title"] = api.title;
}
2019-06-03 01:40:54 +02:00
if (api.get("isNew")) {
data["new"] = true;
}
let requiredParams;
if (authType === "basic") {
requiredParams = ["username", "password"];
} else if (authType === "oauth_2") {
requiredParams = ["tokenUrl", "clientId", "clientSecret"];
} else if (authType === "oauth_3") {
requiredParams = ["authUrl", "tokenUrl", "clientId", "clientSecret"];
}
if (requiredParams) {
for (let rp of requiredParams) {
if (!api[rp]) {
let key = rp.replace("auth", "");
error = `${I18n.t(
`admin.wizard.api.auth.${underscore(key)}`
)} is required for ${authType}`;
break;
}
data[underscore(rp)] = 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/api/${underscore(name)}`, {
type: "PUT",
data,
})
.catch(popupAjaxError)
.then((result) => {
if (result.success) {
this.send("afterSave", result.api.name);
} else {
this.set("responseIcon", "times");
}
})
.finally(() => this.set("updating", false));
},
remove() {
const name = this.get("api.name");
2021-04-12 08:12:20 +02:00
if (!name) {
return;
}
this.set("updating", true);
ajax(`/admin/wizards/api/${underscore(name)}`, {
type: "DELETE",
})
.catch(popupAjaxError)
.then((result) => {
if (result.success) {
this.send("afterDestroy");
}
})
.finally(() => this.set("updating", false));
2019-06-06 18:10:13 +02:00
},
clearLogs() {
const name = this.get("api.name");
2021-04-12 08:12:20 +02:00
if (!name) {
return;
}
ajax(`/admin/wizards/api/${underscore(name)}/logs`, {
type: "DELETE",
})
.catch(popupAjaxError)
.then((result) => {
2019-06-06 18:10:13 +02:00
if (result.success) {
2023-09-14 21:02:02 +02:00
this.router.transitionTo("adminWizardsApis").then(() => {
this.send("refreshModel");
2019-06-06 18:10:13 +02:00
});
}
})
.finally(() => this.set("updating", false));
},
},
});