Passed
Pull Request — master (#30)
by Robbie
03:06
created

CwpBasicAuthMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setWhitelistedIps() 0 7 2
A checkMatchingURL() 0 6 2
A ipMatchesWhitelist() 0 14 3
A providePermissions() 0 6 1
A getWhitelistedIps() 0 3 1
1
<?php
2
3
namespace CWP\Core\Control;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Security\BasicAuthMiddleware;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Security\BasicAuthMiddleware was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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