Failed Conditions
Push — newinternal ( 216d62...410e59 )
by Simon
05:28 queued 13s
created

PagePreferences   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 38
c 1
b 0
f 0
dl 0
loc 69
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A main() 0 49 5
A setCreationMode() 0 8 2
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\Helpers\OAuthUserHelper;
13
use Waca\SessionAlert;
14
use Waca\Tasks\InternalPageBase;
15
use Waca\WebRequest;
16
17
class PagePreferences extends InternalPageBase
18
{
19
    /**
20
     * Main function for this page, when no specific actions are called.
21
     * @return void
22
     */
23
    protected function main()
24
    {
25
        $this->setHtmlTitle('Preferences');
26
27
        $enforceOAuth = $this->getSiteConfiguration()->getEnforceOAuth();
28
        $database = $this->getDatabase();
29
        $user = User::getCurrent($database);
30
31
        // Dual mode
32
        if (WebRequest::wasPosted()) {
33
            $this->validateCSRFToken();
34
            $user->setWelcomeSig(WebRequest::postString('sig'));
35
            $user->setEmailSig(WebRequest::postString('emailsig'));
36
            $user->setAbortPref(WebRequest::getBoolean('sig') ? 1 : 0);
37
            $this->setCreationMode($user);
38
39
            $email = WebRequest::postEmail('email');
40
            if ($email !== null) {
41
                $user->setEmail($email);
42
            }
43
44
            $user->save();
45
            SessionAlert::success("Preferences updated!");
46
47
            $this->redirect('');
48
        }
49
        else {
50
            $this->assignCSRFToken();
51
            $this->setTemplate('preferences/prefs.tpl');
52
            $this->assign("enforceOAuth", $enforceOAuth);
53
54
            $this->assign('canManualCreate',
55
                $this->barrierTest(User::CREATION_MANUAL, $user, 'RequestCreation'));
56
            $this->assign('canOauthCreate',
57
                $this->barrierTest(User::CREATION_OAUTH, $user, 'RequestCreation'));
58
            $this->assign('canBotCreate',
59
                $this->barrierTest(User::CREATION_BOT, $user, 'RequestCreation'));
60
61
            $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(),
62
                $this->getSiteConfiguration());
63
            $this->assign('oauth', $oauth);
64
65
            $identity = null;
66
            if ($oauth->isFullyLinked()) {
67
                $identity = $oauth->getIdentity();
68
            }
69
70
            $this->assign('identity', $identity);
71
            $this->assign('graceTime', $this->getSiteConfiguration()->getOauthIdentityGraceTime());
72
        }
73
    }
74
75
    /**
76
     * @param User $user
77
     */
78
    protected function setCreationMode(User $user)
79
    {
80
        // if the user is selecting a creation mode that they are not allowed, do nothing.
81
        // this has the side effect of allowing them to keep a selected mode that either has been changed for them,
82
        // or that they have kept from when they previously had certain access.
83
        $creationMode = WebRequest::postInt('creationmode');
84
        if($this->barrierTest($creationMode, $user, 'RequestCreation')){
85
            $user->setCreationMode($creationMode);
86
        }
87
    }
88
}
89