Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for user Accounts |
| 3 | * |
| 4 | * @module app/users |
| 5 | * @exports userAccountsController |
| 6 | * @name userAccountsController |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 9 | window.angular && (function(angular) { |
| 10 | 'use strict'; |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 11 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 12 | angular.module('app.users').controller('userAccountsController', [ |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 13 | '$scope', '$q', 'APIUtils', |
| 14 | function($scope, $q, APIUtils) { |
| 15 | $scope.users = []; |
| 16 | $scope.roles = []; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 17 | $scope.state = 'none'; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 18 | $scope.outMsg = ''; |
| 19 | $scope.loading = true; |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 20 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 21 | function loadUserInfo() { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 22 | $scope.loading = true; |
| 23 | $scope.isUserSelected = false; |
| 24 | $scope.selectedUser = null; |
AppaRao Puli | a83cd05 | 2019-01-07 23:25:43 +0530 | [diff] [blame^] | 25 | $scope.togglePassword = false; |
| 26 | $scope.toggleVerify = false; |
| 27 | |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 28 | $q.all([ |
| 29 | APIUtils.getAllUserAccounts().then( |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 30 | function(res) { |
| 31 | $scope.users = res; |
| 32 | }, |
| 33 | function(error) { |
| 34 | console.log(JSON.stringify(error)); |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 35 | }), |
| 36 | APIUtils.getAccountServiceRoles().then( |
| 37 | function(res) { |
| 38 | $scope.roles = res; |
| 39 | }, |
| 40 | function(error) { |
| 41 | console.log(JSON.stringify(error)); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 42 | }) |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 43 | ]).finally(function() { |
| 44 | $scope.loading = false; |
| 45 | }); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 46 | }; |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 47 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 48 | $scope.cancel = function() { |
| 49 | $scope.state = 'none'; |
| 50 | $scope.outMsg = ''; |
| 51 | loadUserInfo(); |
| 52 | }; |
| 53 | $scope.setSelectedUser = function(user) { |
| 54 | $scope.state = 'none'; |
| 55 | $scope.outMsg = ''; |
| 56 | |
| 57 | $scope.isUserSelected = true; |
| 58 | $scope.selectedUser = angular.copy(user); |
| 59 | $scope.selectedUser.VerifyPassword = null; |
| 60 | // Used while renaming the user. |
| 61 | $scope.selectedUser.CurrentUserName = $scope.selectedUser.UserName; |
| 62 | }; |
| 63 | $scope.createNewUser = function() { |
| 64 | $scope.state = 'none'; |
| 65 | $scope.outMsg = ''; |
| 66 | |
| 67 | if (!$scope.selectedUser.UserName || !$scope.selectedUser.Password) { |
| 68 | $scope.state = 'error'; |
| 69 | $scope.outMsg = 'Username or Password can\'t be empty'; |
| 70 | return; |
| 71 | } |
| 72 | if ($scope.selectedUser.Password !== |
| 73 | $scope.selectedUser.VerifyPassword) { |
| 74 | $scope.state = 'error'; |
| 75 | $scope.outMsg = 'Passwords do not match'; |
| 76 | return; |
| 77 | } |
| 78 | var user = $scope.selectedUser.UserName; |
| 79 | var passwd = $scope.selectedUser.Password; |
| 80 | var role = $scope.selectedUser.RoleId; |
| 81 | var enabled = false; |
| 82 | if ($scope.selectedUser.Enabled != null) { |
| 83 | enabled = $scope.selectedUser.Enabled; |
| 84 | } |
| 85 | |
| 86 | $scope.loading = true; |
| 87 | APIUtils.createUser(user, passwd, role, enabled) |
| 88 | .then( |
| 89 | function(response) { |
| 90 | $scope.state = 'success'; |
| 91 | $scope.outMsg = 'User has been created successfully'; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 92 | }, |
| 93 | function(error) { |
| 94 | $scope.state = 'error'; |
AppaRao Puli | a83cd05 | 2019-01-07 23:25:43 +0530 | [diff] [blame^] | 95 | $scope.outMsg = 'Failed to create new user'; |
Gunnar Mills | b3d48d4 | 2018-05-25 08:34:35 -0500 | [diff] [blame] | 96 | }) |
| 97 | .finally(function() { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 98 | loadUserInfo(); |
| 99 | $scope.loading = false; |
Gunnar Mills | b3d48d4 | 2018-05-25 08:34:35 -0500 | [diff] [blame] | 100 | }); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 101 | }; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 102 | $scope.updateUserInfo = function() { |
| 103 | $scope.state = 'none'; |
| 104 | $scope.outMsg = ''; |
| 105 | if ($scope.selectedUser.Password !== |
| 106 | $scope.selectedUser.VerifyPassword) { |
| 107 | $scope.state = 'error'; |
| 108 | $scope.outMsg = 'Passwords do not match'; |
| 109 | return; |
| 110 | } |
| 111 | var data = {}; |
| 112 | if ($scope.selectedUser.UserName !== |
| 113 | $scope.selectedUser.CurrentUserName) { |
| 114 | data['UserName'] = $scope.selectedUser.UserName; |
| 115 | } |
| 116 | $scope.selectedUser.VerifyPassword = null; |
| 117 | if ($scope.selectedUser.Password != null) { |
| 118 | data['Password'] = $scope.selectedUser.Password; |
| 119 | } |
| 120 | data['RoleId'] = $scope.selectedUser.RoleId; |
| 121 | data['Enabled'] = $scope.selectedUser.Enabled; |
| 122 | |
| 123 | $scope.loading = true; |
| 124 | APIUtils |
| 125 | .updateUser( |
| 126 | $scope.selectedUser.CurrentUserName, data['UserName'], |
| 127 | data['Password'], data['RoleId'], data['Enabled']) |
| 128 | .then( |
| 129 | function(response) { |
| 130 | $scope.state = 'success'; |
| 131 | $scope.outMsg = 'User has been updated successfully'; |
| 132 | }, |
| 133 | function(error) { |
| 134 | $scope.state = 'error'; |
AppaRao Puli | a83cd05 | 2019-01-07 23:25:43 +0530 | [diff] [blame^] | 135 | $scope.outMsg = 'Updating user failed'; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 136 | }) |
| 137 | .finally(function() { |
| 138 | loadUserInfo(); |
| 139 | $scope.loading = false; |
| 140 | }); |
| 141 | }; |
| 142 | $scope.deleteUser = function(userName) { |
| 143 | $scope.state = 'none'; |
| 144 | $scope.outMsg = ''; |
| 145 | |
| 146 | $scope.loading = true; |
| 147 | APIUtils.deleteUser(userName) |
| 148 | .then( |
| 149 | function(response) { |
| 150 | $scope.state = 'success'; |
| 151 | $scope.outMsg = 'User has been deleted successfully'; |
| 152 | }, |
| 153 | function(error) { |
| 154 | $scope.state = 'error'; |
AppaRao Puli | a83cd05 | 2019-01-07 23:25:43 +0530 | [diff] [blame^] | 155 | $scope.outMsg = 'Deleting user failed'; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 156 | }) |
| 157 | .finally(function() { |
| 158 | loadUserInfo(); |
| 159 | $scope.loading = false; |
| 160 | }); |
| 161 | }; |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 162 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 163 | loadUserInfo(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 164 | } |
| 165 | ]); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 166 | })(angular); |