Completed
Push — master ( 432f1e...46d11e )
by Vojtěch
02:36
created

RemoteAccessManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 105
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A process() 0 6 2
D isAllowed() 0 27 9
A isAllowedAll() 0 4 1
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
	/** @var null|callable */
47
	public $onAllow;
48
49
	/** @var null|callable */
50
	public $onDeny;
51
52
	/**
53
	 * @param \Nette\Http\IRequest    $request
54
	 * @param string|array            $blacklist
55
	 * @param string|array            $whitelist
56
	 * @param string                  $key
57
	 * @param bool|TRUE               $mode
58
	 * @param bool|FALSE              $consoleMode
59
	 * @param IAccessHandler          $handler
60
	 */
61
	public function __construct(IRequest $request, $blacklist = [], $whitelist = [], $mode = self::ALLOWED_ALL, $key = self::COOKIE_SECRET, $consoleMode = FALSE, IAccessHandler $handler)
62
	{
63
		$this->request = $request;
64
		$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...
65
		$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...
66
		$this->mode = $mode;
67
		$this->consoleMode = $consoleMode;
68
		$this->key = $key;
69
		$this->handler = $handler;
70
71
		$this->onAllow[] = [$this->handler, 'allow'];
72
		$this->onDeny[] = [$this->handler, 'deny'];
73
	}
74
75
	public function process()
76
	{
77
		$this->isAllowed()
78
			? $this->onAllow()
79
			: $this->onDeny();
80
	}
81
82
	/**
83
	 * @return bool
84
	 */
85
	private function isAllowed() : bool
86
	{
87
		if ($this->consoleMode) {
88
			return TRUE;
89
		}
90
91
		$addr = $this->request->getRemoteAddress() ?: php_uname('n');
92
		$secret = $this->request->getCookie($this->key);
93
94
		if ($this->isAllowedAll()) {
95
			$blacklist = is_string($this->blacklist)
96
				? preg_split('#[,\s]+#', $this->blacklist)
97
				: (array) $this->blacklist;
98
			$allow = !(in_array($addr, $blacklist, TRUE) || in_array("$secret@$addr", $blacklist, TRUE));
99
		} else {
100
			$whitelist = is_string($this->whitelist)
101
				? preg_split('#[,\s]+#', $this->whitelist)
102
				: (array) $this->whitelist;
103
			if (!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
104
				$whitelist[] = '127.0.0.1';
105
				$whitelist[] = '::1';
106
			}
107
			$allow = in_array($addr, $whitelist, TRUE) || in_array("$secret@$addr", $whitelist, TRUE);
108
		}
109
110
		return $allow;
111
	}
112
113
	/**
114
	 * @return bool
115
	 */
116
	private function isAllowedAll() : bool
117
	{
118
		return $this->mode === self::ALLOWED_ALL;
119
	}
120
}
121