1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Security\MemberAuthenticator; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\Security\Member; |
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
8
|
|
|
use SilverStripe\Control\HTTPResponse; |
9
|
|
|
use SilverStripe\Control\Session; |
10
|
|
|
use SilverStripe\Control\Director; |
11
|
|
|
use SilverStripe\Security\AuthenticationHandler as AuthenticationHandlerInterface; |
12
|
|
|
use SilverStripe\ORM\ValidationException; |
13
|
|
|
use SilverStripe\Security\IdentityStore; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Authenticate a member pased on a session cookie |
17
|
|
|
*/ |
18
|
|
|
class SessionAuthenticationHandler implements AuthenticationHandlerInterface, IdentityStore |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
private $sessionVariable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the session variable name used to track member ID |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function getSessionVariable() |
29
|
|
|
{ |
30
|
|
|
return $this->sessionVariable; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the session variable name used to track member ID |
35
|
|
|
* |
36
|
|
|
* @param string $sessionVariable |
37
|
|
|
* @return null |
38
|
|
|
*/ |
39
|
|
|
public function setSessionVariable($sessionVariable) |
40
|
|
|
{ |
41
|
|
|
$this->sessionVariable = $sessionVariable; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inherit |
46
|
|
|
*/ |
47
|
|
|
public function authenticateRequest(HTTPRequest $request) |
48
|
|
|
{ |
49
|
|
|
// @todo couple the session to a request object |
50
|
|
|
// $session = $request->getSession(); |
51
|
|
|
|
52
|
|
|
if ($id = Session::get($this->getSessionVariable())) { |
53
|
|
|
// If ID is a bad ID it will be treated as if the user is not logged in, rather than throwing a |
54
|
|
|
// ValidationException |
55
|
|
|
return DataObject::get_by_id(Member::class, $id); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inherit |
63
|
|
|
*/ |
64
|
|
|
public function logIn(Member $member, $persistent, HTTPRequest $request) |
65
|
|
|
{ |
66
|
|
|
// @todo couple the session to a request object |
67
|
|
|
// $session = $request->getSession(); |
68
|
|
|
|
69
|
|
|
$this->regenerateSessionId(); |
70
|
|
|
Session::set($this->getSessionVariable(), $member->ID); |
71
|
|
|
|
72
|
|
|
// This lets apache rules detect whether the user has logged in |
73
|
|
|
// @todo make this a settign on the authentication handler |
74
|
|
|
if (Member::config()->login_marker_cookie) { |
75
|
|
|
Cookie::set(Member::config()->login_marker_cookie, 1, 0); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Regenerate the session_id. |
81
|
|
|
*/ |
82
|
|
|
protected static function regenerateSessionId() |
83
|
|
|
{ |
84
|
|
|
if (!Member::config()->session_regenerate_id) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// This can be called via CLI during testing. |
89
|
|
|
if (Director::is_cli()) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$file = ''; |
94
|
|
|
$line = ''; |
95
|
|
|
|
96
|
|
|
// @ is to supress win32 warnings/notices when session wasn't cleaned up properly |
97
|
|
|
// There's nothing we can do about this, because it's an operating system function! |
98
|
|
|
if (!headers_sent($file, $line)) { |
99
|
|
|
@session_regenerate_id(true); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
/** |
103
|
|
|
* @inherit |
104
|
|
|
*/ |
105
|
|
|
public function logOut(HTTPRequest $request) |
106
|
|
|
{ |
107
|
|
|
// @todo couple the session to a request object |
108
|
|
|
// $session = $request->getSession(); |
109
|
|
|
|
110
|
|
|
Session::clear($this->getSessionVariable()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: