blob: b76d8120ae41d7f8ee62e69125156426ad3c616a [file] [log] [blame]
/**
* Controller for user Accounts
*
* @module app/users
* @exports userAccountsController
* @name userAccountsController
*/
window.angular && (function(angular) {
'use strict';
angular.module('app.users').controller('userAccountsController', [
'$scope', '$q', 'APIUtils',
function($scope, $q, APIUtils) {
$scope.users = [];
$scope.roles = [];
$scope.state = 'none';
$scope.outMsg = '';
$scope.loading = true;
function loadUserInfo() {
$scope.loading = true;
$scope.isUserSelected = false;
$scope.selectedUser = null;
$scope.togglePassword = false;
$scope.toggleVerify = false;
$q.all([
APIUtils.getAllUserAccounts().then(
function(res) {
$scope.users = res;
},
function(error) {
console.log(JSON.stringify(error));
}),
APIUtils.getAccountServiceRoles().then(
function(res) {
$scope.roles = res;
},
function(error) {
console.log(JSON.stringify(error));
})
]).finally(function() {
$scope.loading = false;
});
};
$scope.cancel = function() {
$scope.state = 'none';
$scope.outMsg = '';
loadUserInfo();
};
$scope.setSelectedUser = function(user) {
$scope.state = 'none';
$scope.outMsg = '';
$scope.isUserSelected = true;
$scope.selectedUser = angular.copy(user);
$scope.selectedUser.VerifyPassword = null;
// Used while renaming the user.
$scope.selectedUser.CurrentUserName = $scope.selectedUser.UserName;
};
$scope.createNewUser = function() {
$scope.state = 'none';
$scope.outMsg = '';
if (!$scope.selectedUser.UserName || !$scope.selectedUser.Password) {
$scope.state = 'error';
$scope.outMsg = 'Username or Password can\'t be empty';
return;
}
if ($scope.selectedUser.Password !==
$scope.selectedUser.VerifyPassword) {
$scope.state = 'error';
$scope.outMsg = 'Passwords do not match';
return;
}
var user = $scope.selectedUser.UserName;
var passwd = $scope.selectedUser.Password;
var role = $scope.selectedUser.RoleId;
var enabled = false;
if ($scope.selectedUser.Enabled != null) {
enabled = $scope.selectedUser.Enabled;
}
$scope.loading = true;
APIUtils.createUser(user, passwd, role, enabled)
.then(
function(response) {
$scope.state = 'success';
$scope.outMsg = 'User has been created successfully';
},
function(error) {
$scope.state = 'error';
$scope.outMsg = 'Failed to create new user';
})
.finally(function() {
loadUserInfo();
$scope.loading = false;
});
};
$scope.updateUserInfo = function() {
$scope.state = 'none';
$scope.outMsg = '';
if ($scope.selectedUser.Password !==
$scope.selectedUser.VerifyPassword) {
$scope.state = 'error';
$scope.outMsg = 'Passwords do not match';
return;
}
var data = {};
if ($scope.selectedUser.UserName !==
$scope.selectedUser.CurrentUserName) {
data['UserName'] = $scope.selectedUser.UserName;
}
$scope.selectedUser.VerifyPassword = null;
if ($scope.selectedUser.Password != null) {
data['Password'] = $scope.selectedUser.Password;
}
data['RoleId'] = $scope.selectedUser.RoleId;
data['Enabled'] = $scope.selectedUser.Enabled;
$scope.loading = true;
APIUtils
.updateUser(
$scope.selectedUser.CurrentUserName, data['UserName'],
data['Password'], data['RoleId'], data['Enabled'])
.then(
function(response) {
$scope.state = 'success';
$scope.outMsg = 'User has been updated successfully';
},
function(error) {
$scope.state = 'error';
$scope.outMsg = 'Updating user failed';
})
.finally(function() {
loadUserInfo();
$scope.loading = false;
});
};
$scope.deleteUser = function(userName) {
$scope.state = 'none';
$scope.outMsg = '';
$scope.loading = true;
APIUtils.deleteUser(userName)
.then(
function(response) {
$scope.state = 'success';
$scope.outMsg = 'User has been deleted successfully';
},
function(error) {
$scope.state = 'error';
$scope.outMsg = 'Deleting user failed';
})
.finally(function() {
loadUserInfo();
$scope.loading = false;
});
};
loadUserInfo();
}
]);
})(angular);