Failed Conditions
Push — multiproject/domainswitch ( 54b9a3 )
by Simon
04:31
created

AccessDeniedException   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
eloc 36
dl 0
loc 96
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getReadableError() 0 39 6
A getSecurityManager() 0 3 1
A getDomainAccessManager() 0 3 1
A getLogEntry() 0 11 1
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\SearchHelpers\LogSearchHelper;
16
use Waca\PdoDatabase;
17
use Waca\Security\DomainAccessManager;
18
use Waca\Security\SecurityManager;
19
20
/**
21
 * Class AccessDeniedException
22
 *
23
 * Thrown when a logged-in user does not have permissions to access a page
24
 *
25
 * @package Waca\Exceptions
26
 */
27
class AccessDeniedException extends ReadableException
28
{
29
    use NavigationMenuAccessControl;
30
31
    /**
32
     * @var SecurityManager
33
     */
34
    private $securityManager;
35
36
    /** @var DomainAccessManager|null */
37
    private $domainAccessManager;
38
39
    /**
40
     * AccessDeniedException constructor.
41
     *
42
     * @param SecurityManager          $securityManager
43
     * @param DomainAccessManager|null $domainAccessManager
44
     */
45
    public function __construct(SecurityManager $securityManager = null, DomainAccessManager $domainAccessManager = null)
46
    {
47
        $this->securityManager = $securityManager;
48
        $this->domainAccessManager = $domainAccessManager;
49
    }
50
51
    public function getReadableError()
52
    {
53
        if (!headers_sent()) {
54
            header("HTTP/1.1 403 Forbidden");
55
        }
56
57
        $this->setUpSmarty();
58
59
        // uck. We should still be able to access the database in this situation though.
60
        $database = PdoDatabase::getDatabaseConnection('acc');
61
        $currentUser = User::getCurrent($database);
62
        $this->assign('currentUser', $currentUser);
63
        $this->assign('currentDomain', Domain::getCurrent($database));
64
65
        if ($this->securityManager !== null) {
66
            $this->setupNavMenuAccess($currentUser);
67
        }
68
69
        if ($currentUser->isDeclined()) {
70
            $this->assign('htmlTitle', 'Account Declined');
71
            $this->assign('declineReason', $this->getLogEntry('Declined', $currentUser, $database));
72
73
            return $this->fetchTemplate("exception/account-declined.tpl");
74
        }
75
76
        if ($currentUser->isSuspended()) {
77
            $this->assign('htmlTitle', 'Account Suspended');
78
            $this->assign('suspendReason', $this->getLogEntry('Suspended', $currentUser, $database));
79
80
            return $this->fetchTemplate("exception/account-suspended.tpl");
81
        }
82
83
        if ($currentUser->isNewUser()) {
84
            $this->assign('htmlTitle', 'Account Pending');
85
86
            return $this->fetchTemplate("exception/account-new.tpl");
87
        }
88
89
        return $this->fetchTemplate("exception/access-denied.tpl");
90
    }
91
92
    /**
93
     * @param string      $action
94
     * @param User        $user
95
     * @param PdoDatabase $database
96
     *
97
     * @return null|string
98
     */
99
    private function getLogEntry($action, User $user, PdoDatabase $database)
100
    {
101
        /** @var Log[] $logs */
102
        $logs = LogSearchHelper::get($database)
103
            ->byAction($action)
104
            ->byObjectType('User')
105
            ->byObjectId($user->getId())
106
            ->limit(1)
107
            ->fetch();
108
109
        return $logs[0]->getComment();
110
    }
111
112
    /**
113
     * @return SecurityManager
114
     */
115
    protected function getSecurityManager()
116
    {
117
        return $this->securityManager;
118
    }
119
120
    public function getDomainAccessManager(): DomainAccessManager
121
    {
122
        return $this->domainAccessManager;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->domainAccessManager could return the type null which is incompatible with the type-hinted return Waca\Security\DomainAccessManager. Consider adding an additional type-check to rule them out.
Loading history...
123
    }
124
}