diff --git a/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/actions.module.js b/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/actions.module.js index eff5a0d813..88d2a42a52 100644 --- a/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/actions.module.js +++ b/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/actions.module.js @@ -36,6 +36,8 @@ 'horizon.dashboard.identity.users.actions.create.service', 'horizon.dashboard.identity.users.actions.update.service', 'horizon.dashboard.identity.users.actions.password.service', + 'horizon.dashboard.identity.users.actions.enable.service', + 'horizon.dashboard.identity.users.actions.disable.service', 'horizon.dashboard.identity.users.actions.delete.service', 'horizon.dashboard.identity.users.resourceType' ]; @@ -45,6 +47,8 @@ createService, updateService, passwordService, + enableService, + disableService, deleteService, userResourceTypeCode ) { @@ -67,6 +71,22 @@ type: 'row' } }) + .append({ + id: 'enableAction', + service: enableService, + template: { + text: gettext('Enable User'), + type: 'row' + } + }) + .append({ + id: 'disableAction', + service: disableService, + template: { + text: gettext('Disable User'), + type: 'row' + } + }) .append({ id: 'deleteAction', service: deleteService, diff --git a/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/disable.action.service.js b/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/disable.action.service.js new file mode 100644 index 0000000000..16ee552936 --- /dev/null +++ b/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/disable.action.service.js @@ -0,0 +1,76 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +(function() { + 'use strict'; + + angular + .module('horizon.dashboard.identity.users') + .factory('horizon.dashboard.identity.users.actions.disable.service', disableService); + + disableService.$inject = [ + '$q', + 'horizon.dashboard.identity.users.resourceType', + 'horizon.app.core.openstack-service-api.keystone', + 'horizon.app.core.openstack-service-api.policy', + 'horizon.framework.util.actions.action-result.service', + 'horizon.framework.util.q.extensions', + 'horizon.framework.widgets.toast.service' + ]; + + /** + * @ngDoc factory + * @name horizon.dashboard.identity.users.actions.disable.service + * @Description A service to disable the user. + */ + function disableService( + $q, + resourceType, + keystone, + policy, + actionResultService, + $qExtensions, + toast + ) { + var message = { + success: gettext('User %s was successfully disabled.') + }; + + var service = { + allowed: allowed, + perform: perform + }; + + return service; + + ////////////// + + function allowed(selected) { + return $q.all([ + $qExtensions.booleanAsPromise(selected.enabled), + policy.ifAllowed({ rules: [['identity', 'identity:update_user']] }) + ]); + } + + function perform(selected) { + return keystone.editUser({id: selected.id, enabled: false}).then(success); + function success() { + toast.add('success', interpolate(message.success, [selected.name])); + return actionResultService.getActionResult() + .updated(resourceType, selected.id) + .result; + } + } + } +})(); diff --git a/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/disable.action.service.spec.js b/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/disable.action.service.spec.js new file mode 100644 index 0000000000..131b0583e8 --- /dev/null +++ b/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/disable.action.service.spec.js @@ -0,0 +1,58 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +(function() { + 'use strict'; + + describe('horizon.dashboard.identity.users.actions.disable.service', function() { + + var $q, $scope, service, keystone, policy; + var selected = { + id: '1', + enabled: true + }; + + beforeEach(module('horizon.app.core')); + beforeEach(module('horizon.framework')); + beforeEach(module('horizon.dashboard.identity.users')); + + beforeEach(inject(function($injector, _$rootScope_, _$q_) { + $scope = _$rootScope_.$new(); + $q = _$q_; + service = $injector.get('horizon.dashboard.identity.users.actions.disable.service'); + keystone = $injector.get('horizon.app.core.openstack-service-api.keystone'); + var deferred = $q.defer(); + spyOn(keystone, 'editUser').and.returnValue(deferred.promise); + deferred.resolve({}); + policy = $injector.get('horizon.app.core.openstack-service-api.policy'); + var allowedPromise = $q.defer(); + spyOn(policy, 'ifAllowed').and.returnValue(allowedPromise.promise); + allowedPromise.resolve({allowed: true}); + })); + + it('should check the policy', function() { + var allowed = service.allowed(selected); + $scope.$apply(); + + expect(allowed).toBeTruthy(); + }); + + it('should call keystone.editUser', function() { + service.perform(selected); + $scope.$apply(); + + expect(keystone.editUser).toHaveBeenCalledWith({id: selected.id, enabled: false}); + }); + }); +})(); diff --git a/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/enable.action.service.js b/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/enable.action.service.js new file mode 100644 index 0000000000..61a299bce9 --- /dev/null +++ b/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/enable.action.service.js @@ -0,0 +1,76 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +(function() { + 'use strict'; + + angular + .module('horizon.dashboard.identity.users') + .factory('horizon.dashboard.identity.users.actions.enable.service', enableService); + + enableService.$inject = [ + '$q', + 'horizon.dashboard.identity.users.resourceType', + 'horizon.app.core.openstack-service-api.keystone', + 'horizon.app.core.openstack-service-api.policy', + 'horizon.framework.util.actions.action-result.service', + 'horizon.framework.util.q.extensions', + 'horizon.framework.widgets.toast.service' + ]; + + /** + * @ngDoc factory + * @name horizon.dashboard.identity.users.actions.enable.service + * @Description A service to enable the user. + */ + function enableService( + $q, + resourceType, + keystone, + policy, + actionResultService, + $qExtensions, + toast + ) { + var message = { + success: gettext('User %s was successfully enabled.') + }; + + var service = { + allowed: allowed, + perform: perform + }; + + return service; + + ////////////// + + function allowed(selected) { + return $q.all([ + $qExtensions.booleanAsPromise(!selected.enabled), + policy.ifAllowed({ rules: [['identity', 'identity:update_user']] }) + ]); + } + + function perform(selected) { + return keystone.editUser({id: selected.id, enabled: true}).then(success); + function success() { + toast.add('success', interpolate(message.success, [selected.name])); + return actionResultService.getActionResult() + .updated(resourceType, selected.id) + .result; + } + } + } +})(); diff --git a/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/enable.action.service.spec.js b/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/enable.action.service.spec.js new file mode 100644 index 0000000000..e51a9cf386 --- /dev/null +++ b/openstack_dashboard/dashboards/identity/static/dashboard/identity/users/actions/enable.action.service.spec.js @@ -0,0 +1,58 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +(function() { + 'use strict'; + + describe('horizon.dashboard.identity.users.actions.enable.service', function() { + + var $q, $scope, service, keystone, policy; + var selected = { + id: '1', + enabled: false + }; + + beforeEach(module('horizon.app.core')); + beforeEach(module('horizon.framework')); + beforeEach(module('horizon.dashboard.identity.users')); + + beforeEach(inject(function($injector, _$rootScope_, _$q_) { + $scope = _$rootScope_.$new(); + $q = _$q_; + service = $injector.get('horizon.dashboard.identity.users.actions.enable.service'); + keystone = $injector.get('horizon.app.core.openstack-service-api.keystone'); + var deferred = $q.defer(); + spyOn(keystone, 'editUser').and.returnValue(deferred.promise); + deferred.resolve({}); + policy = $injector.get('horizon.app.core.openstack-service-api.policy'); + var allowedPromise = $q.defer(); + spyOn(policy, 'ifAllowed').and.returnValue(allowedPromise.promise); + allowedPromise.resolve({allowed: true}); + })); + + it('should check the policy', function() { + var allowed = service.allowed(selected); + $scope.$apply(); + + expect(allowed).toBeTruthy(); + }); + + it('should call keystone.editUser', function() { + service.perform(selected); + $scope.$apply(); + + expect(keystone.editUser).toHaveBeenCalledWith({id: selected.id, enabled: true}); + }); + }); +})();