Issues (186)

includes/Pages/PageDomainManagement.php (13 issues)

Labels
Severity
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Pages;
11
12
use Waca\DataObjects\Domain;
13
use Waca\DataObjects\EmailTemplate;
14
use Waca\DataObjects\User;
15
use Waca\Exceptions\AccessDeniedException;
16
use Waca\Helpers\Logger;
17
use Waca\SessionAlert;
18
use Waca\Tasks\InternalPageBase;
19
use Waca\WebRequest;
20
21
class PageDomainManagement extends InternalPageBase
22
{
23
    protected function main()
24
    {
25
        $this->setHtmlTitle('Domain Management');
26
27
        $database = $this->getDatabase();
28
        $currentUser = User::getCurrent($database);
29
30
        /** @var Domain[] $domains */
31
        $domains = Domain::getAll($database);
32
33
        $templates = [];
34
        foreach ($domains as $domain) {
35
            if ($domain->getDefaultClose() !== null) {
36
                $templates[$domain->getDefaultClose()] = EmailTemplate::getById($domain->getDefaultClose(), $database);
37
            }
38
        }
39
40
        $canEdit = $this->barrierTest('edit', $currentUser);
41
        $canEditAll = $this->barrierTest('editAll', $currentUser);
42
        $canCreate = $this->barrierTest('create', $currentUser);
43
        $this->assign('canEdit', $canEdit);
44
        $this->assign('canEditAll', $canEditAll);
45
        $this->assign('canCreate', $canCreate);
46
47
        $this->assign('domains', $domains);
48
        $this->assign('closeTemplates', $templates);
49
        $this->assign('currentDomain', Domain::getCurrent($database));
50
        $this->setTemplate('domain-management/main.tpl');
51
    }
52
53
    protected function create()
54
    {
55
        $this->setHtmlTitle('Domain Management');
56
        $database = $this->getDatabase();
57
        $currentUser = User::getCurrent($database);
58
59
        // quickly check the user is allowed to edit all fields. If not, then they shouldn't be allowed to create
60
        // new domains either. With any luck, a competent developer would never grant create without editAll to a role
61
        // anyway, so this will never be hit.
62
        if (!$this->barrierTest('editAll', $currentUser)) {
63
            throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
64
        }
65
66
        if (WebRequest::wasPosted()) {
67
            $this->validateCSRFToken();
68
69
            $domain = new Domain();
70
            $domain->setDatabase($database);
71
72
            $domain->setShortName(WebRequest::postString('shortName'));
0 ignored issues
show
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

72
            $domain->setShortName(/** @scrutinizer ignore-type */ WebRequest::postString('shortName'));
Loading history...
73
            $domain->setLongName(WebRequest::postString('longName'));
0 ignored issues
show
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

73
            $domain->setLongName(/** @scrutinizer ignore-type */ WebRequest::postString('longName'));
Loading history...
74
            $domain->setWikiArticlePath(WebRequest::postString('articlePath'));
0 ignored issues
show
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

74
            $domain->setWikiArticlePath(/** @scrutinizer ignore-type */ WebRequest::postString('articlePath'));
Loading history...
75
            $domain->setWikiApiPath(WebRequest::postString('apiPath'));
0 ignored issues
show
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

75
            $domain->setWikiApiPath(/** @scrutinizer ignore-type */ WebRequest::postString('apiPath'));
Loading history...
76
            $domain->setEnabled(WebRequest::postBoolean('enabled'));
77
            $domain->setDefaultLanguage(WebRequest::postString('defaultLanguage'));
0 ignored issues
show
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

77
            $domain->setDefaultLanguage(/** @scrutinizer ignore-type */ WebRequest::postString('defaultLanguage'));
Loading history...
78
            $domain->setDefaultClose(null);
79
            $domain->setEmailReplyAddress(WebRequest::postString('emailReplyTo'));
0 ignored issues
show
It seems like Waca\WebRequest::postString('emailReplyTo') can also be of type null; however, parameter $emailReplyAddress of Waca\DataObjects\Domain::setEmailReplyAddress() 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

79
            $domain->setEmailReplyAddress(/** @scrutinizer ignore-type */ WebRequest::postString('emailReplyTo'));
Loading history...
80
            $domain->setNotificationTarget(WebRequest::postString('notificationTarget'));
81
            $domain->setLocalDocumentation(WebRequest::postString('localDocumentation'));
0 ignored issues
show
It seems like Waca\WebRequest::postString('localDocumentation') can also be of type null; however, parameter $localDocumentation of Waca\DataObjects\Domain::setLocalDocumentation() 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

81
            $domain->setLocalDocumentation(/** @scrutinizer ignore-type */ WebRequest::postString('localDocumentation'));
Loading history...
82
83
            $domain->save();
84
85
            Logger::domainCreated($database, $domain);
86
            $this->redirect('domainManagement');
87
        }
88
        else {
89
            $this->assignCSRFToken();
90
91
            $this->assign('shortName', '');
92
            $this->assign('longName', '');
93
            $this->assign('articlePath', '');
94
            $this->assign('apiPath', '');
95
            $this->assign('enabled', false);
96
            $this->assign('defaultLanguage', 'en');
97
            $this->assign('emailReplyTo', '');
98
            $this->assign('notificationTarget', '');
99
            $this->assign('localDocumentation', '');
100
101
            $this->assign('createMode', true);
102
            $this->assign('canEditAll', true);
103
104
            $this->setTemplate('domain-management/edit.tpl');
105
        }
106
    }
107
108
    protected function edit()
109
    {
110
        $this->setHtmlTitle('Domain Management');
111
        $database = $this->getDatabase();
112
        $currentUser = User::getCurrent($database);
113
114
        $canEditAll = $this->barrierTest('editAll', $currentUser);
115
116
        /** @var Domain $domain */
117
        $domain = Domain::getById(WebRequest::getInt('domain'), $database);
118
119
        if (WebRequest::wasPosted()) {
120
            $this->validateCSRFToken();
121
122
            $domain->setLongName(WebRequest::postString('longName'));
0 ignored issues
show
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

122
            $domain->setLongName(/** @scrutinizer ignore-type */ WebRequest::postString('longName'));
Loading history...
123
            $domain->setDefaultLanguage(WebRequest::postString('defaultLanguage'));
0 ignored issues
show
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

123
            $domain->setDefaultLanguage(/** @scrutinizer ignore-type */ WebRequest::postString('defaultLanguage'));
Loading history...
124
            $domain->setLocalDocumentation(WebRequest::postString('localDocumentation'));
0 ignored issues
show
It seems like Waca\WebRequest::postString('localDocumentation') can also be of type null; however, parameter $localDocumentation of Waca\DataObjects\Domain::setLocalDocumentation() 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

124
            $domain->setLocalDocumentation(/** @scrutinizer ignore-type */ WebRequest::postString('localDocumentation'));
Loading history...
125
126
            /** @var EmailTemplate|false $template */
127
            $template = EmailTemplate::getById(WebRequest::postInt('defaultClose'), $database);
128
            if ($template !== false
129
                && $template->getActive()
130
                && $template->getPreloadOnly() === false
131
                && $template->getDefaultAction() === EmailTemplate::ACTION_CREATED) {
132
                $domain->setDefaultClose(WebRequest::postInt('defaultClose'));
133
            }
134
            else {
135
                SessionAlert::warning("Chosen email template is not valid for use as the default creation template");
136
            }
137
138
            if ($canEditAll) {
139
                $domain->setWikiArticlePath(WebRequest::postString('articlePath'));
0 ignored issues
show
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

139
                $domain->setWikiArticlePath(/** @scrutinizer ignore-type */ WebRequest::postString('articlePath'));
Loading history...
140
                $domain->setWikiApiPath(WebRequest::postString('apiPath'));
0 ignored issues
show
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

140
                $domain->setWikiApiPath(/** @scrutinizer ignore-type */ WebRequest::postString('apiPath'));
Loading history...
141
                $domain->setEnabled(WebRequest::postBoolean('enabled'));
142
                $domain->setEmailReplyAddress(WebRequest::postString('emailReplyTo'));
0 ignored issues
show
It seems like Waca\WebRequest::postString('emailReplyTo') can also be of type null; however, parameter $emailReplyAddress of Waca\DataObjects\Domain::setEmailReplyAddress() 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

142
                $domain->setEmailReplyAddress(/** @scrutinizer ignore-type */ WebRequest::postString('emailReplyTo'));
Loading history...
143
                $domain->setNotificationTarget(WebRequest::postString('notificationTarget'));
144
            }
145
146
            $domain->save();
147
148
            Logger::domainEdited($database, $domain);
149
            $this->redirect('domainManagement');
150
        }
151
        else {
152
            $this->assignCSRFToken();
153
154
            $templates = EmailTemplate::getActiveNonpreloadTemplates(
155
                EmailTemplate::ACTION_CREATED,
156
                $database,
157
                $domain->getId());
158
159
            $this->assign('closeTemplates', $templates);
160
161
            $this->assign('shortName', $domain->getShortName());
162
            $this->assign('longName', $domain->getLongName());
163
            $this->assign('articlePath', $domain->getWikiArticlePath());
164
            $this->assign('apiPath', $domain->getWikiApiPath());
165
            $this->assign('enabled', $domain->isEnabled());
166
            $this->assign('defaultClose', $domain->getDefaultClose());
167
            $this->assign('defaultLanguage', $domain->getDefaultLanguage());
168
            $this->assign('emailReplyTo', $domain->getEmailReplyAddress());
169
            $this->assign('notificationTarget', $domain->getNotificationTarget());
170
            $this->assign('localDocumentation', $domain->getLocalDocumentation());
171
172
173
            $this->assign('createMode', false);
174
            $this->assign('canEditAll', $canEditAll);
175
176
            $this->setTemplate('domain-management/edit.tpl');
177
        }
178
    }
179
}
180