2018-06-15 23:03:24 +08:00
|
|
|
import AdminUser from "admin/models/admin-user";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2017-07-06 04:47:01 +08:00
|
|
|
|
2019-01-24 00:40:05 +08:00
|
|
|
const KEY_ENDPOINT = "/admin/api/key";
|
|
|
|
const KEYS_ENDPOINT = "/admin/api/keys";
|
2013-10-23 03:53:08 +08:00
|
|
|
|
2019-01-24 00:40:05 +08:00
|
|
|
const ApiKey = Discourse.Model.extend({
|
|
|
|
regenerate() {
|
|
|
|
return ajax(KEY_ENDPOINT, {
|
2018-06-15 23:03:24 +08:00
|
|
|
type: "PUT",
|
2019-05-27 16:15:39 +08:00
|
|
|
data: { id: this.id }
|
2019-01-24 00:40:05 +08:00
|
|
|
}).then(result => {
|
|
|
|
this.set("key", result.api_key.key);
|
|
|
|
return this;
|
2013-10-23 03:53:08 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-01-24 00:40:05 +08:00
|
|
|
revoke() {
|
|
|
|
return ajax(KEY_ENDPOINT, {
|
2018-06-15 23:03:24 +08:00
|
|
|
type: "DELETE",
|
2019-05-27 16:15:39 +08:00
|
|
|
data: { id: this.id }
|
2018-06-15 23:03:24 +08:00
|
|
|
});
|
2013-10-23 03:53:08 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-21 09:27:06 +08:00
|
|
|
ApiKey.reopenClass({
|
2017-07-06 04:47:01 +08:00
|
|
|
create() {
|
2019-01-24 00:40:05 +08:00
|
|
|
const result = this._super.apply(this, arguments);
|
2013-10-23 03:53:08 +08:00
|
|
|
if (result.user) {
|
2015-11-21 09:27:06 +08:00
|
|
|
result.user = AdminUser.create(result.user);
|
2013-10-23 03:53:08 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2019-01-24 00:40:05 +08:00
|
|
|
find() {
|
|
|
|
return ajax(KEYS_ENDPOINT).then(keys =>
|
|
|
|
keys.map(key => ApiKey.create(key))
|
|
|
|
);
|
2013-10-23 03:53:08 +08:00
|
|
|
},
|
|
|
|
|
2019-01-24 00:40:05 +08:00
|
|
|
generateMasterKey() {
|
|
|
|
return ajax(KEY_ENDPOINT, { type: "POST" }).then(result =>
|
|
|
|
ApiKey.create(result.api_key)
|
|
|
|
);
|
2013-10-23 03:53:08 +08:00
|
|
|
}
|
|
|
|
});
|
2015-11-21 09:27:06 +08:00
|
|
|
|
|
|
|
export default ApiKey;
|