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
|
|
|
|
2015-11-21 09:27:06 +08:00
|
|
|
const ApiKey = Discourse.Model.extend({
|
2013-10-23 03:53:08 +08:00
|
|
|
/**
|
|
|
|
Regenerates the api key
|
|
|
|
|
|
|
|
@method regenerate
|
|
|
|
@returns {Promise} a promise that resolves to the key
|
|
|
|
**/
|
|
|
|
regenerate: function() {
|
|
|
|
var self = this;
|
2018-06-15 23:03:24 +08:00
|
|
|
return ajax("/admin/api/key", {
|
|
|
|
type: "PUT",
|
|
|
|
data: { id: this.get("id") }
|
|
|
|
}).then(function(result) {
|
|
|
|
self.set("key", result.api_key.key);
|
2013-10-23 03:53:08 +08:00
|
|
|
return self;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Revokes the current key
|
|
|
|
|
|
|
|
@method revoke
|
|
|
|
@returns {Promise} a promise that resolves when the key has been revoked
|
|
|
|
**/
|
|
|
|
revoke: function() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return ajax("/admin/api/key", {
|
|
|
|
type: "DELETE",
|
|
|
|
data: { id: this.get("id") }
|
|
|
|
});
|
2013-10-23 03:53:08 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-21 09:27:06 +08:00
|
|
|
ApiKey.reopenClass({
|
2013-10-23 03:53:08 +08:00
|
|
|
/**
|
|
|
|
Creates an API key instance with internal user object
|
|
|
|
|
|
|
|
@method create
|
2014-03-19 09:19:20 +08:00
|
|
|
@param {...} var_args the properties to initialize this with
|
2015-11-21 09:27:06 +08:00
|
|
|
@returns {ApiKey} the ApiKey instance
|
2013-10-23 03:53:08 +08:00
|
|
|
**/
|
2017-07-06 04:47:01 +08:00
|
|
|
create() {
|
2013-10-30 01:01:42 +08:00
|
|
|
var 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;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Finds a list of API keys
|
|
|
|
|
|
|
|
@method find
|
2015-11-21 09:27:06 +08:00
|
|
|
@returns {Promise} a promise that resolves to the array of `ApiKey` instances
|
2013-10-23 03:53:08 +08:00
|
|
|
**/
|
|
|
|
find: function() {
|
2016-09-19 16:43:06 +08:00
|
|
|
return ajax("/admin/api/keys").then(function(keys) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return keys.map(function(key) {
|
2015-11-21 09:27:06 +08:00
|
|
|
return ApiKey.create(key);
|
2013-10-23 03:53:08 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Generates a master api key and returns it.
|
|
|
|
|
|
|
|
@method generateMasterKey
|
2015-11-21 09:27:06 +08:00
|
|
|
@returns {Promise} a promise that resolves to a master `ApiKey`
|
2013-10-23 03:53:08 +08:00
|
|
|
**/
|
|
|
|
generateMasterKey: function() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return ajax("/admin/api/key", { type: "POST" }).then(function(result) {
|
2015-11-21 09:27:06 +08:00
|
|
|
return ApiKey.create(result.api_key);
|
2013-10-23 03:53:08 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-11-21 09:27:06 +08:00
|
|
|
|
|
|
|
export default ApiKey;
|