Passed
Branch master (05c266)
by Andreas
09:55
created

midcom_services_auth_backend_simple   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 65.22%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 59
ccs 15
cts 23
cp 0.6522
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

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