Passed
Push — master ( d9761c...9b08f6 )
by Andreas
10:15
created

read_session()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.087

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 14
ccs 3
cts 10
cp 0.3
crap 6.087
rs 9.9666
c 0
b 0
f 0
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 string $_cookie_id = 'midcom_services_auth_backend_simple-';
27
28
    private midcom_services_session $session;
29
30
    /**
31
     * Read the configuration
32
     */
33 1
    public function __construct(string $cookie_id)
34
    {
35 1
        $this->_cookie_id .= $cookie_id;
36
    }
37
38 350
    public function read_session(Request $request) : ?array
39
    {
40 350
        if (!$request->hasPreviousSession()) {
41 350
            return null;
42
        }
43
44
        $this->session = new midcom_services_session($this->_cookie_id);
45
        if (!$this->session->exists('userid')) {
46
            return null;
47
        }
48
        return [
49
            'userid' => $this->session->get('userid'),
50
            'clientip' => $this->session->get('clientip'),
51
            'timestamp' => $this->session->get('timestamp')
52
        ];
53
    }
54
55 65
    public function create_session(?string $clientip, midcom_core_user $user) : bool
56
    {
57 65
        $this->session = new midcom_services_session($this->_cookie_id);
58 65
        $this->session->set('userid', $user->id);
59 65
        $this->session->set('clientip', $clientip ?? $_SERVER['REMOTE_ADDR']);
60 65
        $this->session->set('timestamp', time());
61 65
        return midcom::get()->session->migrate();
62
    }
63
64
    public function update_session()
65
    {
66
        $this->session->set('timestamp', time());
67
    }
68
69 1
    public function delete_session()
70
    {
71 1
        midcom::get()->session->remove($this->_cookie_id);
72
    }
73
}
74