Passed
Push — master ( bee4ae...fc5262 )
by Andreas
16:28
created

delete_session()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 $_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
    public function __construct($auth)
37
    {
38
        $this->_cookie_id .= midcom::get()->config->get('auth_backend_simple_cookie_id');
39
        parent::__construct($auth);
40
    }
41
42 1
    public function read_session(Request $request)
43
    {
44 1
        if (!$request->hasPreviousSession()) {
45 1
            return false;
46
        }
47
48
        $this->session = new midcom_services_session($this->_cookie_id);
49
        if (!$this->session->exists('userid')) {
50
            return false;
51
        }
52
        return [
53
            'userid' => $this->session->get('userid'),
54
            'clientip' => $this->session->get('clientip'),
55
            'timestamp' => $this->session->get('timestamp')
56
        ];
57
    }
58
59 58
    public function create_session($clientip, midcom_core_user $user) : bool
60
    {
61 58
        if (empty($clientip)) {
62 58
            $clientip = $_SERVER['REMOTE_ADDR'];
63
        }
64
65 58
        $this->session = new midcom_services_session($this->_cookie_id);
66 58
        $this->session->set('userid', $user->id);
67 58
        $this->session->set('clientip', $clientip);
68 58
        $this->session->set('timestamp', time());
69 58
        return midcom::get()->session->migrate();
70
    }
71
72
    public function update_session()
73
    {
74
        $this->session->set('timestamp', time());
75
    }
76
77 1
    public function delete_session()
78
    {
79 1
        midcom::get()->session->remove($this->_cookie_id);
80 1
    }
81
}
82