Sessions.php 1.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

namespace Lu\LuAuth\Models;

use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Class Sessions.
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @owner   University of Latvia
 * @version 3.2
 * @since   06.06.2018
 *
 * @package Lu\LuAuth\Models
 */
class Sessions
{
    /**
     * Table name
     *
     * @var string
     */
    protected $table = 'be_sessions';

    /**
     * Set request type
     *
     * @var string
     */
    protected $type = 'be';

    /**
     * Users constructor. Set initial usage.
     *
     * @param string $type 'be' or 'fe'
     */
    public function __construct(string $type)
    {
        // Set config value
        $this->conf = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)->get('lu_auth');

        // Set used group
        if ($type == 'fe') {
            $this->table = 'fe_sessions';
        } else {
            $this->table = 'be_sessions';
        }

        // Set used group
        $this->type = $type == 'fe' ? 'fe' : 'be';
    }

    /**
     * Destroy user session
     */
    public function destroy()
    {
        // Delete user session
        $queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
        $queryBuilder->delete($this->table)->where(
            $queryBuilder->expr()->eq('ses_id', $queryBuilder->createNamedParameter($_COOKIE['fe_typo_user']))
        );
        $queryBuilder->execute();

        // Update cookie
        setcookie('fe_typo_user', false, time() - 3600, '/', null, null, null);
    }
}