DEV: refactoring api-keys (#6931)

This commit is contained in:
Joffrey JAFFEUX 2019-01-23 17:40:05 +01:00 committed by GitHub
parent 9a594fed01
commit 60974932c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 66 deletions

View File

@ -1,6 +1,12 @@
import ApiKey from "admin/models/api-key"; import ApiKey from "admin/models/api-key";
import { default as computed } from "ember-addons/ember-computed-decorators";
export default Ember.Controller.extend({ export default Ember.Controller.extend({
@computed("model.[]")
hasMasterKey(model) {
return !!model.findBy("user", null);
},
actions: { actions: {
generateMasterKey() { generateMasterKey() {
ApiKey.generateMasterKey().then(key => this.get("model").pushObject(key)); ApiKey.generateMasterKey().then(key => this.get("model").pushObject(key));
@ -31,10 +37,5 @@ export default Ember.Controller.extend({
} }
); );
} }
}, }
// Has a master key already been generated?
hasMasterKey: function() {
return !!this.get("model").findBy("user", null);
}.property("model.[]")
}); });

View File

@ -1,32 +1,22 @@
import AdminUser from "admin/models/admin-user"; import AdminUser from "admin/models/admin-user";
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
const ApiKey = Discourse.Model.extend({ const KEY_ENDPOINT = "/admin/api/key";
/** const KEYS_ENDPOINT = "/admin/api/keys";
Regenerates the api key
@method regenerate const ApiKey = Discourse.Model.extend({
@returns {Promise} a promise that resolves to the key regenerate() {
**/ return ajax(KEY_ENDPOINT, {
regenerate: function() {
var self = this;
return ajax("/admin/api/key", {
type: "PUT", type: "PUT",
data: { id: this.get("id") } data: { id: this.get("id") }
}).then(function(result) { }).then(result => {
self.set("key", result.api_key.key); this.set("key", result.api_key.key);
return self; return this;
}); });
}, },
/** revoke() {
Revokes the current key return ajax(KEY_ENDPOINT, {
@method revoke
@returns {Promise} a promise that resolves when the key has been revoked
**/
revoke: function() {
return ajax("/admin/api/key", {
type: "DELETE", type: "DELETE",
data: { id: this.get("id") } data: { id: this.get("id") }
}); });
@ -34,45 +24,24 @@ const ApiKey = Discourse.Model.extend({
}); });
ApiKey.reopenClass({ ApiKey.reopenClass({
/**
Creates an API key instance with internal user object
@method create
@param {...} var_args the properties to initialize this with
@returns {ApiKey} the ApiKey instance
**/
create() { create() {
var result = this._super.apply(this, arguments); const result = this._super.apply(this, arguments);
if (result.user) { if (result.user) {
result.user = AdminUser.create(result.user); result.user = AdminUser.create(result.user);
} }
return result; return result;
}, },
/** find() {
Finds a list of API keys return ajax(KEYS_ENDPOINT).then(keys =>
keys.map(key => ApiKey.create(key))
@method find );
@returns {Promise} a promise that resolves to the array of `ApiKey` instances
**/
find: function() {
return ajax("/admin/api/keys").then(function(keys) {
return keys.map(function(key) {
return ApiKey.create(key);
});
});
}, },
/** generateMasterKey() {
Generates a master api key and returns it. return ajax(KEY_ENDPOINT, { type: "POST" }).then(result =>
ApiKey.create(result.api_key)
@method generateMasterKey );
@returns {Promise} a promise that resolves to a master `ApiKey`
**/
generateMasterKey: function() {
return ajax("/admin/api/key", { type: "POST" }).then(function(result) {
return ApiKey.create(result.api_key);
});
} }
}); });

View File

@ -1,35 +1,47 @@
{{#if model}} {{#if model}}
<table class='api-keys grid'> <table class="api-keys grid">
<thead> <thead>
<th>{{i18n 'admin.api.key'}}</th> <th>{{i18n "admin.api.key"}}</th>
<th>{{i18n 'admin.api.user'}}</th> <th>{{i18n "admin.api.user"}}</th>
<th>&nbsp;</th> <th>&nbsp;</th>
</thead> </thead>
<tbody> <tbody>
{{#each model as |k|}} {{#each model as |k|}}
<tr> <tr>
<td class='key'>{{k.key}}</td> <td class="key">{{k.key}}</td>
<td class="key-user"> <td class="key-user">
{{#if k.user}} {{#if k.user}}
{{#link-to 'adminUser' k.user}} {{#link-to "adminUser" k.user}}
{{avatar k.user imageSize="small"}} {{avatar k.user imageSize="small"}}
{{/link-to}} {{/link-to}}
{{else}} {{else}}
{{i18n 'admin.api.all_users'}} {{i18n "admin.api.all_users"}}
{{/if}} {{/if}}
</td> </td>
<td class="key-controls"> <td class="key-controls">
{{d-button class="btn-default" action=(action "regenerateKey") actionParam=k icon="undo" label='admin.api.regenerate'}} {{d-button
{{d-button class="btn-default" action=(action "revokeKey") actionParam=k icon="times" label='admin.api.revoke'}} class="btn-default"
action=(action "regenerateKey")
actionParam=k icon="undo"
label="admin.api.regenerate"}}
{{d-button
class="btn-default"
action=(action "revokeKey")
actionParam=k
icon="times"
label="admin.api.revoke"}}
</td> </td>
</tr> </tr>
{{/each}} {{/each}}
</tbody> </tbody>
</table> </table>
{{else}} {{else}}
<p>{{i18n 'admin.api.none'}}</p> <p>{{i18n "admin.api.none"}}</p>
{{/if}} {{/if}}
{{#unless hasMasterKey}} {{#unless hasMasterKey}}
<button class='btn btn-icon no-text btn-primary' {{action "generateMasterKey"}}>{{d-icon "key"}}</button> {{d-button
class="btn-primary"
action=(action "generateMasterKey")
icon="key"}}
{{/unless}} {{/unless}}