blob: 3420eaf33197a63d9e89c3925b5e94ee5e74d8b0 [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
7 * @version 0.1.0
8 */
9
10window.angular && (function (angular) {
11 'use strict';
12
13 angular
14 .module('app.users')
15 .controller('userAccountsController', [
Gunnar Millseedefd32018-02-28 17:02:34 -060016 '$scope',
17 '$window',
18 'APIUtils',
Iftekharul Islamcd789502017-04-19 14:37:55 -050019 'dataService',
20 function($scope, $window, APIUtils, dataService){
21 $scope.dataService = dataService;
Gunnar Mills08744f82018-03-19 16:24:41 -050022 $scope.state = "none";
23 $scope.errorMsg = "";
24
Gunnar Mills6b97dde2018-03-14 17:04:58 -050025 $scope.changePassword = function(oldPassword, newPassword, confirmNewPassword){
Gunnar Mills08744f82018-03-19 16:24:41 -050026 var user = $scope.dataService.getUser();
Gunnar Mills6b97dde2018-03-14 17:04:58 -050027 if(!oldPassword || !newPassword || !confirmNewPassword ){
Gunnar Mills08744f82018-03-19 16:24:41 -050028 $scope.state = "error";
29 $scope.errorMsg = "Field is required!";
Gunnar Mills6b97dde2018-03-14 17:04:58 -050030 return false;
31 }
32 if (newPassword !== confirmNewPassword){
Gunnar Mills08744f82018-03-19 16:24:41 -050033 $scope.state = "error";
34 $scope.errorMsg = "New passwords do not match!";
Gunnar Mills6b97dde2018-03-14 17:04:58 -050035 return false;
36 }
37 if (newPassword === oldPassword){
Gunnar Mills08744f82018-03-19 16:24:41 -050038 $scope.state = "error";
39 $scope.errorMsg = "New password and old password match!";
Gunnar Mills6b97dde2018-03-14 17:04:58 -050040 return false;
41 }
Gunnar Mills6b97dde2018-03-14 17:04:58 -050042
Gunnar Mills32581cf2018-03-16 15:52:54 -050043 // Verify the oldPassword is correct
Gunnar Mills08744f82018-03-19 16:24:41 -050044 APIUtils.testPassword(user, oldPassword).then(function(state){
45 APIUtils.changePassword(user, newPassword).then(function(response){
Gunnar Mills32581cf2018-03-16 15:52:54 -050046 // Clear the textboxes on a success
47 $scope.passwordVerify = '';
48 $scope.password = '';
49 $scope.oldPassword = '';
Gunnar Mills08744f82018-03-19 16:24:41 -050050 $scope.state = "success";
Gunnar Mills32581cf2018-03-16 15:52:54 -050051 }, function(error){
Gunnar Mills08744f82018-03-19 16:24:41 -050052 $scope.state = "error";
53 $scope.errorMsg = "Error changing password!";
Gunnar Mills32581cf2018-03-16 15:52:54 -050054 });
Gunnar Mills6b97dde2018-03-14 17:04:58 -050055 }, function(error){
Gunnar Mills08744f82018-03-19 16:24:41 -050056 $scope.state = "error";
57 $scope.errorMsg = "Old password is not correct!";
Gunnar Mills6b97dde2018-03-14 17:04:58 -050058 });
59 }
Iftekharul Islamcd789502017-04-19 14:37:55 -050060 }
61 ]
62 );
Iftekharul Islamcd789502017-04-19 14:37:55 -050063})(angular);