2017-03-12 16:24:15 +00:00
|
|
|
angular.module('users', [])
|
2017-06-17 13:25:23 +00:00
|
|
|
.controller('UsersController', ['$q', '$scope', '$state', '$sanitize', 'UserService', 'TeamService', 'TeamMembershipService', 'ModalService', 'Notifications', 'Pagination', 'Authentication',
|
|
|
|
function ($q, $scope, $state, $sanitize, UserService, TeamService, TeamMembershipService, ModalService, Notifications, Pagination, Authentication) {
|
2017-03-12 16:24:15 +00:00
|
|
|
$scope.state = {
|
|
|
|
userCreationError: '',
|
|
|
|
selectedItemCount: 0,
|
|
|
|
validUsername: false,
|
|
|
|
pagination_count: Pagination.getPaginationCount('users')
|
|
|
|
};
|
|
|
|
$scope.sortType = 'RoleName';
|
|
|
|
$scope.sortReverse = false;
|
|
|
|
|
|
|
|
$scope.formValues = {
|
|
|
|
Username: '',
|
|
|
|
Password: '',
|
|
|
|
ConfirmPassword: '',
|
2017-03-27 12:44:39 +00:00
|
|
|
Administrator: false,
|
2017-05-23 18:56:10 +00:00
|
|
|
Teams: []
|
2017-03-12 16:24:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.order = function(sortType) {
|
|
|
|
$scope.sortReverse = ($scope.sortType === sortType) ? !$scope.sortReverse : false;
|
|
|
|
$scope.sortType = sortType;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.changePaginationCount = function() {
|
|
|
|
Pagination.setPaginationCount('endpoints', $scope.state.pagination_count);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.selectItems = function (allSelected) {
|
|
|
|
angular.forEach($scope.state.filteredUsers, function (user) {
|
|
|
|
if (user.Checked !== allSelected) {
|
|
|
|
user.Checked = allSelected;
|
|
|
|
$scope.selectItem(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.selectItem = function (item) {
|
|
|
|
if (item.Checked) {
|
|
|
|
$scope.state.selectedItemCount++;
|
|
|
|
} else {
|
|
|
|
$scope.state.selectedItemCount--;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.checkUsernameValidity = function() {
|
|
|
|
var valid = true;
|
|
|
|
for (var i = 0; i < $scope.users.length; i++) {
|
|
|
|
if ($scope.formValues.Username === $scope.users[i].Username) {
|
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$scope.state.validUsername = valid;
|
|
|
|
$scope.state.userCreationError = valid ? '' : 'Username already taken';
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.addUser = function() {
|
2017-05-23 18:56:10 +00:00
|
|
|
$('#createUserSpinner').show();
|
2017-03-12 16:24:15 +00:00
|
|
|
$scope.state.userCreationError = '';
|
2017-06-17 13:25:23 +00:00
|
|
|
var username = $sanitize($scope.formValues.Username);
|
|
|
|
var password = $sanitize($scope.formValues.Password);
|
2017-03-27 12:44:39 +00:00
|
|
|
var role = $scope.formValues.Administrator ? 1 : 2;
|
2017-05-23 18:56:10 +00:00
|
|
|
var teamIds = [];
|
|
|
|
angular.forEach($scope.formValues.Teams, function(team) {
|
|
|
|
teamIds.push(team.Id);
|
|
|
|
});
|
|
|
|
UserService.createUser(username, password, role, teamIds)
|
2017-03-12 16:24:15 +00:00
|
|
|
.then(function success(data) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.success('User successfully created', username);
|
2017-03-12 16:24:15 +00:00
|
|
|
$state.reload();
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', err, 'Unable to create user');
|
2017-03-12 16:24:15 +00:00
|
|
|
})
|
|
|
|
.finally(function final() {
|
2017-05-23 18:56:10 +00:00
|
|
|
$('#createUserSpinner').hide();
|
2017-03-12 16:24:15 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function deleteSelectedUsers() {
|
|
|
|
$('#loadUsersSpinner').show();
|
|
|
|
var counter = 0;
|
|
|
|
var complete = function () {
|
|
|
|
counter = counter - 1;
|
|
|
|
if (counter === 0) {
|
|
|
|
$('#loadUsersSpinner').hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
angular.forEach($scope.users, function (user) {
|
|
|
|
if (user.Checked) {
|
|
|
|
counter = counter + 1;
|
|
|
|
UserService.deleteUser(user.Id)
|
|
|
|
.then(function success(data) {
|
|
|
|
var index = $scope.users.indexOf(user);
|
|
|
|
$scope.users.splice(index, 1);
|
2017-04-12 19:47:22 +00:00
|
|
|
Notifications.success('User successfully deleted', user.Username);
|
2017-03-12 16:24:15 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', err, 'Unable to remove user');
|
2017-03-12 16:24:15 +00:00
|
|
|
})
|
|
|
|
.finally(function final() {
|
|
|
|
complete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.removeAction = function () {
|
|
|
|
ModalService.confirmDeletion(
|
2017-04-25 08:20:57 +00:00
|
|
|
'Do you want to remove the selected users? They will not be able to login into Portainer anymore.',
|
2017-03-12 16:24:15 +00:00
|
|
|
function onConfirm(confirmed) {
|
|
|
|
if(!confirmed) { return; }
|
|
|
|
deleteSelectedUsers();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
function assignTeamLeaders(users, memberships) {
|
|
|
|
for (var i = 0; i < users.length; i++) {
|
|
|
|
var user = users[i];
|
|
|
|
user.isTeamLeader = false;
|
|
|
|
for (var j = 0; j < memberships.length; j++) {
|
|
|
|
var membership = memberships[j];
|
|
|
|
if (user.Id === membership.UserId && membership.Role === 1) {
|
|
|
|
user.isTeamLeader = true;
|
|
|
|
user.RoleName = 'team leader';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function initView() {
|
2017-03-12 16:24:15 +00:00
|
|
|
$('#loadUsersSpinner').show();
|
2017-05-23 18:56:10 +00:00
|
|
|
var userDetails = Authentication.getUserDetails();
|
|
|
|
var isAdmin = userDetails.role === 1 ? true: false;
|
|
|
|
$scope.isAdmin = isAdmin;
|
|
|
|
$q.all({
|
|
|
|
users: UserService.users(true),
|
|
|
|
teams: isAdmin ? TeamService.teams() : UserService.userLeadingTeams(userDetails.ID),
|
|
|
|
memberships: TeamMembershipService.memberships()
|
|
|
|
})
|
2017-03-12 16:24:15 +00:00
|
|
|
.then(function success(data) {
|
2017-05-23 18:56:10 +00:00
|
|
|
var users = data.users;
|
|
|
|
assignTeamLeaders(users, data.memberships);
|
|
|
|
$scope.users = users;
|
|
|
|
$scope.teams = data.teams;
|
2017-03-12 16:24:15 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve users and teams');
|
2017-03-12 16:24:15 +00:00
|
|
|
$scope.users = [];
|
2017-05-23 18:56:10 +00:00
|
|
|
$scope.teams = [];
|
2017-03-12 16:24:15 +00:00
|
|
|
})
|
|
|
|
.finally(function final() {
|
|
|
|
$('#loadUsersSpinner').hide();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
initView();
|
2017-03-12 16:24:15 +00:00
|
|
|
}]);
|