1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package midcom.services |
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The simple auth backend uses cookies to store a session identifier which |
13
|
|
|
* consists of the midgard person GUID. |
14
|
|
|
* |
15
|
|
|
* The basic cookie id (username prefix) is taken from the config option |
16
|
|
|
* <i>auth_backend_simple_cookie_id</i>, which defaults to 1 |
17
|
|
|
* |
18
|
|
|
* @package midcom.services |
19
|
|
|
*/ |
20
|
|
|
class midcom_services_auth_backend_simple extends midcom_services_auth_backend |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The auto-generated cookie ID for which this login session is valid. This consists |
24
|
|
|
* of a static string with the host GUID concatenated to it. |
25
|
|
|
*/ |
26
|
|
|
private $_cookie_id = 'midcom_services_auth_backend_simple-'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var midcom_services_session |
30
|
|
|
*/ |
31
|
|
|
private $session; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Read the configuration |
35
|
|
|
*/ |
36
|
1 |
|
public function __construct(string $cookie_id) |
37
|
|
|
{ |
38
|
1 |
|
$this->_cookie_id .= $cookie_id; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function read_session(Request $request) : ?array |
42
|
|
|
{ |
43
|
1 |
|
if (!$request->hasPreviousSession()) { |
44
|
1 |
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->session = new midcom_services_session($this->_cookie_id); |
48
|
|
|
if (!$this->session->exists('userid')) { |
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
return [ |
52
|
|
|
'userid' => $this->session->get('userid'), |
53
|
|
|
'clientip' => $this->session->get('clientip'), |
54
|
|
|
'timestamp' => $this->session->get('timestamp') |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
65 |
|
public function create_session(?string $clientip, midcom_core_user $user) : bool |
59
|
|
|
{ |
60
|
65 |
|
if (empty($clientip)) { |
61
|
65 |
|
$clientip = $_SERVER['REMOTE_ADDR']; |
62
|
|
|
} |
63
|
|
|
|
64
|
65 |
|
$this->session = new midcom_services_session($this->_cookie_id); |
65
|
65 |
|
$this->session->set('userid', $user->id); |
66
|
65 |
|
$this->session->set('clientip', $clientip); |
67
|
65 |
|
$this->session->set('timestamp', time()); |
68
|
65 |
|
return midcom::get()->session->migrate(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function update_session() |
72
|
|
|
{ |
73
|
|
|
$this->session->set('timestamp', time()); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function delete_session() |
77
|
|
|
{ |
78
|
1 |
|
midcom::get()->session->remove($this->_cookie_id); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|