diff --git a/awx/ui/client/features/users/tokens/tokens.strings.js b/awx/ui/client/features/users/tokens/tokens.strings.js index 27b9450992..2f84642eae 100644 --- a/awx/ui/client/features/users/tokens/tokens.strings.js +++ b/awx/ui/client/features/users/tokens/tokens.strings.js @@ -28,7 +28,8 @@ function TokensStrings (BaseString) { DELETE_ACTION_LABEL: t.s('DELETE'), SCOPE_PLACEHOLDER: t.s('Select a scope'), SCOPE_READ_LABEL: t.s('Read'), - SCOPE_WRITE_LABEL: t.s('Write') + SCOPE_WRITE_LABEL: t.s('Write'), + APPLICATION_HELP_TEXT: t.s('Leaving this field blank will result in the creation of a Personal Access Token which is not linked to an Application.') }; ns.list = { @@ -37,6 +38,7 @@ function TokensStrings (BaseString) { ROW_ITEM_LABEL_USED: t.s('LAST USED'), ROW_ITEM_LABEL_SCOPE: t.s('SCOPE'), ROW_ITEM_LABEL_APPLICATION: t.s('APPLICATION'), + PERSONAL_ACCESS_TOKEN: t.s('Personal Access Token'), HEADER: appName => t.s('{{ appName }} Token', { appName }), }; } diff --git a/awx/ui/client/features/users/tokens/users-tokens-add.controller.js b/awx/ui/client/features/users/tokens/users-tokens-add.controller.js index 26ce20e5f2..4ad1fa2d27 100644 --- a/awx/ui/client/features/users/tokens/users-tokens-add.controller.js +++ b/awx/ui/client/features/users/tokens/users-tokens-add.controller.js @@ -1,59 +1,72 @@ function AddTokensController ( - models, $state, strings, Rest, Alert, Wait, GetBasePath, + models, $state, strings, Alert, Wait, $filter, ProcessErrors, $scope ) { const vm = this || {}; - const { application } = models; + const { application, token, user } = models; vm.mode = 'add'; vm.strings = strings; vm.panelTitle = strings.get('add.PANEL_TITLE'); - vm.form = {}; - - vm.form.application = { - type: 'field', - label: 'Application', - id: 'application' - }; - vm.form.description = { - type: 'String', - label: 'Description', - id: 'description' - }; - - vm.form.application._resource = 'application'; - vm.form.application._route = 'users.edit.tokens.add.application'; - vm.form.application._model = application; - vm.form.application._placeholder = strings.get('add.APP_PLACEHOLDER'); - vm.form.application.required = true; - - vm.form.description.required = false; - - vm.form.scope = { - choices: [ - [null, ''], - ['read', strings.get('add.SCOPE_READ_LABEL')], - ['write', strings.get('add.SCOPE_WRITE_LABEL')] - ], - help_text: strings.get('add.SCOPE_HELP_TEXT'), - id: 'scope', - label: 'Scope', - required: true, - _component: 'at-input-select', - _data: [ - [null, ''], - ['read', strings.get('add.SCOPE_READ_LABEL')], - ['write', strings.get('add.SCOPE_WRITE_LABEL')] - ], - _exp: 'choice[1] for (index, choice) in state._data', - _format: 'selectFromOptions' + vm.form = { + application: { + type: 'field', + label: 'Application', + id: 'application', + required: false, + help_text: strings.get('add.APPLICATION_HELP_TEXT'), + _resource: 'application', + _route: 'users.edit.tokens.add.application', + _model: application, + _placeholder: strings.get('add.APP_PLACEHOLDER') + }, + description: { + type: 'String', + label: 'Description', + id: 'description', + required: false + }, + scope: { + choices: [ + [null, ''], + ['read', strings.get('add.SCOPE_READ_LABEL')], + ['write', strings.get('add.SCOPE_WRITE_LABEL')] + ], + help_text: strings.get('add.SCOPE_HELP_TEXT'), + id: 'scope', + label: 'Scope', + required: true, + _component: 'at-input-select', + _data: [ + [null, ''], + ['read', strings.get('add.SCOPE_READ_LABEL')], + ['write', strings.get('add.SCOPE_WRITE_LABEL')] + ], + _exp: 'choice[1] for (index, choice) in state._data', + _format: 'selectFromOptions' + } }; vm.form.save = payload => { - Rest.setUrl(`${GetBasePath('users')}${$state.params.user_id}/authorized_tokens`); - return Rest.post(payload) + const postToken = _.has(payload, 'application') ? + user.postAuthorizedTokens({ + id: $state.params.user_id, + payload + }) : token.request('post', { data: payload }); + + return postToken .then(({ data }) => { + const refreshHTML = data.refresh_token ? + `
+
+ ${strings.get('add.REFRESH_TOKEN_LABEL')} +
+
+ ${data.refresh_token} +
+
` : ''; + Alert(strings.get('add.TOKEN_MODAL_HEADER'), `
@@ -63,14 +76,7 @@ function AddTokensController ( ${data.token}
-
-
- ${strings.get('add.REFRESH_TOKEN_LABEL')} -
-
- ${data.refresh_token} -
-
+ ${refreshHTML}
${strings.get('add.TOKEN_EXPIRES_LABEL')} @@ -106,10 +112,8 @@ AddTokensController.$inject = [ 'resolvedModels', '$state', 'TokensStrings', - 'Rest', 'Alert', 'Wait', - 'GetBasePath', '$filter', 'ProcessErrors', '$scope' diff --git a/awx/ui/client/features/users/tokens/users-tokens-add.route.js b/awx/ui/client/features/users/tokens/users-tokens-add.route.js index fe9548b79a..43a663433f 100644 --- a/awx/ui/client/features/users/tokens/users-tokens-add.route.js +++ b/awx/ui/client/features/users/tokens/users-tokens-add.route.js @@ -3,17 +3,21 @@ import AddController from './users-tokens-add.controller'; const addTemplate = require('~features/users/tokens/users-tokens-add.partial.html'); -function TokensDetailResolve ($q, Application) { +function TokensDetailResolve ($q, Application, Token, User) { const promises = {}; promises.application = new Application('options'); + promises.token = new Token('options'); + promises.user = new User('options'); return $q.all(promises); } TokensDetailResolve.$inject = [ '$q', - 'ApplicationModel' + 'ApplicationModel', + 'TokenModel', + 'UserModel' ]; function isMeResolve ($rootScope, $stateParams, $state) { diff --git a/awx/ui/client/features/users/tokens/users-tokens-list.controller.js b/awx/ui/client/features/users/tokens/users-tokens-list.controller.js index 056a77792a..4dde61c441 100644 --- a/awx/ui/client/features/users/tokens/users-tokens-list.controller.js +++ b/awx/ui/client/features/users/tokens/users-tokens-list.controller.js @@ -10,13 +10,15 @@ function ListTokensController ( Dataset, strings, ProcessErrors, - Rest, GetBasePath, Prompt, - Wait + Wait, + models ) { const vm = this || {}; + const { token } = models; + vm.strings = strings; vm.activeId = $state.params.token_id; @@ -48,8 +50,8 @@ function ListTokensController ( return undefined; }; - vm.getLastUsed = token => { - const lastUsed = _.get(token, 'last_used'); + vm.getLastUsed = tokenToCheck => { + const lastUsed = _.get(tokenToCheck, 'last_used'); if (!lastUsed) { return undefined; @@ -57,7 +59,7 @@ function ListTokensController ( let html = $filter('longDate')(lastUsed); - const { username, id } = _.get(token, 'summary_fields.last_used', {}); + const { username, id } = _.get(tokenToCheck, 'summary_fields.last_used', {}); if (username && id) { html += ` ${strings.get('add.LAST_USED_LABEL')} ${$filter('sanitize')(username)}`; @@ -70,8 +72,7 @@ function ListTokensController ( const action = () => { $('#prompt-modal').modal('hide'); Wait('start'); - Rest.setUrl(`${GetBasePath('tokens')}${tok.id}`); - Rest.destroy() + token.request('delete', tok.id) .then(() => { let reloadListStateParams = null; @@ -89,14 +90,12 @@ function ListTokensController ( } else { $state.go('.', reloadListStateParams, { reload: true }); } - }) - .catch(({ data, status }) => { + }).catch(({ data, status }) => { ProcessErrors($scope, data, status, null, { hdr: strings.get('error.HEADER'), msg: strings.get('error.CALL', { path: `${GetBasePath('tokens')}${tok.id}`, status }) }); - }) - .finally(() => { + }).finally(() => { Wait('stop'); }); }; @@ -105,7 +104,9 @@ function ListTokensController ( Prompt({ hdr: strings.get('deleteResource.HEADER'), - resourceName: strings.get('list.HEADER', tok.summary_fields.application.name), + resourceName: _.has(tok, 'summary_fields.application.name') ? + strings.get('list.HEADER', tok.summary_fields.application.name) : + strings.get('list.PERSONAL_ACCESS_TOKEN'), body: deleteModalBody, action, actionText: strings.get('add.DELETE_ACTION_LABEL') @@ -120,10 +121,10 @@ ListTokensController.$inject = [ 'Dataset', 'TokensStrings', 'ProcessErrors', - 'Rest', 'GetBasePath', 'Prompt', - 'Wait' + 'Wait', + 'resolvedModels' ]; export default ListTokensController; diff --git a/awx/ui/client/features/users/tokens/users-tokens-list.partial.html b/awx/ui/client/features/users/tokens/users-tokens-list.partial.html index 01e1f624f6..9018b6a7b8 100644 --- a/awx/ui/client/features/users/tokens/users-tokens-list.partial.html +++ b/awx/ui/client/features/users/tokens/users-tokens-list.partial.html @@ -25,7 +25,10 @@
+ header-value="{{ token.summary_fields.application.name ? + vm.strings.get('list.HEADER', token.summary_fields.application.name) : + vm.strings.get('list.PERSONAL_ACCESS_TOKEN') + }}">