Completed
Branch newinternal (104de7)
by Simon
10:16
created

AccessDeniedException::getReadableError()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 34
rs 8.5806
cc 4
eloc 19
nc 4
nop 0
1
<?php
2
3
namespace Waca\Exceptions;
4
5
use Waca\DataObjects\Log;
6
use Waca\DataObjects\User;
7
use Waca\Helpers\LogHelper;
8
use Waca\PdoDatabase;
9
10
/**
11
 * Class AccessDeniedException
12
 *
13
 * Thrown when a logged-in user does not have permissions to access a page
14
 *
15
 * @package Waca\Exceptions
16
 */
17
class AccessDeniedException extends ReadableException
18
{
19
	public function getReadableError()
20
	{
21
		header("HTTP/1.1 403 Forbidden");
22
23
		$this->setUpSmarty();
24
25
		// uck. We should still be able to access the database in this situation though.
26
		$database = PdoDatabase::getDatabaseConnection('acc');
27
		$currentUser = User::getCurrent($database);
28
		$this->assign('currentUser', $currentUser);
29
		$this->assign("loggedIn", (!$currentUser->isCommunityUser()));
30
31
		if ($currentUser->isDeclined()) {
32
			$this->assign('htmlTitle', 'Account Declined');
33
			$this->assign('declineReason', $this->getLogEntry('Declined', $currentUser, $database));
34
35
			return $this->fetchTemplate("exception/account-declined.tpl");
36
		}
37
38
		if ($currentUser->isSuspended()) {
39
			$this->assign('htmlTitle', 'Account Suspended');
40
			$this->assign('suspendReason', $this->getLogEntry('Suspended', $currentUser, $database));
41
42
			return $this->fetchTemplate("exception/account-suspended.tpl");
43
		}
44
45
		if ($currentUser->isNewUser()) {
46
			$this->assign('htmlTitle', 'Account Pending');
47
48
			return $this->fetchTemplate("exception/account-new.tpl");
49
		}
50
51
		return $this->fetchTemplate("exception/access-denied.tpl");
52
	}
53
54
	private function getLogEntry($action, User $user, PdoDatabase $database)
55
	{
56
		/** @var Log[] $logs */
57
		list($logs, $count) = LogHelper::getLogs($database, null, $action, 'User', $user->getId(), 1);
58
59
		if ($count === false || count($logs) < 1) {
60
			return "Unable to retrieve log entry";
61
		}
62
63
		return $logs[0]->getComment();
64
	}
65
}