blob: 2cccbd7938309700a0c234cb5f2c277400ec8592 [file] [log] [blame]
Iftekharul Islamcd789502017-04-19 14:37:55 -05001/**
2 * Controller for user Accounts
3 *
4 * @module app/users
5 * @exports userAccountsController
6 * @name userAccountsController
Iftekharul Islamcd789502017-04-19 14:37:55 -05007 */
8
9window.angular && (function (angular) {
10 'use strict';
11
12 angular
13 .module('app.users')
14 .controller('userAccountsController', [
Gunnar Millseedefd32018-02-28 17:02:34 -060015 '$scope',
16 '$window',
17 'APIUtils',
Iftekharul Islamcd789502017-04-19 14:37:55 -050018 'dataService',
19 function($scope, $window, APIUtils, dataService){
20 $scope.dataService = dataService;
Gunnar Mills08744f82018-03-19 16:24:41 -050021 $scope.state = "none";
22 $scope.errorMsg = "";
23
Gunnar Mills6b97dde2018-03-14 17:04:58 -050024 $scope.changePassword = function(oldPassword, newPassword, confirmNewPassword){
Gunnar Mills08744f82018-03-19 16:24:41 -050025 var user = $scope.dataService.getUser();
Gunnar Mills6b97dde2018-03-14 17:04:58 -050026 if(!oldPassword || !newPassword || !confirmNewPassword ){
Gunnar Mills08744f82018-03-19 16:24:41 -050027 $scope.state = "error";
28 $scope.errorMsg = "Field is required!";
Gunnar Mills6b97dde2018-03-14 17:04:58 -050029 return false;
30 }
31 if (newPassword !== confirmNewPassword){
Gunnar Mills08744f82018-03-19 16:24:41 -050032 $scope.state = "error";
33 $scope.errorMsg = "New passwords do not match!";
Gunnar Mills6b97dde2018-03-14 17:04:58 -050034 return false;
35 }
36 if (newPassword === oldPassword){
Gunnar Mills08744f82018-03-19 16:24:41 -050037 $scope.state = "error";
38 $scope.errorMsg = "New password and old password match!";
Gunnar Mills6b97dde2018-03-14 17:04:58 -050039 return false;
40 }
Gunnar Mills6b97dde2018-03-14 17:04:58 -050041
Gunnar Mills32581cf2018-03-16 15:52:54 -050042 // Verify the oldPassword is correct
Gunnar Mills08744f82018-03-19 16:24:41 -050043 APIUtils.testPassword(user, oldPassword).then(function(state){
44 APIUtils.changePassword(user, newPassword).then(function(response){
Gunnar Mills32581cf2018-03-16 15:52:54 -050045 // Clear the textboxes on a success
46 $scope.passwordVerify = '';
47 $scope.password = '';
48 $scope.oldPassword = '';
Gunnar Mills08744f82018-03-19 16:24:41 -050049 $scope.state = "success";
Gunnar Mills32581cf2018-03-16 15:52:54 -050050 }, function(error){
Gunnar Mills08744f82018-03-19 16:24:41 -050051 $scope.state = "error";
52 $scope.errorMsg = "Error changing password!";
Gunnar Mills32581cf2018-03-16 15:52:54 -050053 });
Gunnar Mills6b97dde2018-03-14 17:04:58 -050054 }, function(error){
Gunnar Mills08744f82018-03-19 16:24:41 -050055 $scope.state = "error";
56 $scope.errorMsg = "Old password is not correct!";
Gunnar Mills6b97dde2018-03-14 17:04:58 -050057 });
58 }
Iftekharul Islamcd789502017-04-19 14:37:55 -050059 }
60 ]
61 );
Iftekharul Islamcd789502017-04-19 14:37:55 -050062})(angular);