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', [ |
| 13 | '$scope', '$window', 'APIUtils', 'dataService', |
| 14 | function($scope, $window, APIUtils, dataService) { |
| 15 | $scope.dataService = dataService; |
| 16 | $scope.state = 'none'; |
| 17 | $scope.errorMsg = ''; |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 18 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 19 | $scope.changePassword = function( |
| 20 | oldPassword, newPassword, confirmNewPassword) { |
| 21 | var user = $scope.dataService.getUser(); |
| 22 | if (!oldPassword || !newPassword || !confirmNewPassword) { |
| 23 | $scope.state = 'error'; |
| 24 | $scope.errorMsg = 'Field is required!'; |
| 25 | return false; |
| 26 | } |
| 27 | if (newPassword !== confirmNewPassword) { |
| 28 | $scope.state = 'error'; |
| 29 | $scope.errorMsg = 'New passwords do not match!'; |
| 30 | return false; |
| 31 | } |
| 32 | if (newPassword === oldPassword) { |
| 33 | $scope.state = 'error'; |
| 34 | $scope.errorMsg = 'New password and old password match!'; |
| 35 | return false; |
| 36 | } |
Gunnar Mills | 6b97dde | 2018-03-14 17:04:58 -0500 | [diff] [blame] | 37 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 38 | // Verify the oldPassword is correct |
Gunnar Mills | 11bac7e | 2018-05-17 14:45:49 -0500 | [diff] [blame^] | 39 | dataService.ignoreHttpError = true; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 40 | APIUtils.testPassword(user, oldPassword) |
| 41 | .then( |
| 42 | function(state) { |
| 43 | APIUtils.changePassword(user, newPassword) |
| 44 | .then( |
| 45 | function(response) { |
| 46 | // Clear the textboxes on a success |
| 47 | $scope.passwordVerify = ''; |
| 48 | $scope.password = ''; |
| 49 | $scope.oldPassword = ''; |
| 50 | $scope.state = 'success'; |
| 51 | }, |
| 52 | function(error) { |
| 53 | $scope.state = 'error'; |
| 54 | $scope.errorMsg = 'Error changing password!'; |
| 55 | }); |
| 56 | }, |
| 57 | function(error) { |
| 58 | $scope.state = 'error'; |
| 59 | $scope.errorMsg = 'Old password is not correct!'; |
Gunnar Mills | 11bac7e | 2018-05-17 14:45:49 -0500 | [diff] [blame^] | 60 | }).finally(function() { |
| 61 | dataService.ignoreHttpError = false; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 62 | }); |
| 63 | }; |
| 64 | } |
| 65 | ]); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 66 | })(angular); |