Completed
Branch newinternal (6027bd)
by Simon
06:03
created

SecurityManager::allows()   C

Complexity

Conditions 11
Paths 15

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 5.9012
cc 11
eloc 14
nc 15
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Waca\Security;
4
5
use Waca\DataObjects\User;
6
use Waca\Exceptions\AccessDeniedException;
7
use Waca\IdentificationVerifier;
8
9
final class SecurityManager
10
{
11
	/** @var IdentificationVerifier */
12
	private $identificationVerifier;
13
	/** @var SecurityConfigurationFactory */
14
	private $securityConfigurationFactory;
15
16
	/**
17
	 * SecurityManager constructor.
18
	 *
19
	 * @param IdentificationVerifier $identificationVerifier
20
	 * @param bool                   $forceIdentification
21
	 */
22
	public function __construct(IdentificationVerifier $identificationVerifier, $forceIdentification)
23
	{
24
		$this->identificationVerifier = $identificationVerifier;
25
26
		$this->securityConfigurationFactory = new SecurityConfigurationFactory($forceIdentification);
27
	}
28
29
	public function configure(){
30
		return $this->securityConfigurationFactory;
31
	}
32
33
	/**
34
	 * @param $value
35
	 * @param $filter
36
	 *
37
	 * @return bool
38
	 * @throws AccessDeniedException
39
	 * @category Security-Critical
40
	 */
41
	private function test($value, $filter)
42
	{
43
		if (!$filter) {
44
			return false;
45
		}
46
47
		if ($value == SecurityConfiguration::DENY) {
48
			// FILE_NOT_FOUND...?
49
			throw new AccessDeniedException();
50
		}
51
52
		return $value === SecurityConfiguration::ALLOW;
53
	}
54
55
	/**
56
	 * Tests if a user is allowed to perform an action.
57
	 *
58
	 * This method should form a hard, deterministic security barrier, and only return true if it is absolutely sure
59
	 * that a user should have access to something.
60
	 *
61
	 * @param SecurityConfiguration $config
62
	 * @param User                  $user
63
	 *
64
	 * @return bool
65
	 *
66
	 * @category Security-Critical
67
	 */
68
	public function allows(SecurityConfiguration $config, User $user)
69
	{
70
		if ($config->requiresIdentifiedUser() && !$user->isCommunityUser() && !$user->isIdentified($this->identificationVerifier)) {
71
			return false;
72
		}
73
74
		try {
75
			$allowed = $this->test($config->getAdmin(), $user->isAdmin())
76
				|| $this->test($config->getUser(), $user->isUser())
77
				|| $this->test($config->getCommunity(), $user->isCommunityUser())
78
				|| $this->test($config->getSuspended(), $user->isSuspended())
79
				|| $this->test($config->getDeclined(), $user->isDeclined())
80
				|| $this->test($config->getNew(), $user->isNew())
81
				|| $this->test($config->getCheckuser(), $user->isCheckuser());
82
83
			return $allowed;
84
		}
85
		catch (AccessDeniedException $ex) {
86
			// something is set to deny.
87
			return false;
88
		}
89
	}
90
}