Completed
Pull Request — master (#7007)
by Simon
08:19
created

RequestAuthenticationHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandlers() 0 4 1
A setHandlers() 0 5 1
A authenticateRequest() 0 12 3
A logIn() 0 11 2
A logOut() 0 8 2
1
<?php
2
3
namespace SilverStripe\Security;
4
5
use SilverStripe\Control\HTTPRequest;
6
7
/**
8
 * Core authentication handler / store
9
 */
10
class RequestAuthenticationHandler implements AuthenticationHandler
11
{
12
    /**
13
     * @var AuthenticationHandler[]
14
     */
15
    protected $handlers = [];
16
17
    /**
18
     * This method currently uses a fallback as loading the handlers via YML has proven unstable
19
     *
20
     * @return AuthenticationHandler[]
21
     */
22
    protected function getHandlers()
23
    {
24
        return $this->handlers;
25
    }
26
27
    /**
28
     * Set an associative array of handlers
29
     *
30
     * @param AuthenticationHandler[] $handlers
31
     * @return $this
32
     */
33
    public function setHandlers(array $handlers)
34
    {
35
        $this->handlers = $handlers;
36
        return $this;
37
    }
38
39
    public function authenticateRequest(HTTPRequest $request)
40
    {
41
        /** @var AuthenticationHandler $handler */
42
        foreach ($this->getHandlers() as $name => $handler) {
43
            // in order to add cookies, etc
44
            $member = $handler->authenticateRequest($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $member is correct as $handler->authenticateRequest($request) (which targets SilverStripe\Security\Au...::authenticateRequest()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
45
            if ($member) {
46
                Security::setCurrentUser($member);
47
                return;
48
            }
49
        }
50
    }
51
    /**
52
     * Log into the identity-store handlers attached to this request filter
53
     *
54
     * @param Member $member
55
     * @param bool $persistent
56
     * @param HTTPRequest $request
57
     */
58
    public function logIn(Member $member, $persistent = false, HTTPRequest $request = null)
59
    {
60
        $member->beforeMemberLoggedIn();
61
62
        foreach ($this->getHandlers() as $handler) {
63
            $handler->logIn($member, $persistent, $request);
64
        }
65
66
        Security::setCurrentUser($member);
67
        $member->afterMemberLoggedIn();
68
    }
69
70
    /**
71
     * Log out of all the identity-store handlers attached to this request filter
72
     *
73
     * @param HTTPRequest $request
74
     */
75
    public function logOut(HTTPRequest $request = null)
76
    {
77
        foreach ($this->getHandlers() as $handler) {
78
            $handler->logOut($request);
79
        }
80
81
        Security::setCurrentUser(null);
82
    }
83
}
84