Passed
Push — multiproject/local-access ( 5353e5 )
by Simon
04:56
created

AccessDeniedException::getReadableError()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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