Failed Conditions
Pull Request — newinternal (#527)
by Simon
16:02 queued 05:59
created

PageChangePassword   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A main() 0 37 4
A validateNewPassword() 0 16 6
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages\UserAuth;
10
11
use Waca\DataObjects\User;
12
use Waca\Exceptions\ApplicationLogicException;
13
use Waca\Security\CredentialProviders\PasswordCredentialProvider;
14
use Waca\SessionAlert;
15
use Waca\Tasks\InternalPageBase;
16
use Waca\WebRequest;
17
18
class PageChangePassword extends InternalPageBase
19
{
20
    /**
21
     * Main function for this page, when no specific actions are called.
22
     * @return void
23
     */
24
    protected function main()
25
    {
26
        $this->setHtmlTitle('Change Password');
27
28
        if (WebRequest::wasPosted()) {
29
            $this->validateCSRFToken();
30
            try {
31
                $oldPassword = WebRequest::postString('oldpassword');
32
                $newPassword = WebRequest::postString('newpassword');
33
                $newPasswordConfirmation = WebRequest::postString('newpasswordconfirm');
34
35
                $user = User::getCurrent($this->getDatabase());
36
                if (!$user instanceof User) {
37
                    throw new ApplicationLogicException('User not found');
38
                }
39
40
                $this->validateNewPassword($oldPassword, $newPassword, $newPasswordConfirmation, $user);
41
            }
42
            catch (ApplicationLogicException $ex) {
43
                SessionAlert::error($ex->getMessage());
44
                $this->redirect('changePassword');
45
46
                return;
47
            }
48
49
            $passwordProvider = new PasswordCredentialProvider($this->getDatabase(), $this->getSiteConfiguration());
50
            $passwordProvider->setCredential($user, 1, $newPassword);
51
52
            SessionAlert::success('Password changed successfully!');
53
54
            $this->redirect('preferences');
55
        }
56
        else {
57
            $this->assignCSRFToken();
58
            $this->setTemplate('preferences/changePassword.tpl');
59
        }
60
    }
61
62
    /**
63
     * @param string $oldPassword
64
     * @param string $newPassword
65
     * @param string $newPasswordConfirmation
66
     * @param User   $user
67
     *
68
     * @throws ApplicationLogicException
69
     */
70
    protected function validateNewPassword($oldPassword, $newPassword, $newPasswordConfirmation, User $user)
71
    {
72
        if ($oldPassword === null || $newPassword === null || $newPasswordConfirmation === null) {
73
            throw new ApplicationLogicException('All three fields must be completed to change your password');
74
        }
75
76
        if ($newPassword !== $newPasswordConfirmation) {
77
            throw new ApplicationLogicException('Your new passwords did not match!');
78
        }
79
80
        // TODO: adapt for MFA support
81
        $passwordProvider = new PasswordCredentialProvider($this->getDatabase(), $this->getSiteConfiguration());
82
        if (!$passwordProvider->authenticate($user, $oldPassword)) {
83
            throw new ApplicationLogicException('The password you entered was incorrect.');
84
        }
85
    }
86
}