Failed Conditions
Push — master ( 1b2d37...3f91db )
by Simon
13:21 queued 09:15
created

AccessDeniedException::getLogEntry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 8
cp 0
rs 9.9666
cc 2
nc 2
nop 3
crap 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\Exceptions;
11
12
use Waca\DataObjects\Domain;
13
use Waca\DataObjects\User;
14
use Waca\Fragments\LogEntryLookup;
15
use Waca\Fragments\NavigationMenuAccessControl;
16
use Waca\Helpers\PreferenceManager;
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
    use LogEntryLookup;
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->isDeactivated()) {
66
            $this->assign('htmlTitle', 'Account Deactivated');
67
            $this->assign('deactivationReason', $this->getLogEntry('DeactivatedUser', $currentUser, $database));
68
69
            return $this->fetchTemplate("exception/account-deactivated.tpl");
70
        }
71
72
        if ($currentUser->isNewUser()) {
73
            $this->assign('htmlTitle', 'Account Pending');
74
75
            return $this->fetchTemplate("exception/account-new.tpl");
76
        }
77
78
        return $this->fetchTemplate("exception/access-denied.tpl");
79
    }
80
81
82
83
    protected function getSecurityManager(): ISecurityManager
84
    {
85
        return $this->securityManager;
86
    }
87
88
    public function getDomainAccessManager(): IDomainAccessManager
89
    {
90
        return $this->domainAccessManager;
91
    }
92
}