mirror of
https://github.com/discourse/discourse.git
synced 2024-12-13 00:43:44 +08:00
49 lines
988 B
JavaScript
49 lines
988 B
JavaScript
import AdminUser from "admin/models/admin-user";
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
const KEY_ENDPOINT = "/admin/api/key";
|
|
const KEYS_ENDPOINT = "/admin/api/keys";
|
|
|
|
const ApiKey = Discourse.Model.extend({
|
|
regenerate() {
|
|
return ajax(KEY_ENDPOINT, {
|
|
type: "PUT",
|
|
data: { id: this.id }
|
|
}).then(result => {
|
|
this.set("key", result.api_key.key);
|
|
return this;
|
|
});
|
|
},
|
|
|
|
revoke() {
|
|
return ajax(KEY_ENDPOINT, {
|
|
type: "DELETE",
|
|
data: { id: this.id }
|
|
});
|
|
}
|
|
});
|
|
|
|
ApiKey.reopenClass({
|
|
create() {
|
|
const result = this._super.apply(this, arguments);
|
|
if (result.user) {
|
|
result.user = AdminUser.create(result.user);
|
|
}
|
|
return result;
|
|
},
|
|
|
|
find() {
|
|
return ajax(KEYS_ENDPOINT).then(keys =>
|
|
keys.map(key => ApiKey.create(key))
|
|
);
|
|
},
|
|
|
|
generateMasterKey() {
|
|
return ajax(KEY_ENDPOINT, { type: "POST" }).then(result =>
|
|
ApiKey.create(result.api_key)
|
|
);
|
|
}
|
|
});
|
|
|
|
export default ApiKey;
|