From 6239df6778ae89e7ee2274894fe1f41effb3330e Mon Sep 17 00:00:00 2001 From: gconsidine Date: Thu, 27 Jul 2017 18:07:06 -0400 Subject: [PATCH] Add read-only credential form depending on access --- .../client/features/credentials/_index.less | 2 +- .../credentials/add-credentials.controller.js | 2 + .../edit-credentials.controller.js | 4 +- .../lib/components/form/action.partial.html | 2 +- .../lib/components/form/form.directive.js | 2 + .../lib/components/input/lookup.directive.js | 2 + awx/ui/client/lib/models/Base.js | 46 +++++++++++++++++++ awx/ui/client/lib/models/Credential.js | 11 +++-- 8 files changed, 64 insertions(+), 7 deletions(-) diff --git a/awx/ui/client/features/credentials/_index.less b/awx/ui/client/features/credentials/_index.less index 4f4f37cd91..87f746b2c3 100644 --- a/awx/ui/client/features/credentials/_index.less +++ b/awx/ui/client/features/credentials/_index.less @@ -1,3 +1,3 @@ .at-CredentialsPermissions { - margin-top: 20px; + margin-top: 50px; } diff --git a/awx/ui/client/features/credentials/add-credentials.controller.js b/awx/ui/client/features/credentials/add-credentials.controller.js index e1f925a60b..c991f62b0f 100644 --- a/awx/ui/client/features/credentials/add-credentials.controller.js +++ b/awx/ui/client/features/credentials/add-credentials.controller.js @@ -19,6 +19,8 @@ function AddCredentialsController (models, $state, strings) { omit: ['user', 'team', 'inputs'] }); + vm.form.disabled = !credential.isCreatable(); + vm.form.organization._resource = 'organization'; vm.form.organization._route = 'credentials.add.organization'; vm.form.organization._model = organization; diff --git a/awx/ui/client/features/credentials/edit-credentials.controller.js b/awx/ui/client/features/credentials/edit-credentials.controller.js index 8c2c79626a..602972ff73 100644 --- a/awx/ui/client/features/credentials/edit-credentials.controller.js +++ b/awx/ui/client/features/credentials/edit-credentials.controller.js @@ -35,10 +35,12 @@ function EditCredentialsController (models, $state, $scope, strings) { // Only exists for permissions compatibility $scope.credential_obj = credential.get(); - vm.form = credential.createFormSchema('put', { + vm.form = credential.createFormSchema({ omit: ['user', 'team', 'inputs'] }); + vm.form.disabled = !credential.isEditable(); + vm.form.organization._resource = 'organization'; vm.form.organization._model = organization; vm.form.organization._route = 'credentials.edit.organization'; diff --git a/awx/ui/client/lib/components/form/action.partial.html b/awx/ui/client/lib/components/form/action.partial.html index 8affd3a414..245c649de1 100644 --- a/awx/ui/client/lib/components/form/action.partial.html +++ b/awx/ui/client/lib/components/form/action.partial.html @@ -1,5 +1,5 @@ diff --git a/awx/ui/client/lib/components/form/form.directive.js b/awx/ui/client/lib/components/form/form.directive.js index 7d7aa2e30d..e8b80898aa 100644 --- a/awx/ui/client/lib/components/form/form.directive.js +++ b/awx/ui/client/lib/components/form/form.directive.js @@ -27,6 +27,8 @@ function AtFormController (eventService, strings) { form = _form_; modal = scope[scope.ns].modal; + vm.state.disabled = scope.state.disabled; + vm.setListeners(); }; diff --git a/awx/ui/client/lib/components/input/lookup.directive.js b/awx/ui/client/lib/components/input/lookup.directive.js index 792709035b..58ab894737 100644 --- a/awx/ui/client/lib/components/input/lookup.directive.js +++ b/awx/ui/client/lib/components/input/lookup.directive.js @@ -79,6 +79,8 @@ function AtInputLookupController (baseInputController, $q, $state, $stateParams) }; vm.search = () => { + scope.state._touched = true; + return model.search({ [search.key]: scope.state._displayValue }, search.config) .then(found => { if (!found) { diff --git a/awx/ui/client/lib/models/Base.js b/awx/ui/client/lib/models/Base.js index fea1fbd413..4fa46e50a0 100644 --- a/awx/ui/client/lib/models/Base.js +++ b/awx/ui/client/lib/models/Base.js @@ -217,12 +217,55 @@ function find (method, keys) { return value; } +function has (method, keys) { + if (!keys) { + keys = method; + method = 'GET'; + } + + method = method.toUpperCase(); + + let value; + switch (method) { + case 'OPTIONS': + value = this.options(keys); + break; + default: + value = this.get(keys); + } + + return value !== undefined && value !== null; +} + function normalizePath (resource) { let version = '/api/v2/'; return `${version}${resource}/`; } +function isEditable () { + let canEdit = this.get('summary_fields.user_capabilities.edit'); + + if (canEdit) { + return true; + } + + if (this.has('options', 'actions.PUT')) { + return true; + } + + return false; + +} + +function isCreatable () { + if (this.has('options', 'actions.POST')) { + return true; + } + + return false; +} + function graft (id) { let item = this.get('results').filter(result => result.id === id); @@ -255,6 +298,9 @@ function BaseModel (path) { this.find = find; this.get = get; this.graft = graft; + this.has = has; + this.isEditable = isEditable; + this.isCreatable = isCreatable; this.match = match; this.model = {}; this.normalizePath = normalizePath; diff --git a/awx/ui/client/lib/models/Credential.js b/awx/ui/client/lib/models/Credential.js index 0599513b84..06b824b410 100644 --- a/awx/ui/client/lib/models/Credential.js +++ b/awx/ui/client/lib/models/Credential.js @@ -3,18 +3,21 @@ const ENCRYPTED_VALUE = '$encrypted$'; let BaseModel; function createFormSchema (method, config) { + if (!config) { + config = method; + method = 'GET'; + } + let schema = Object.assign({}, this.options(`actions.${method.toUpperCase()}`)); if (config && config.omit) { - config.omit.forEach(key => { - delete schema[key]; - }); + config.omit.forEach(key => delete schema[key]); } for (let key in schema) { schema[key].id = key; - if (method === 'put') { + if (this.has(key)) { schema[key]._value = this.get(key); } }