2019-11-08 05:38:28 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2017-07-06 04:47:01 +08:00
|
|
|
import AdminUser from "admin/models/admin-user";
|
2019-11-05 22:10:23 +08:00
|
|
|
import RestModel from "discourse/models/rest";
|
2016-07-01 01:55:44 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2019-11-14 00:57:42 +08:00
|
|
|
import { computed } from "@ember/object";
|
2019-12-12 19:45:00 +08:00
|
|
|
import { fmt } from "discourse/lib/computed";
|
2017-07-06 04:47:01 +08:00
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
const ApiKey = RestModel.extend({
|
2019-11-09 02:28:11 +08:00
|
|
|
user: computed("_user", {
|
2019-11-05 22:10:23 +08:00
|
|
|
get() {
|
|
|
|
return this._user;
|
|
|
|
},
|
|
|
|
set(key, value) {
|
|
|
|
if (value && !(value instanceof AdminUser)) {
|
|
|
|
this.set("_user", AdminUser.create(value));
|
|
|
|
} else {
|
|
|
|
this.set("_user", value);
|
|
|
|
}
|
|
|
|
return this._user;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("description")
|
2019-11-05 22:10:23 +08:00
|
|
|
shortDescription(description) {
|
|
|
|
if (!description || description.length < 40) return description;
|
|
|
|
return `${description.substring(0, 40)}...`;
|
2013-10-23 03:53:08 +08:00
|
|
|
},
|
|
|
|
|
2019-12-12 19:45:00 +08:00
|
|
|
truncatedKey: fmt("truncated_key", "%@..."),
|
|
|
|
|
2019-01-24 00:40:05 +08:00
|
|
|
revoke() {
|
2019-11-05 22:10:23 +08:00
|
|
|
return ajax(`${this.basePath}/revoke`, {
|
|
|
|
type: "POST"
|
|
|
|
}).then(result => this.setProperties(result.api_key));
|
|
|
|
},
|
2013-10-23 03:53:08 +08:00
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
undoRevoke() {
|
|
|
|
return ajax(`${this.basePath}/undo-revoke`, {
|
|
|
|
type: "POST"
|
|
|
|
}).then(result => this.setProperties(result.api_key));
|
2013-10-23 03:53:08 +08:00
|
|
|
},
|
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
createProperties() {
|
2020-07-17 02:51:24 +08:00
|
|
|
return this.getProperties("description", "username", "scopes");
|
2013-10-23 03:53:08 +08:00
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed()
|
2019-11-05 22:10:23 +08:00
|
|
|
basePath() {
|
|
|
|
return this.store
|
|
|
|
.adapterFor("api-key")
|
|
|
|
.pathFor(this.store, "api-key", this.id);
|
2013-10-23 03:53:08 +08:00
|
|
|
}
|
|
|
|
});
|
2015-11-21 09:27:06 +08:00
|
|
|
|
|
|
|
export default ApiKey;
|