blob: 2d65d8a3b1a4458b237f374df47301d4ae13dffc [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 Geisslerba5e3f32018-05-24 10:58:00 -070012 angular
13 .module('app.users')
14 .controller('userAccountsController', [
15 '$scope',
16 '$window',
17 'APIUtils',
18 'dataService',
19 function($scope, $window, APIUtils, dataService) {
20 $scope.dataService = dataService;
21 $scope.state = 'none';
22 $scope.errorMsg = '';
Gunnar Mills08744f82018-03-19 16:24:41 -050023
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070024 $scope.changePassword = function(oldPassword, newPassword, confirmNewPassword) {
25 var user = $scope.dataService.getUser();
26 if (!oldPassword || !newPassword || !confirmNewPassword) {
27 $scope.state = 'error';
28 $scope.errorMsg = 'Field is required!';
29 return false;
30 }
31 if (newPassword !== confirmNewPassword) {
32 $scope.state = 'error';
33 $scope.errorMsg = 'New passwords do not match!';
34 return false;
35 }
36 if (newPassword === oldPassword) {
37 $scope.state = 'error';
38 $scope.errorMsg = 'New password and old password match!';
39 return false;
40 }
Gunnar Mills6b97dde2018-03-14 17:04:58 -050041
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070042 // Verify the oldPassword is correct
43 APIUtils.testPassword(user, oldPassword).then(function(state) {
44 APIUtils.changePassword(user, newPassword).then(function(response) {
45 // Clear the textboxes on a success
46 $scope.passwordVerify = '';
47 $scope.password = '';
48 $scope.oldPassword = '';
49 $scope.state = 'success';
50 }, function(error) {
51 $scope.state = 'error';
52 $scope.errorMsg = 'Error changing password!';
53 });
54 }, function(error) {
55 $scope.state = 'error';
56 $scope.errorMsg = 'Old password is not correct!';
57 });
58 };
59 }
60 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -050061})(angular);