Passed
Pull Request — master (#30)
by Robbie
03:06
created
src/Control/CwpBasicAuthMiddleware.php 1 patch
Indentation   +75 added lines, -75 removed lines patch added patch discarded remove patch
@@ -8,86 +8,86 @@
 block discarded – undo
8 8
 
9 9
 class CwpBasicAuthMiddleware extends BasicAuthMiddleware implements PermissionProvider
10 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 = [];
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 21
 
22
-    /**
23
-     * @return array
24
-     */
25
-    public function getWhitelistedIps()
26
-    {
27
-        return $this->whitelistedIps;
28
-    }
22
+	/**
23
+	 * @return array
24
+	 */
25
+	public function getWhitelistedIps()
26
+	{
27
+		return $this->whitelistedIps;
28
+	}
29 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
-    }
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 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
-    }
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 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
-        }
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 69
 
70
-        $userIp = $_SERVER['REMOTE_ADDR'];
71
-        if (in_array($userIp, $whitelist)) {
72
-            return true;
73
-        }
70
+		$userIp = $_SERVER['REMOTE_ADDR'];
71
+		if (in_array($userIp, $whitelist)) {
72
+			return true;
73
+		}
74 74
 
75
-        return false;
76
-    }
75
+		return false;
76
+	}
77 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
-    }
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 93
 }
Please login to merge, or discard this patch.
tests/Control/CwpBasicAuthMiddlewareTest.php 2 patches
Indentation   +88 added lines, -88 removed lines patch added patch discarded remove patch
@@ -13,92 +13,92 @@
 block discarded – undo
13 13
 
14 14
 class CwpBasicAuthMiddlewareTest extends SapphireTest
