From 547750e9dd25e9b06355404cd54ac2293ad1df48 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 19 Sep 2016 16:43:06 +0800 Subject: [PATCH] Unify API keys and web hooks into a single admin nav header. --- .../admin/adapters/web-hook-event.js.es6 | 7 ++++ .../admin/adapters/web-hook.js.es6 | 7 ++++ .../components/admin-web-hook-event.js.es6 | 2 +- ...admin-api.js.es6 => admin-api-keys.js.es6} | 0 .../admin-web-hooks-show-events.js.es6 | 4 +- .../javascripts/admin/models/api-key.js.es6 | 2 +- .../admin/routes/admin-api-index.js.es6 | 5 +++ ...admin-api.js.es6 => admin-api-keys.js.es6} | 0 .../admin/routes/admin-route-map.js.es6 | 12 ++++-- .../routes/admin-web-hooks-show-events.js.es6 | 2 +- .../admin/routes/admin-web-hooks-show.js.es6 | 2 +- .../javascripts/admin/templates/admin.hbs | 3 +- .../javascripts/admin/templates/api-keys.hbs | 34 +++++++++++++++ .../javascripts/admin/templates/api.hbs | 41 ++++--------------- app/controllers/admin/web_hooks_controller.rb | 2 +- config/locales/client.en.yml | 1 - config/routes.rb | 15 +++---- 17 files changed, 86 insertions(+), 53 deletions(-) create mode 100644 app/assets/javascripts/admin/adapters/web-hook-event.js.es6 create mode 100644 app/assets/javascripts/admin/adapters/web-hook.js.es6 rename app/assets/javascripts/admin/controllers/{admin-api.js.es6 => admin-api-keys.js.es6} (100%) create mode 100644 app/assets/javascripts/admin/routes/admin-api-index.js.es6 rename app/assets/javascripts/admin/routes/{admin-api.js.es6 => admin-api-keys.js.es6} (100%) create mode 100644 app/assets/javascripts/admin/templates/api-keys.hbs diff --git a/app/assets/javascripts/admin/adapters/web-hook-event.js.es6 b/app/assets/javascripts/admin/adapters/web-hook-event.js.es6 new file mode 100644 index 00000000000..122070ce3e4 --- /dev/null +++ b/app/assets/javascripts/admin/adapters/web-hook-event.js.es6 @@ -0,0 +1,7 @@ +import RESTAdapter from 'discourse/adapters/rest'; + +export default RESTAdapter.extend({ + basePath() { + return '/admin/api/'; + } +}); diff --git a/app/assets/javascripts/admin/adapters/web-hook.js.es6 b/app/assets/javascripts/admin/adapters/web-hook.js.es6 new file mode 100644 index 00000000000..122070ce3e4 --- /dev/null +++ b/app/assets/javascripts/admin/adapters/web-hook.js.es6 @@ -0,0 +1,7 @@ +import RESTAdapter from 'discourse/adapters/rest'; + +export default RESTAdapter.extend({ + basePath() { + return '/admin/api/'; + } +}); diff --git a/app/assets/javascripts/admin/components/admin-web-hook-event.js.es6 b/app/assets/javascripts/admin/components/admin-web-hook-event.js.es6 index b03c9a5f8ef..264e827da61 100644 --- a/app/assets/javascripts/admin/components/admin-web-hook-event.js.es6 +++ b/app/assets/javascripts/admin/components/admin-web-hook-event.js.es6 @@ -33,7 +33,7 @@ export default Ember.Component.extend({ redeliver() { return bootbox.confirm(I18n.t('admin.web_hooks.events.redeliver_confirm'), I18n.t('no_value'), I18n.t('yes_value'), result => { if (result) { - ajax(`/admin/web_hooks/${this.get('model.web_hook_id')}/events/${this.get('model.id')}/redeliver`, { type: 'POST' }).then(json => { + ajax(`/admin/api/web_hooks/${this.get('model.web_hook_id')}/events/${this.get('model.id')}/redeliver`, { type: 'POST' }).then(json => { this.set('model', json.web_hook_event); }).catch(popupAjaxError); } diff --git a/app/assets/javascripts/admin/controllers/admin-api.js.es6 b/app/assets/javascripts/admin/controllers/admin-api-keys.js.es6 similarity index 100% rename from app/assets/javascripts/admin/controllers/admin-api.js.es6 rename to app/assets/javascripts/admin/controllers/admin-api-keys.js.es6 diff --git a/app/assets/javascripts/admin/controllers/admin-web-hooks-show-events.js.es6 b/app/assets/javascripts/admin/controllers/admin-web-hooks-show-events.js.es6 index d91469d33df..6ef4411702e 100644 --- a/app/assets/javascripts/admin/controllers/admin-web-hooks-show-events.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-web-hooks-show-events.js.es6 @@ -41,7 +41,7 @@ export default Ember.Controller.extend({ ping() { this.set('pingDisabled', true); - ajax(`/admin/web_hooks/${this.get('model.extras.web_hook_id')}/ping`, { + ajax(`/admin/api/web_hooks/${this.get('model.extras.web_hook_id')}/ping`, { type: 'POST' }).catch(error => { this.set('pingDisabled', false); @@ -52,7 +52,7 @@ export default Ember.Controller.extend({ showInserted() { const webHookId = this.get('model.extras.web_hook_id'); - ajax(`/admin/web_hooks/${webHookId}/events/bulk`, { + ajax(`/admin/api/web_hooks/${webHookId}/events/bulk`, { type: 'GET', data: { ids: this.get('incomingEventIds') } }).then(data => { diff --git a/app/assets/javascripts/admin/models/api-key.js.es6 b/app/assets/javascripts/admin/models/api-key.js.es6 index aa05ce8341c..2a7cf867783 100644 --- a/app/assets/javascripts/admin/models/api-key.js.es6 +++ b/app/assets/javascripts/admin/models/api-key.js.es6 @@ -52,7 +52,7 @@ ApiKey.reopenClass({ @returns {Promise} a promise that resolves to the array of `ApiKey` instances **/ find: function() { - return ajax("/admin/api").then(function(keys) { + return ajax("/admin/api/keys").then(function(keys) { return keys.map(function (key) { return ApiKey.create(key); }); diff --git a/app/assets/javascripts/admin/routes/admin-api-index.js.es6 b/app/assets/javascripts/admin/routes/admin-api-index.js.es6 new file mode 100644 index 00000000000..f41f6bb7948 --- /dev/null +++ b/app/assets/javascripts/admin/routes/admin-api-index.js.es6 @@ -0,0 +1,5 @@ +export default Ember.Route.extend({ + beforeModel() { + this.transitionTo('adminApiKeys'); + } +}); diff --git a/app/assets/javascripts/admin/routes/admin-api.js.es6 b/app/assets/javascripts/admin/routes/admin-api-keys.js.es6 similarity index 100% rename from app/assets/javascripts/admin/routes/admin-api.js.es6 rename to app/assets/javascripts/admin/routes/admin-api-keys.js.es6 diff --git a/app/assets/javascripts/admin/routes/admin-route-map.js.es6 b/app/assets/javascripts/admin/routes/admin-route-map.js.es6 index df27e2c07be..69b51e5cabb 100644 --- a/app/assets/javascripts/admin/routes/admin-route-map.js.es6 +++ b/app/assets/javascripts/admin/routes/admin-route-map.js.es6 @@ -35,10 +35,14 @@ export default { this.route('edit', { path: '/:id' }); }); }); - this.route('api'); - this.resource('adminWebHooks', { path: '/web_hooks' }, function() { - this.route('show', { path: '/:web_hook_id' }); - this.route('showEvents', { path: '/:web_hook_id/events' }); + + this.resource('adminApi', { path: '/api' }, function() { + this.resource('adminApiKeys', { path: '/keys' }); + + this.resource('adminWebHooks', { path: '/web_hooks' }, function() { + this.route('show', { path: '/:web_hook_id' }); + this.route('showEvents', { path: '/:web_hook_id/events' }); + }); }); this.resource('admin.backups', { path: '/backups' }, function() { diff --git a/app/assets/javascripts/admin/routes/admin-web-hooks-show-events.js.es6 b/app/assets/javascripts/admin/routes/admin-web-hooks-show-events.js.es6 index 89841d5a7ba..c9700db463c 100644 --- a/app/assets/javascripts/admin/routes/admin-web-hooks-show-events.js.es6 +++ b/app/assets/javascripts/admin/routes/admin-web-hooks-show-events.js.es6 @@ -13,6 +13,6 @@ export default Discourse.Route.extend({ }, renderTemplate() { - this.render('admin/templates/web-hooks-show-events', { into: 'admin' }); + this.render('admin/templates/web-hooks-show-events', { into: 'adminApi' }); } }); diff --git a/app/assets/javascripts/admin/routes/admin-web-hooks-show.js.es6 b/app/assets/javascripts/admin/routes/admin-web-hooks-show.js.es6 index 533d7c09fe0..51b7d55d47c 100644 --- a/app/assets/javascripts/admin/routes/admin-web-hooks-show.js.es6 +++ b/app/assets/javascripts/admin/routes/admin-web-hooks-show.js.es6 @@ -21,6 +21,6 @@ export default Discourse.Route.extend({ }, renderTemplate() { - this.render('admin/templates/web-hooks-show', { into: 'admin' }); + this.render('admin/templates/web-hooks-show', { into: 'adminApi' }); } }); diff --git a/app/assets/javascripts/admin/templates/admin.hbs b/app/assets/javascripts/admin/templates/admin.hbs index 05076b7408e..e29125aec3d 100644 --- a/app/assets/javascripts/admin/templates/admin.hbs +++ b/app/assets/javascripts/admin/templates/admin.hbs @@ -19,8 +19,7 @@ {{nav-item route='adminLogs' label='admin.logs.title'}} {{#if currentUser.admin}} {{nav-item route='adminCustomize' label='admin.customize.title'}} - {{nav-item route='admin.api' label='admin.api.title'}} - {{nav-item route='adminWebHooks' label='admin.web_hooks.title'}} + {{nav-item route='adminApi' label='admin.api.title'}} {{nav-item route='admin.backups' label='admin.backups.title'}} {{/if}} {{nav-item route='adminPlugins' label='admin.plugins.title'}} diff --git a/app/assets/javascripts/admin/templates/api-keys.hbs b/app/assets/javascripts/admin/templates/api-keys.hbs new file mode 100644 index 00000000000..4123e576389 --- /dev/null +++ b/app/assets/javascripts/admin/templates/api-keys.hbs @@ -0,0 +1,34 @@ +{{#if model}} + + + + + + + {{#each model as |k|}} + + + + + + {{/each}} +
{{i18n 'admin.api.key'}}{{i18n 'admin.api.user'}} 
{{k.key}} + {{#if k.user}} + {{#link-to 'adminUser' k.user}} + {{avatar k.user imageSize="small"}} + {{/link-to}} + {{else}} + {{i18n 'admin.api.all_users'}} + {{/if}} + + {{d-button action="regenerateKey" actionParam=k icon="undo" label='admin.api.regenerate'}} + {{d-button action="revokeKey" actionParam=k icon="times" label='admin.api.revoke'}} +
+{{else}} +

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

+{{/if}} + +{{#unless hasMasterKey}} + +{{/unless }} + diff --git a/app/assets/javascripts/admin/templates/api.hbs b/app/assets/javascripts/admin/templates/api.hbs index 0f2e65b89c3..f3407fe0ccf 100644 --- a/app/assets/javascripts/admin/templates/api.hbs +++ b/app/assets/javascripts/admin/templates/api.hbs @@ -1,33 +1,10 @@ -{{#if model}} - - - - - - - {{#each model as |k|}} - - - - - - {{/each}} -
{{i18n 'admin.api.key'}}{{i18n 'admin.api.user'}} 
{{k.key}} - {{#if k.user}} - {{#link-to 'adminUser' k.user}} - {{avatar k.user imageSize="small"}} - {{/link-to}} - {{else}} - {{i18n 'admin.api.all_users'}} - {{/if}} - - - -
-{{else}} -

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

-{{/if}} +
+ {{#admin-nav}} + {{nav-item route='adminApiKeys' label='admin.api.title'}} + {{nav-item route='adminWebHooks' label='admin.web_hooks.title'}} + {{/admin-nav}} -{{#unless hasMasterKey}} - -{{/unless }} +
+ {{outlet}} +
+
diff --git a/app/controllers/admin/web_hooks_controller.rb b/app/controllers/admin/web_hooks_controller.rb index 3d7a134c44e..af984f6477d 100644 --- a/app/controllers/admin/web_hooks_controller.rb +++ b/app/controllers/admin/web_hooks_controller.rb @@ -63,7 +63,7 @@ class Admin::WebHooksController < Admin::AdminController json = { web_hook_events: serialize_data(@web_hook.web_hook_events.limit(limit).offset(offset), AdminWebHookEventSerializer), total_rows_web_hook_events: @web_hook.web_hook_events.count, - load_more_web_hook_events: admin_web_hook_events_path(limit: limit, offset: offset + limit, format: :json), + load_more_web_hook_events: web_hook_events_admin_api_index_path(limit: limit, offset: offset + limit, format: :json), extras: { web_hook_id: @web_hook.id } diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 53416d176d4..22b93e6b826 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2413,7 +2413,6 @@ en: flair_preview: "Preview" flair_note: "Note: Flair will only show for a user's primary group." - api: generate_master: "Generate Master API Key" none: "There are no active API keys right now." diff --git a/config/routes.rb b/config/routes.rb index d4a33a08053..77646512997 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -200,19 +200,20 @@ Discourse::Application.routes.draw do resources :api, only: [:index], constraints: AdminConstraint.new do collection do + get "keys" => "api#index" post "key" => "api#create_master_key" put "key" => "api#regenerate_key" delete "key" => "api#revoke_key" + + resources :web_hooks + get 'web_hook_events/:id' => 'web_hooks#list_events', as: :web_hook_events + get 'web_hooks/:id/events' => 'web_hooks#list_events' + get 'web_hooks/:id/events/bulk' => 'web_hooks#bulk_events' + post 'web_hooks/:web_hook_id/events/:event_id/redeliver' => 'web_hooks#redeliver_event' + post 'web_hooks/:id/ping' => 'web_hooks#ping' end end - resources :web_hooks, constraints: AdminConstraint.new - get 'web_hook_events/:id' => 'web_hooks#list_events', constraints: AdminConstraint.new, as: :web_hook_events - get 'web_hooks/:id/events' => 'web_hooks#list_events', constraints: AdminConstraint.new - get 'web_hooks/:id/events/bulk' => 'web_hooks#bulk_events', constraints: AdminConstraint.new - post 'web_hooks/:web_hook_id/events/:event_id/redeliver' => 'web_hooks#redeliver_event', constraints: AdminConstraint.new - post 'web_hooks/:id/ping' => 'web_hooks#ping', constraints: AdminConstraint.new - resources :backups, only: [:index, :create], constraints: AdminConstraint.new do member do get "" => "backups#show", constraints: { id: BACKUP_ROUTE_FORMAT }