blob: 17dd0e916d6bbbd34003e28e105d77bebd799b63 [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
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07009window.angular && (function(angular) {
10 'use strict';
Iftekharul Islamcd789502017-04-19 14:37:55 -050011
Andrew Geisslerd27bb132018-05-24 11:07:27 -070012 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 Mills08744f82018-03-19 16:24:41 -050018
Andrew Geisslerd27bb132018-05-24 11:07:27 -070019 $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 Mills6b97dde2018-03-14 17:04:58 -050037
Andrew Geisslerd27bb132018-05-24 11:07:27 -070038 // Verify the oldPassword is correct
Gunnar Mills11bac7e2018-05-17 14:45:49 -050039 dataService.ignoreHttpError = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -070040 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 Mills11bac7e2018-05-17 14:45:49 -050060 }).finally(function() {
61 dataService.ignoreHttpError = false;
Andrew Geisslerd27bb132018-05-24 11:07:27 -070062 });
63 };
64 }
65 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -050066})(angular);