|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CWP\Core\Control; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
6
|
|
|
use SilverStripe\Security\BasicAuthMiddleware; |
|
|
|
|
|
|
7
|
|
|
use SilverStripe\Security\PermissionProvider; |
|
8
|
|
|
|
|
9
|
|
|
class CwpBasicAuthMiddleware extends BasicAuthMiddleware implements PermissionProvider |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Whitelisted IP addresses will not be given a basic authentication prompt when other basic authentication |
|
13
|
|
|
* rules via {@link BasicAuthMiddleware} are enabled. |
|
14
|
|
|
* |
|
15
|
|
|
* Please note that this will not have any effect if using BasicAuth.entire_site_protected, which will |
|
16
|
|
|
* always enabled basic authentication for the entire site. |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $whitelistedIps = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getWhitelistedIps() |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->whitelistedIps; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string|string[] $whitelistedIps An array of IP addresses, or a comma delimited string |
|
32
|
|
|
* @return $this |
|
33
|
|
|
*/ |
|
34
|
|
|
public function setWhitelistedIps($whitelistedIps) |
|
35
|
|
|
{ |
|
36
|
|
|
if (is_string($whitelistedIps)) { |
|
37
|
|
|
$whitelistedIps = explode(',', $whitelistedIps); |
|
38
|
|
|
} |
|
39
|
|
|
$this->whitelistedIps = $whitelistedIps; |
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Check for any whitelisted IP addresses. If one matches the current user's IP then return false early, |
|
45
|
|
|
* otherwise allow the default {@link BasicAuthMiddleware} to continue its logic. |
|
46
|
|
|
* |
|
47
|
|
|
* {@inheritDoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function checkMatchingURL(HTTPRequest $request) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->ipMatchesWhitelist()) { |
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
return parent::checkMatchingURL($request); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Check whether the current user's IP address is in the IP whitelist |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function ipMatchesWhitelist() |
|
63
|
|
|
{ |
|
64
|
|
|
$whitelist = $this->getWhitelistedIps(); |
|
65
|
|
|
// Continue if no whitelist is defined |
|
66
|
|
|
if (empty($whitelist)) { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$userIp = $_SERVER['REMOTE_ADDR']; |
|
71
|
|
|
if (in_array($userIp, $whitelist)) { |
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Provide a permission code for users to be able to access the site in test mode (UAT sites). This will |
|
80
|
|
|
* apply to any route other than those required to change your password. |
|
81
|
|
|
* |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
public function providePermissions() |
|
85
|
|
|
{ |
|
86
|
|
|
return [ |
|
87
|
|
|
'ACCESS_UAT_SERVER' => _t( |
|
88
|
|
|
__CLASS__ . '.UatServerPermission', |
|
89
|
|
|
'Allow users to use their accounts to access the UAT server' |
|
90
|
|
|
) |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths