silverstripe /
silverstripe-framework
| 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); |
||
| 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 | $member = Security::getCurrentUser(); |
||
| 78 | if ($member) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 79 | $member->beforeMemberLoggedOut($request); |
||
| 80 | } |
||
| 81 | |||
| 82 | foreach ($this->getHandlers() as $handler) { |
||
| 83 | $handler->logOut($request); |
||
| 84 | } |
||
| 85 | |||
| 86 | Security::setCurrentUser(null); |
||
| 87 | |||
| 88 | if ($member) { |
||
|
0 ignored issues
–
show
|
|||
| 89 | $member->afterMemberLoggedOut($request); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 |