15 15
 {
16
-    /**
17
-     * @var CwpBasicAuthMiddleware
18
-     */
19
-    protected $middleware;
20
-
21
-    /**
22
-     * @var array
23
-     */
24
-    protected $originalServersVars = [];
25
-
26
-    protected function setUp()
27
-    {
28
-        parent::setUp();
29
-
30
-        $this->middleware = Injector::inst()->get(BasicAuthMiddleware::class);
31
-        $this->originalServersVars = $_SERVER;
32
-
33
-        Config::modify()->set(BasicAuth::class, 'ignore_cli', false);
34
-    }
35
-
36
-    protected function tearDown()
37
-    {
38
-        $_SERVER = $this->originalServersVars;
39
-
40
-        parent::tearDown();
41
-    }
42
-
43
-    public function testSetWhitelistedIps()
44
-    {
45
-        $this->middleware->setWhitelistedIps('127.0.0.1,127.0.0.2');
46
-        $this->assertSame([
47
-            '127.0.0.1',
48
-            '127.0.0.2',
49
-        ], $this->middleware->getWhitelistedIps(), 'Accepts comma delimited strings');
50
-
51
-        $this->middleware->setWhitelistedIps(['127.0.0.1']);
52
-        $this->assertSame(['127.0.0.1'], $this->middleware->getWhitelistedIps(), 'Accepts array values');
53
-    }
54
-
55
-    /**
56
-     * @param string $currentIp
57
-     * @param int $expected
58
-     * @dataProvider whitelistingProvider
59
-     */
60
-    public function testIpWhitelisting($currentIp, $expected)
61
-    {
62
-        // Enable basic auth everywhere
63
-        $this->middleware->setURLPatterns(['#.*#' => true]);
64
-
65
-        // Set a whitelisted IP address
66
-        $_SERVER['REMOTE_ADDR'] = $currentIp;
67
-        $this->middleware->setWhitelistedIps(['127.0.0.1']);
68
-
69
-        $response = $this->mockRequest();
70
-
71
-        $this->assertEquals($expected, $response->getStatusCode());
72
-    }
73
-
74
-    /**
75
-     * @return array[]
76
-     */
77
-    public function whitelistingProvider()
78
-    {
79
-        return [
80
-            'IP not in whitelist' => ['123.456.789.012', 401],
81
-            'IP in whitelist' => ['127.0.0.1', 200],
82
-        ];
83
-    }
84
-
85
-    public function testMiddlewareProvidesUatServerPermissions()
86
-    {
87
-        $this->assertArrayHasKey('ACCESS_UAT_SERVER', $this->middleware->providePermissions());
88
-    }
89
-
90
-    /**
91
-     * Perform a mock middleware request. Will return 200 if everything is OK.
92
-     *
93
-     * @param string $url
94
-     * @return HTTPResponse
95
-     */
96
-    protected function mockRequest($url = '/foo')
97
-    {
98
-        $request = new HTTPRequest('GET', $url);
99
-
100
-        return $this->middleware->process($request, function () {
101
-            return new HTTPResponse('OK', 200);
102
-        });
103
-    }
16
+	/**
17
+	 * @var CwpBasicAuthMiddleware
18
+	 */
19
+	protected $middleware;
20
+
21
+	/**
22
+	 * @var array
23
+	 */
24
+	protected $originalServersVars = [];
25
+
26
+	protected function setUp()
27
+	{
28
+		parent::setUp();
29
+
30
+		$this->middleware = Injector::inst()->get(BasicAuthMiddleware::class);
31
+		$this->originalServersVars = $_SERVER;
32
+
33
+		Config::modify()->set(BasicAuth::class, 'ignore_cli', false);
34
+	}
35
+
36
+	protected function tearDown()
37
+	{
38
+		$_SERVER = $this->originalServersVars;
39
+
40
+		parent::tearDown();
41
+	}
42
+
43
+	public function testSetWhitelistedIps()
44
+	{
45
+		$this->middleware->setWhitelistedIps('127.0.0.1,127.0.0.2');
46
+		$this->assertSame([
47
+			'127.0.0.1',
48
+			'127.0.0.2',
49
+		], $this->middleware->getWhitelistedIps(), 'Accepts comma delimited strings');
50
+
51
+		$this->middleware->setWhitelistedIps(['127.0.0.1']);
52
+		$this->assertSame(['127.0.0.1'], $this->middleware->getWhitelistedIps(), 'Accepts array values');
53
+	}
54
+
55
+	/**
56
+	 * @param string $currentIp
57
+	 * @param int $expected
58
+	 * @dataProvider whitelistingProvider
59
+	 */
60
+	public function testIpWhitelisting($currentIp, $expected)
61
+	{
62
+		// Enable basic auth everywhere
63
+		$this->middleware->setURLPatterns(['#.*#' => true]);
64
+
65
+		// Set a whitelisted IP address
66
+		$_SERVER['REMOTE_ADDR'] = $currentIp;
67
+		$this->middleware->setWhitelistedIps(['127.0.0.1']);
68
+
69
+		$response = $this->mockRequest();
70
+
71
+		$this->assertEquals($expected, $response->getStatusCode());
72
+	}
73
+
74
+	/**
75
+	 * @return array[]
76
+	 */
77
+	public function whitelistingProvider()
78
+	{
79
+		return [
80
+			'IP not in whitelist' => ['123.456.789.012', 401],
81
+			'IP in whitelist' => ['127.0.0.1', 200],
82
+		];
83
+	}
84
+
85
+	public function testMiddlewareProvidesUatServerPermissions()
86
+	{
87
+		$this->assertArrayHasKey('ACCESS_UAT_SERVER', $this->middleware->providePermissions());
88
+	}
89
+
90
+	/**
91
+	 * Perform a mock middleware request. Will return 200 if everything is OK.
92
+	 *
93
+	 * @param string $url
94
+	 * @return HTTPResponse
95
+	 */
96
+	protected function mockRequest($url = '/foo')
97
+	{
98
+		$request = new HTTPRequest('GET', $url);
99
+
100
+		return $this->middleware->process($request, function () {
101
+			return new HTTPResponse('OK', 200);
102
+		});
103
+	}
104 104
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@
 block discarded – undo
97 97
     {
98 98
         $request = new HTTPRequest('GET', $url);
99 99
 
100
-        return $this->middleware->process($request, function () {
100
+        return $this->middleware->process($request, function() {
101 101
             return new HTTPResponse('OK', 200);
102 102
         });
103 103
     }
Please login to merge, or discard this patch.