Add Enable/Disable User action for Angular users panel

To Test
 - set 'users_panel' to True in settings.py

Change-Id: I980d11e3ac5a3ee14dc253dc69dd7aeb40578cc2
Implements: blueprint ng-users
This commit is contained in:
Shu Muto 2017-11-29 19:18:58 +09:00
parent c174036c84
commit b52f8d9138
5 changed files with 288 additions and 0 deletions

View File

@ -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,

View File

@ -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;
}
}
}
})();

View File

@ -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});
});
});
})();

View File

@ -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;
}
}
}
})();

View File

@ -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});
});
});
})();