Passed
Push — master ( c3ec55...6fe0c7 )
by Simon
04:07
created

AccessDeniedException::getReadableError()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 22
cp 0
rs 9.2728
cc 5
nc 8
nop 0
crap 30
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\Exceptions;
11
12
use Waca\DataObjects\Domain;
13
use Waca\DataObjects\Log;
14
use Waca\DataObjects\User;
15
use Waca\Fragments\NavigationMenuAccessControl;
16
use Waca\Helpers\PreferenceManager;
17
use Waca\Helpers\SearchHelpers\LogSearchHelper;
18
use Waca\PdoDatabase;
19
use Waca\Security\IDomainAccessManager;
20
use Waca\Security\ISecurityManager;
21
22
/**
23
 * Class AccessDeniedException
24
 *
25
 * Thrown when a logged-in user does not have permissions to access a page
26
 *
27
 * @package Waca\Exceptions
28
 */
29
class AccessDeniedException extends ReadableException
30
{
31
    use NavigationMenuAccessControl;
32
33
    private ISecurityManager $securityManager;
34
    private IDomainAccessManager $domainAccessManager;
35
36
    /**
37
     * AccessDeniedException constructor.
38
     *
39
     * @param ISecurityManager     $securityManager
40
     * @param IDomainAccessManager $domainAccessManager
41
     */
42
    public function __construct(ISecurityManager $securityManager, IDomainAccessManager $domainAccessManager)
43
    {
44
        $this->securityManager = $securityManager;
45
        $this->domainAccessManager = $domainAccessManager;
46
    }
47
48
    public function getReadableError()
49
    {
50
        if (!headers_sent()) {
51
            header("HTTP/1.1 403 Forbidden");
52
        }
53
54
        $this->setUpSmarty();
55
56
        // uck. We should still be able to access the database in this situation though.
57
        $database = PdoDatabase::getDatabaseConnection($this->getSiteConfiguration());
58
        $currentUser = User::getCurrent($database);
59
        $this->assign('skin', PreferenceManager::getForCurrent($database)->getPreference(PreferenceManager::PREF_SKIN));
60
        $this->assign('currentUser', $currentUser);
61
        $this->assign('currentDomain', Domain::getCurrent($database));
62
63
        $this->setupNavMenuAccess($currentUser);
64
65
        if ($currentUser->isDeclined()) {
66
            $this->assign('htmlTitle', 'Account Declined');
67
            $this->assign('declineReason', $this->getLogEntry('Declined', $currentUser, $database));
68
69
            return $this->fetchTemplate("exception/account-declined.tpl");
70
        }
71
72
        if ($currentUser->isSuspended()) {
73
            $this->assign('htmlTitle', 'Account Suspended');
74
            $this->assign('suspendReason', $this->getLogEntry('Suspended', $currentUser, $database));
75
76
            return $this->fetchTemplate("exception/account-suspended.tpl");
77
        }
78
79
        if ($currentUser->isNewUser()) {
80
            $this->assign('htmlTitle', 'Account Pending');
81
82
            return $this->fetchTemplate("exception/account-new.tpl");
83
        }
84
85
        return $this->fetchTemplate("exception/access-denied.tpl");
86
    }
87
88
    /**
89
     * @param string      $action
90
     * @param User        $user
91
     * @param PdoDatabase $database
92
     *
93
     * @return null|string
94
     */
95
    private function getLogEntry($action, User $user, PdoDatabase $database)
96
    {
97
        /** @var Log[] $logs */
98
        $logs = LogSearchHelper::get($database, null)
99
            ->byAction($action)
100
            ->byObjectType('User')
101
            ->byObjectId($user->getId())
102
            ->limit(1)
103
            ->fetch();
104
105
        if (count($logs) > 0) {
106
            return $logs[0]->getComment();
107
        }
108
109
        return null;
110
    }
111
112
    protected function getSecurityManager(): ISecurityManager
113
    {
114
        return $this->securityManager;
115
    }
116
117
    public function getDomainAccessManager(): IDomainAccessManager
118
    {
119
        return $this->domainAccessManager;
120
    }
121
}