Passed
Push — appeals ( bdf92f )
by Simon
05:06
created

PageUserReactivate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 31
dl 0
loc 53
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B main() 0 45 6
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\UserAuth;
11
12
use Exception;
13
use Waca\DataObjects\Domain;
14
use Waca\DataObjects\User;
15
use Waca\Exceptions\ApplicationLogicException;
16
use Waca\Fragments\LogEntryLookup;
17
use Waca\Helpers\Logger;
18
use Waca\Helpers\PreferenceManager;
19
use Waca\SessionAlert;
20
use Waca\Tasks\InternalPageBase;
21
use Waca\WebRequest;
22
23
class PageUserReactivate extends InternalPageBase
24
{
25
    use LogEntryLookup;
26
27
    /**
28
     * @throws ApplicationLogicException
29
     * @throws Exception
30
     */
31
    protected function main()
32
    {
33
        $db = $this->getDatabase();
34
        $currentUser = User::getCurrent($db);
35
36
        // *Only* deactivated users should be able to access this.
37
        // Redirect anyone else away.
38
        if (! $currentUser->isDeactivated()) {
39
            $this->redirect();
40
            return;
41
        }
42
43
        $ableToAppeal = true;
44
        $prefs = new PreferenceManager($db, $currentUser->getId(), Domain::getCurrent($db)->getId());
45
        if ($prefs->getPreference(PreferenceManager::ADMIN_PREF_PREVENT_REACTIVATION) ?? false) {
46
            $ableToAppeal = false;
47
        }
48
49
        if (WebRequest::wasPosted()) {
50
            $this->validateCSRFToken();
51
52
            $reason = WebRequest::postString('reason');
53
            $updateVersion = WebRequest::postInt('updateVersion');
54
55
            if(!$ableToAppeal) {
56
                throw new ApplicationLogicException('Appeal is disabled');
57
            }
58
59
            if ($reason == '') {
60
                throw new ApplicationLogicException('The reason field cannot be empty.');
61
            }
62
63
            Logger::requestedReactivation($db, $currentUser, $reason);
0 ignored issues
show
Bug introduced by
It seems like $reason can also be of type null; however, parameter $comment of Waca\Helpers\Logger::requestedReactivation() 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

63
            Logger::requestedReactivation($db, $currentUser, /** @scrutinizer ignore-type */ $reason);
Loading history...
64
            $currentUser->setStatus(User::STATUS_NEW);
65
            $currentUser->setUpdateVersion($updateVersion);
66
            $currentUser->save();
67
68
            SessionAlert::success('Reactivation request has been saved. Please wait for a response from the tool admin team.');
69
            $this->redirect();
70
        } else {
71
            $this->assignCSRFToken();
72
            $this->assign('deactivationReason', $this->getLogEntry('DeactivatedUser', $currentUser, $db));
73
            $this->assign('updateVersion', $currentUser->getUpdateVersion());
74
            $this->assign('ableToAppeal', $ableToAppeal);
75
            $this->setTemplate('reactivate.tpl');
76
        }
77
    }
78
}