discourse/app/assets/javascripts/admin/models/api-key.js.es6

49 lines
1002 B
Plaintext
Raw Normal View History

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