Completed
Push — master ( daed8c...cf758d )
by Damian
08:03
created

setAuthenticationHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Security;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Control\HTTPResponse;
7
use SilverStripe\Control\Middleware\HTTPMiddleware;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\ORM\ValidationException;
10
11
class AuthenticationMiddleware implements HTTPMiddleware
12
{
13
    use Configurable;
14
15
    /**
16
     * @var AuthenticationHandler
17
     */
18
    protected $authenticationHandler;
19
20
    /**
21
     * @return AuthenticationHandler
22
     */
23
    public function getAuthenticationHandler()
24
    {
25
        return $this->authenticationHandler;
26
    }
27
28
    /**
29
     * @param AuthenticationHandler $authenticationHandler
30
     * @return $this
31
     */
32
    public function setAuthenticationHandler(AuthenticationHandler $authenticationHandler)
33
    {
34
        $this->authenticationHandler = $authenticationHandler;
35
        return $this;
36
    }
37
38
    /**
39
     * Identify the current user from the request
40
     *
41
     * @param HTTPRequest $request
42
     * @param callable $delegate
43
     * @return HTTPResponse
44
     */
45
    public function process(HTTPRequest $request, callable $delegate)
46
    {
47
        if (Security::database_is_ready()) {
48
            try {
49
                $this
50
                    ->getAuthenticationHandler()
51
                    ->authenticateRequest($request);
52
            } catch (ValidationException $e) {
53
                return new HTTPResponse(
54
                    "Bad log-in details: " . $e->getMessage(),
55
                    400
56
                );
57
            }
58
        }
59
60
        return $delegate($request);
61
    }
62
}
63