2016-07-07 18:03:32 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
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.
|
|
|
|
*/
|
2016-05-12 12:30:01 +02:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('harbor.user')
|
|
|
|
.directive('toggleAdmin', toggleAdmin);
|
|
|
|
|
2016-07-02 12:20:31 +02:00
|
|
|
ToggleAdminController.$inject = ['$scope', 'ToggleAdminService', '$filter', 'trFilter'];
|
2016-05-12 12:30:01 +02:00
|
|
|
|
2016-07-02 12:20:31 +02:00
|
|
|
function ToggleAdminController($scope, ToggleAdminService, $filter, trFilter) {
|
2016-05-12 12:30:01 +02:00
|
|
|
var vm = this;
|
|
|
|
|
2016-07-28 05:22:16 +02:00
|
|
|
vm.isAdmin = (vm.hasAdminRole === 1);
|
2016-06-17 09:22:58 +02:00
|
|
|
vm.enabled = vm.isAdmin ? 0 : 1;
|
2016-05-12 12:30:01 +02:00
|
|
|
vm.toggle = toggle;
|
|
|
|
|
|
|
|
function toggle() {
|
2016-06-17 09:22:58 +02:00
|
|
|
ToggleAdminService(vm.userId, vm.enabled)
|
2016-05-12 12:30:01 +02:00
|
|
|
.success(toggleAdminSuccess)
|
|
|
|
.error(toggleAdminFailed);
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleAdminSuccess(data, status) {
|
|
|
|
if(vm.isAdmin) {
|
|
|
|
vm.isAdmin = false;
|
|
|
|
}else{
|
|
|
|
vm.isAdmin = true;
|
|
|
|
}
|
|
|
|
console.log('Toggled userId:' + vm.userId + ' to admin:' + vm.isAdmin);
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleAdminFailed(data, status) {
|
2016-07-02 12:20:31 +02:00
|
|
|
$scope.$emit('modalTitle', $filter('tr')('error'));
|
2016-07-04 06:34:34 +02:00
|
|
|
$scope.$emit('modalMessage', $filter('tr')('failed_to_toggle_admin'));
|
2016-07-02 12:20:31 +02:00
|
|
|
$scope.$emit('raiseError', true);
|
2016-07-06 06:56:44 +02:00
|
|
|
if(vm.isAdmin) {
|
|
|
|
vm.isAdmin = false;
|
|
|
|
}else{
|
|
|
|
vm.isAdmin = true;
|
|
|
|
}
|
2016-07-04 06:34:34 +02:00
|
|
|
console.log('Failed to toggle admin:' + data);
|
2016-05-12 12:30:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleAdmin() {
|
|
|
|
var directive = {
|
|
|
|
'restrict': 'E',
|
2016-06-16 08:10:35 +02:00
|
|
|
'templateUrl': '/static/resources/js/components/user/toggle-admin.directive.html',
|
2016-05-12 12:30:01 +02:00
|
|
|
'scope': {
|
|
|
|
'hasAdminRole': '=',
|
|
|
|
'userId': '@'
|
|
|
|
},
|
|
|
|
'link': link,
|
|
|
|
'controller': ToggleAdminController,
|
|
|
|
'controllerAs': 'vm',
|
|
|
|
'bindToController': true
|
|
|
|
};
|
|
|
|
return directive;
|
|
|
|
|
|
|
|
function link(scope, element, attrs, ctrl) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 12:38:51 +02:00
|
|
|
})();
|