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