From 60974932c4d0895180908a141d4e83d3a95e3777 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Wed, 23 Jan 2019 17:40:05 +0100 Subject: [PATCH] DEV: refactoring api-keys (#6931) --- .../admin/controllers/admin-api-keys.js.es6 | 13 ++-- .../javascripts/admin/models/api-key.js.es6 | 69 +++++-------------- .../javascripts/admin/templates/api-keys.hbs | 32 ++++++--- 3 files changed, 48 insertions(+), 66 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/admin-api-keys.js.es6 b/app/assets/javascripts/admin/controllers/admin-api-keys.js.es6 index 69a1c321f78..1ec203fd893 100644 --- a/app/assets/javascripts/admin/controllers/admin-api-keys.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-api-keys.js.es6 @@ -1,6 +1,12 @@ import ApiKey from "admin/models/api-key"; +import { default as computed } from "ember-addons/ember-computed-decorators"; export default Ember.Controller.extend({ + @computed("model.[]") + hasMasterKey(model) { + return !!model.findBy("user", null); + }, + actions: { generateMasterKey() { 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.[]") + } }); diff --git a/app/assets/javascripts/admin/models/api-key.js.es6 b/app/assets/javascripts/admin/models/api-key.js.es6 index 56362504482..4b0eb655cae 100644 --- a/app/assets/javascripts/admin/models/api-key.js.es6 +++ b/app/assets/javascripts/admin/models/api-key.js.es6 @@ -1,32 +1,22 @@ import AdminUser from "admin/models/admin-user"; import { ajax } from "discourse/lib/ajax"; -const ApiKey = Discourse.Model.extend({ - /** - Regenerates the api key +const KEY_ENDPOINT = "/admin/api/key"; +const KEYS_ENDPOINT = "/admin/api/keys"; - @method regenerate - @returns {Promise} a promise that resolves to the key - **/ - regenerate: function() { - var self = this; - return ajax("/admin/api/key", { +const ApiKey = Discourse.Model.extend({ + regenerate() { + return ajax(KEY_ENDPOINT, { type: "PUT", data: { id: this.get("id") } - }).then(function(result) { - self.set("key", result.api_key.key); - return self; + }).then(result => { + this.set("key", result.api_key.key); + return this; }); }, - /** - Revokes the current key - - @method revoke - @returns {Promise} a promise that resolves when the key has been revoked - **/ - revoke: function() { - return ajax("/admin/api/key", { + revoke() { + return ajax(KEY_ENDPOINT, { type: "DELETE", data: { id: this.get("id") } }); @@ -34,45 +24,24 @@ const ApiKey = Discourse.Model.extend({ }); 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() { - var result = this._super.apply(this, arguments); + const result = this._super.apply(this, arguments); if (result.user) { result.user = AdminUser.create(result.user); } return result; }, - /** - Finds a list of API keys - - @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); - }); - }); + find() { + return ajax(KEYS_ENDPOINT).then(keys => + keys.map(key => ApiKey.create(key)) + ); }, - /** - Generates a master api key and returns it. - - @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); - }); + generateMasterKey() { + return ajax(KEY_ENDPOINT, { type: "POST" }).then(result => + ApiKey.create(result.api_key) + ); } }); diff --git a/app/assets/javascripts/admin/templates/api-keys.hbs b/app/assets/javascripts/admin/templates/api-keys.hbs index 1b85311b6e1..d631e4188d8 100644 --- a/app/assets/javascripts/admin/templates/api-keys.hbs +++ b/app/assets/javascripts/admin/templates/api-keys.hbs @@ -1,35 +1,47 @@ {{#if model}} - +
- - + + {{#each model as |k|}} - + {{/each}}
{{i18n 'admin.api.key'}}{{i18n 'admin.api.user'}}{{i18n "admin.api.key"}}{{i18n "admin.api.user"}}  
{{k.key}}{{k.key}} {{#if k.user}} - {{#link-to 'adminUser' k.user}} + {{#link-to "adminUser" k.user}} {{avatar k.user imageSize="small"}} {{/link-to}} {{else}} - {{i18n 'admin.api.all_users'}} + {{i18n "admin.api.all_users"}} {{/if}} - {{d-button 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'}} + {{d-button + 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"}}
{{else}} -

{{i18n 'admin.api.none'}}

+

{{i18n "admin.api.none"}}

{{/if}} {{#unless hasMasterKey}} - + {{d-button + class="btn-primary" + action=(action "generateMasterKey") + icon="key"}} {{/unless}}