Passed
Push — search ( bdb480...5e7f6e )
by Simon
17:14 queued 07:17
created

PageDomainManagement   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 94
dl 0
loc 146
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A main() 0 28 3
A create() 0 50 3
B edit() 0 61 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;
10
11
use Waca\DataObjects\Domain;
12
use Waca\DataObjects\EmailTemplate;
13
use Waca\DataObjects\User;
14
use Waca\Exceptions\AccessDeniedException;
15
use Waca\Helpers\Logger;
16
use Waca\SessionAlert;
17
use Waca\Tasks\InternalPageBase;
18
use Waca\WebRequest;
19
20
class PageDomainManagement extends InternalPageBase
21
{
22
    protected function main()
23
    {
24
        $this->setHtmlTitle('Domain Management');
25
26
        $database = $this->getDatabase();
27
        $currentUser = User::getCurrent($database);
28
29
        /** @var Domain[] $domains */
30
        $domains = Domain::getAll($database);
31
32
        $templates = [];
33
        foreach ($domains as $domain) {
34
            if ($domain->getDefaultClose() !== null) {
35
                $templates[$domain->getDefaultClose()] = EmailTemplate::getById($domain->getDefaultClose(), $database);
36
            }
37
        }
38
39
        $canEdit = $this->barrierTest('edit', $currentUser);
40
        $canEditAll = $this->barrierTest('editAll', $currentUser);
41
        $canCreate = $this->barrierTest('create', $currentUser);
42
        $this->assign('canEdit', $canEdit);
43
        $this->assign('canEditAll', $canEditAll);
44
        $this->assign('canCreate', $canCreate);
45
46
        $this->assign('domains', $domains);
47
        $this->assign('closeTemplates', $templates);
48
        $this->assign('currentDomain', Domain::getCurrent($database));
49
        $this->setTemplate('domain-management/main.tpl');
50
    }
51
52
    protected function create()
53
    {
54
        $this->setHtmlTitle('Domain Management');
55
        $database = $this->getDatabase();
56
        $currentUser = User::getCurrent($database);
57
58
        // quickly check the user is allowed to edit all fields. If not, then they shouldn't be allowed to create
59
        // new domains either. With any luck, a competent developer would never grant create without editAll to a role
60
        // anyway, so this will never be hit.
61
        if (!$this->barrierTest('editAll', $currentUser)) {
62
            throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
63
        }
64
65
        if (WebRequest::wasPosted()) {
66
            $this->validateCSRFToken();
67
68
            $domain = new Domain();
69
            $domain->setDatabase($database);
70
71
            $domain->setShortName(WebRequest::postString('shortName'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('shortName') can also be of type null; however, parameter $shortName of Waca\DataObjects\Domain::setShortName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
            $domain->setShortName(/** @scrutinizer ignore-type */ WebRequest::postString('shortName'));
Loading history...
72
            $domain->setLongName(WebRequest::postString('longName'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('longName') can also be of type null; however, parameter $longName of Waca\DataObjects\Domain::setLongName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            $domain->setLongName(/** @scrutinizer ignore-type */ WebRequest::postString('longName'));
Loading history...
73
            $domain->setWikiArticlePath(WebRequest::postString('articlePath'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('articlePath') can also be of type null; however, parameter $wikiArticlePath of Waca\DataObjects\Domain::setWikiArticlePath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            $domain->setWikiArticlePath(/** @scrutinizer ignore-type */ WebRequest::postString('articlePath'));
Loading history...
74
            $domain->setWikiApiPath(WebRequest::postString('apiPath'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('apiPath') can also be of type null; however, parameter $wikiApiPath of Waca\DataObjects\Domain::setWikiApiPath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            $domain->setWikiApiPath(/** @scrutinizer ignore-type */ WebRequest::postString('apiPath'));
Loading history...
75
            $domain->setEnabled(WebRequest::postBoolean('enabled'));
76
            $domain->setDefaultLanguage(WebRequest::postString('defaultLanguage'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('defaultLanguage') can also be of type null; however, parameter $defaultLanguage of Waca\DataObjects\Domain::setDefaultLanguage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            $domain->setDefaultLanguage(/** @scrutinizer ignore-type */ WebRequest::postString('defaultLanguage'));
Loading history...
77
            $domain->setDefaultClose(null);
78
            $domain->setEmailSender(WebRequest::postString('emailSender'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('emailSender') can also be of type null; however, parameter $emailSender of Waca\DataObjects\Domain::setEmailSender() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            $domain->setEmailSender(/** @scrutinizer ignore-type */ WebRequest::postString('emailSender'));
Loading history...
79
            $domain->setNotificationTarget(WebRequest::postString('notificationTarget'));
80
81
            $domain->save();
82
83
            Logger::domainCreated($database, $domain);
84
            $this->redirect('domainManagement');
85
        }
86
        else {
87
            $this->assignCSRFToken();
88
89
            $this->assign('shortName', '');
90
            $this->assign('longName', '');
91
            $this->assign('articlePath', '');
92
            $this->assign('apiPath', '');
93
            $this->assign('enabled', false);
94
            $this->assign('defaultLanguage', 'en');
95
            $this->assign('emailSender', '');
96
            $this->assign('notificationTarget', '');
97
98
            $this->assign('createMode', true);
99
            $this->assign('canEditAll', true);
100
101
            $this->setTemplate('domain-management/edit.tpl');
102
        }
103
    }
104
105
    protected function edit()
106
    {
107
        $this->setHtmlTitle('Domain Management');
108
        $database = $this->getDatabase();
109
        $currentUser = User::getCurrent($database);
110
111
        $canEditAll = $this->barrierTest('editAll', $currentUser);
112
113
        /** @var Domain $domain */
114
        $domain = Domain::getById(WebRequest::getInt('domain'), $database);
115
116
        if (WebRequest::wasPosted()) {
117
            $this->validateCSRFToken();
118
119
            $domain->setLongName(WebRequest::postString('longName'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('longName') can also be of type null; however, parameter $longName of Waca\DataObjects\Domain::setLongName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
            $domain->setLongName(/** @scrutinizer ignore-type */ WebRequest::postString('longName'));
Loading history...
120
            $domain->setDefaultLanguage(WebRequest::postString('defaultLanguage'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('defaultLanguage') can also be of type null; however, parameter $defaultLanguage of Waca\DataObjects\Domain::setDefaultLanguage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

120
            $domain->setDefaultLanguage(/** @scrutinizer ignore-type */ WebRequest::postString('defaultLanguage'));
Loading history...
121
122
            /** @var EmailTemplate $template */
123
            $template = EmailTemplate::getById(WebRequest::postInt('defaultClose'), $database);
124
            if ($template->getActive()
125
                && $template->getPreloadOnly() === false
126
                && $template->getDefaultAction() === EmailTemplate::ACTION_CREATED) {
127
                $domain->setDefaultClose(WebRequest::postInt('defaultClose'));
128
            }
129
            else {
130
                SessionAlert::warning("Chosen email template is not valid for use as the default creation template");
131
            }
132
133
            if ($canEditAll) {
134
                $domain->setWikiArticlePath(WebRequest::postString('articlePath'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('articlePath') can also be of type null; however, parameter $wikiArticlePath of Waca\DataObjects\Domain::setWikiArticlePath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
                $domain->setWikiArticlePath(/** @scrutinizer ignore-type */ WebRequest::postString('articlePath'));
Loading history...
135
                $domain->setWikiApiPath(WebRequest::postString('apiPath'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('apiPath') can also be of type null; however, parameter $wikiApiPath of Waca\DataObjects\Domain::setWikiApiPath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
                $domain->setWikiApiPath(/** @scrutinizer ignore-type */ WebRequest::postString('apiPath'));
Loading history...
136
                $domain->setEnabled(WebRequest::postBoolean('enabled'));
137
                $domain->setEmailSender(WebRequest::postString('emailSender'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('emailSender') can also be of type null; however, parameter $emailSender of Waca\DataObjects\Domain::setEmailSender() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
                $domain->setEmailSender(/** @scrutinizer ignore-type */ WebRequest::postString('emailSender'));
Loading history...
138
                $domain->setNotificationTarget(WebRequest::postString('notificationTarget'));
139
            }
140
141
            $domain->save();
142
143
            Logger::domainEdited($database, $domain);
144
            $this->redirect('domainManagement');
145
        }
146
        else {
147
            $this->assignCSRFToken();
148
149
            $templates = EmailTemplate::getActiveNonpreloadTemplates(EmailTemplate::ACTION_CREATED, $database);
150
            $this->assign('closeTemplates', $templates);
151
152
            $this->assign('shortName', $domain->getShortName());
153
            $this->assign('longName', $domain->getLongName());
154
            $this->assign('articlePath', $domain->getWikiArticlePath());
155
            $this->assign('apiPath', $domain->getWikiApiPath());
156
            $this->assign('enabled', $domain->isEnabled());
157
            $this->assign('defaultClose', $domain->getDefaultClose());
158
            $this->assign('defaultLanguage', $domain->getDefaultLanguage());
159
            $this->assign('emailSender', $domain->getEmailSender());
160
            $this->assign('notificationTarget', $domain->getNotificationTarget());
161
162
            $this->assign('createMode', false);
163
            $this->assign('canEditAll', $canEditAll);
164
165
            $this->setTemplate('domain-management/edit.tpl');
166
        }
167
    }
168
}
169