RemoteAccessManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\Application\RemoteAccessManager;
6
7
use Nette\Http\IRequest;
8
use Nette\SmartObject;
9
use SixtyEightPublishers\Application\RemoteAccessManager\Handler\IAccessHandler;
10
use Tracy\Debugger;
11
12
/**
13
 * @method  RemoteAccessManager   onAccess()
14
 * @method  RemoteAccessManager   onDeny()
15
 */
16
class RemoteAccessManager implements IRemoteAccessManager
17
{
18
	use SmartObject;
19
20
	const
21
		COOKIE_SECRET = 'ram-secret-key',
22
		ALLOWED_ALL = TRUE,
23
		DENY_ALL = FALSE;
24
25
	/** @var \Nette\Http\IRequest */
26
	private $request;
27
28
	/** @var array */
29
	private $whitelist;
30
31
	/** @var array */
32
	private $blacklist;
33
34
	/** @var string|null */
35
	private $key;
36
37
	/** @var bool|true */
38
	private $mode;
39
40
	/** @var bool|false */
41
	private $consoleMode;
42
43
	/** @var \SixtyEightPublishers\Application\RemoteAccessManager\Handler\IAccessHandler */
44
	private $handler;
45
46
	/**
47
	 * @param \Nette\Http\IRequest    $request
48
	 * @param string|array            $blacklist
49
	 * @param string|array            $whitelist
50
	 * @param string                  $key
51
	 * @param bool|TRUE               $mode
52
	 * @param bool|FALSE              $consoleMode
53
	 * @param IAccessHandler          $handler
54
	 */
55
	public function __construct(IRequest $request, $blacklist = [], $whitelist = [], $mode = self::ALLOWED_ALL, $key = self::COOKIE_SECRET, $consoleMode = FALSE, IAccessHandler $handler)
56
	{
57
		$this->request = $request;
58
		$this->blacklist = $blacklist;
0 ignored issues
show
Documentation Bug introduced by
It seems like $blacklist can also be of type string. However, the property $blacklist is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
		$this->whitelist = $whitelist;
0 ignored issues
show
Documentation Bug introduced by
It seems like $whitelist can also be of type string. However, the property $whitelist is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
60
		$this->mode = $mode;
61
		$this->consoleMode = $consoleMode;
62
		$this->key = $key;
63
		$this->handler = $handler;
64
	}
65
66
	public function process()
67
	{
68
		$this->isAllowed()
69
			? $this->handler->allow()
70
			: $this->handler->deny();
71
	}
72
73
	/**
74
	 * @return bool
75
	 */
76
	private function isAllowed() : bool
77
	{
78
		if ($this->consoleMode) {
79
			return TRUE;
80
		}
81
82
		$addr = $this->request->getRemoteAddress() ?: php_uname('n');
83
		$secret = $this->request->getCookie($this->key);
84
85
		if ($this->isAllowedAll()) {
86
			$blacklist = is_string($this->blacklist)
87
				? preg_split('#[,\s]+#', $this->blacklist)
88
				: (array) $this->blacklist;
89
			$allow = !(in_array($addr, $blacklist, TRUE) || in_array("$secret@$addr", $blacklist, TRUE));
90
		} else {
91
			$whitelist = is_string($this->whitelist)
92
				? preg_split('#[,\s]+#', $this->whitelist)
93
				: (array) $this->whitelist;
94
			if (!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
95
				$whitelist[] = '127.0.0.1';
96
				$whitelist[] = '::1';
97
			}
98
			$allow = in_array($addr, $whitelist, TRUE) || in_array("$secret@$addr", $whitelist, TRUE) || in_array($secret, $whitelist, TRUE);
99
		}
100
101
		return $allow;
102
	}
103
104
	/**
105
	 * @return bool
106
	 */
107
	private function isAllowedAll() : bool
108
	{
109
		return $this->mode === self::ALLOWED_ALL;
110
	}
111
}
112