Completed
Push — autoskin ( a6ce29 )
by Simon
04:51
created

PagePreferences   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 49
c 1
b 0
f 0
dl 0
loc 90
ccs 0
cts 62
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setCreationMode() 0 8 2
A refreshOAuth() 0 18 3
A main() 0 50 5
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::postBoolean('abortpref') ? 1 : 0);
37
            $this->setCreationMode($user);
38
            $user->setSkin(WebRequest::postString('skintype'));
39
40
            $email = WebRequest::postEmail('email');
41
            if ($email !== null) {
42
                $user->setEmail($email);
43
            }
44
45
            $user->save();
46
            SessionAlert::success("Preferences updated!");
47
48
            $this->redirect('');
49
        }
50
        else {
51
            $this->assignCSRFToken();
52
            $this->setTemplate('preferences/prefs.tpl');
53
            $this->assign("enforceOAuth", $enforceOAuth);
54
55
            $this->assign('canManualCreate',
56
                $this->barrierTest(User::CREATION_MANUAL, $user, 'RequestCreation'));
57
            $this->assign('canOauthCreate',
58
                $this->barrierTest(User::CREATION_OAUTH, $user, 'RequestCreation'));
59
            $this->assign('canBotCreate',
60
                $this->barrierTest(User::CREATION_BOT, $user, 'RequestCreation'));
61
62
            $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(),
63
                $this->getSiteConfiguration());
64
            $this->assign('oauth', $oauth);
65
66
            $identity = null;
67
            if ($oauth->isFullyLinked()) {
68
                $identity = $oauth->getIdentity();
69
            }
70
71
            $this->assign('identity', $identity);
72
            $this->assign('graceTime', $this->getSiteConfiguration()->getOauthIdentityGraceTime());
73
        }
74
    }
75
76
    protected function refreshOAuth()
77
    {
78
        if (!WebRequest::wasPosted()) {
79
            $this->redirect('preferences');
80
81
            return;
82
        }
83
84
        $database = $this->getDatabase();
85
        $oauth = new OAuthUserHelper(User::getCurrent($database), $database, $this->getOAuthProtocolHelper(),
86
            $this->getSiteConfiguration());
87
        if ($oauth->isFullyLinked()) {
88
            $oauth->refreshIdentity();
89
        }
90
91
        $this->redirect('preferences');
92
93
        return;
94
    }
95
96
    /**
97
     * @param User $user
98
     */
99
    protected function setCreationMode(User $user)
100
    {
101
        // if the user is selecting a creation mode that they are not allowed, do nothing.
102
        // this has the side effect of allowing them to keep a selected mode that either has been changed for them,
103
        // or that they have kept from when they previously had certain access.
104
        $creationMode = WebRequest::postInt('creationmode');
105
        if ($this->barrierTest($creationMode, $user, 'RequestCreation')) {
106
            $user->setCreationMode($creationMode);
107
        }
108
    }
109
}
110