summaryrefslogtreecommitdiff
path: root/app/Core/User/UserSession.php
blob: 0206be80b3a7b8ca16c91e44aaadcb3e2d621d71 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php

namespace Kanboard\Core\User;

use Kanboard\Core\Base;
use Kanboard\Core\Security\Role;

/**
 * User Session
 *
 * @package  user
 * @author   Frederic Guillot
 */
class UserSession extends Base
{
    /**
     * Refresh current session if necessary
     *
     * @access public
     * @param integer $user_id
     */
    public function refresh($user_id)
    {
        if ($this->getId() == $user_id) {
            $this->initialize($this->userModel->getById($user_id));
        }
    }

    /**
     * Update user session
     *
     * @access public
     * @param  array  $user
     */
    public function initialize(array $user)
    {
        foreach (array('password', 'is_admin', 'is_project_admin', 'twofactor_secret') as $column) {
            if (isset($user[$column])) {
                unset($user[$column]);
            }
        }

        $user['id'] = (int) $user['id'];
        $user['is_ldap_user'] = isset($user['is_ldap_user']) ? (bool) $user['is_ldap_user'] : false;
        $user['twofactor_activated'] = isset($user['twofactor_activated']) ? (bool) $user['twofactor_activated'] : false;

        session_set('user', $user);
        session_set('postAuthenticationValidated', false);
    }

    /**
     * Get user properties
     *
     * @access public
     * @return array
     */
    public function getAll()
    {
        return session_get('user');
    }

    /**
     * Get user application role
     *
     * @access public
     * @return string
     */
    public function getRole()
    {
        if (! $this->isLogged()) {
            return '';
        }

        return session_get('user')['role'];
    }

    /**
     * Return true if the user has validated the 2FA key
     *
     * @access public
     * @return bool
     */
    public function isPostAuthenticationValidated()
    {
        return session_is_true('postAuthenticationValidated');
    }

    /**
     * Validate 2FA for the current session
     *
     * @access public
     */
    public function validatePostAuthentication()
    {
        session_set('postAuthenticationValidated', true);
    }

    /**
     * Return true if the user has 2FA enabled
     *
     * @access public
     * @return bool
     */
    public function hasPostAuthentication()
    {
        if (! $this->isLogged()) {
            return false;
        }

        return session_get('user')['twofactor_activated'] === true;
    }

    /**
     * Disable 2FA for the current session
     *
     * @access public
     */
    public function disablePostAuthentication()
    {
        session_merge('user', ['twofactor_activated' => false]);
    }

    /**
     * Return true if the logged user is admin
     *
     * @access public
     * @return bool
     */
    public function isAdmin()
    {
        return $this->getRole() === Role::APP_ADMIN;
    }

    /**
     * Get the connected user id
     *
     * @access public
     * @return integer
     */
    public function getId()
    {
        if (! $this->isLogged()) {
            return 0;
        }

        return session_get('user')['id'];
    }

    /**
     * Get username
     *
     * @access public
     * @return string
     */
    public function getUsername()
    {
        if (! $this->isLogged()) {
            return '';
        }

        return session_get('user')['username'];
    }

    /**
     * Get user language
     *
     * @access public
     * @return string
     */
    public function getLanguage()
    {
        if (! $this->isLogged()) {
            return '';
        }

        return session_get('user')['language'];
    }

    /**
     * Get user timezone
     *
     * @access public
     * @return string
     */
    public function getTimezone()
    {
        if (! $this->isLogged()) {
            return '';
        }

        return session_get('user')['timezone'];
    }

    /**
     * Return true if subtask list toggle is active
     *
     * @access public
     * @return string
     */
    public function hasSubtaskListActivated()
    {
        return session_is_true('subtaskListToggle');
    }

    /**
     * Check is the user is connected
     *
     * @access public
     * @return bool
     */
    public function isLogged()
    {
        return session_exists('user') && session_get('user') !== [];
    }

    /**
     * Get project filters from the session
     *
     * @access public
     * @param  integer  $projectID
     * @return string
     */
    public function getFilters($projectID)
    {
        if (! session_exists('filters:'.$projectID)) {
            return 'status:open';
        }

        return session_get('filters:'.$projectID);
    }

    /**
     * Save project filters in the session
     *
     * @access public
     * @param  integer  $projectID
     * @param  string   $filters
     */
    public function setFilters($projectID, $filters)
    {
        session_set('filters:'.$projectID, $filters);
    }
}