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

midcom_services_auth_backend_simple   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 56.52%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 52
ccs 13
cts 23
cp 0.5652
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A read_session() 0 14 3
A delete_session() 0 3 1
A create_session() 0 7 1
A update_session() 0 3 1
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