summaryrefslogtreecommitdiff
path: root/app/Core/User/UserSession.php
blob: 534e519240d88305c74f5b223f30f45da69c1a39 (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
<?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
{
    /**
     * 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;

        $this->sessionStorage->user = $user;
        $this->sessionStorage->postAuthenticationValidated = false;
    }

    /**
     * Get user application role
     *
     * @access public
     * @return string
     */
    public function getRole()
    {
        return $this->sessionStorage->user['role'];
    }

    /**
     * Return true if the user has validated the 2FA key
     *
     * @access public
     * @return bool
     */
    public function isPostAuthenticationValidated()
    {
        return isset($this->sessionStorage->postAuthenticationValidated) && $this->sessionStorage->postAuthenticationValidated === true;
    }

    /**
     * Validate 2FA for the current session
     *
     * @access public
     */
    public function validatePostAuthentication()
    {
        $this->sessionStorage->postAuthenticationValidated = true;
    }

    /**
     * Return true if the user has 2FA enabled
     *
     * @access public
     * @return bool
     */
    public function hasPostAuthentication()
    {
        return isset($this->sessionStorage->user['twofactor_activated']) && $this->sessionStorage->user['twofactor_activated'] === true;
    }

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

    /**
     * Return true if the logged user is admin
     *
     * @access public
     * @return bool
     */
    public function isAdmin()
    {
        return isset($this->sessionStorage->user['role']) && $this->sessionStorage->user['role'] === Role::APP_ADMIN;
    }

    /**
     * Get the connected user id
     *
     * @access public
     * @return integer
     */
    public function getId()
    {
        return isset($this->sessionStorage->user['id']) ? (int) $this->sessionStorage->user['id'] : 0;
    }

    /**
     * Get username
     *
     * @access public
     * @return string
     */
    public function getUsername()
    {
        return isset($this->sessionStorage->user['username']) ? $this->sessionStorage->user['username'] : '';
    }

    /**
     * Check is the user is connected
     *
     * @access public
     * @return bool
     */
    public function isLogged()
    {
        return isset($this->sessionStorage->user) && ! empty($this->sessionStorage->user);
    }

    /**
     * Get project filters from the session
     *
     * @access public
     * @param  integer  $project_id
     * @return string
     */
    public function getFilters($project_id)
    {
        return ! empty($this->sessionStorage->filters[$project_id]) ? $this->sessionStorage->filters[$project_id] : 'status:open';
    }

    /**
     * Save project filters in the session
     *
     * @access public
     * @param  integer  $project_id
     * @param  string   $filters
     */
    public function setFilters($project_id, $filters)
    {
        $this->sessionStorage->filters[$project_id] = $filters;
    }

    /**
     * Is board collapsed or expanded
     *
     * @access public
     * @param  integer  $project_id
     * @return boolean
     */
    public function isBoardCollapsed($project_id)
    {
        return ! empty($this->sessionStorage->boardCollapsed[$project_id]) ? $this->sessionStorage->boardCollapsed[$project_id] : false;
    }

    /**
     * Set board display mode
     *
     * @access public
     * @param  integer  $project_id
     * @param  boolean  $is_collapsed
     */
    public function setBoardDisplayMode($project_id, $is_collapsed)
    {
        $this->sessionStorage->boardCollapsed[$project_id] = $is_collapsed;
    }

    /**
     * Set comments sorting
     *
     * @access public
     * @param  string $order
     */
    public function setCommentSorting($order)
    {
        $this->sessionStorage->commentSorting = $order;
    }

    /**
     * Get comments sorting direction
     *
     * @access public
     * @return string
     */
    public function getCommentSorting()
    {
        return empty($this->sessionStorage->commentSorting) ? 'ASC' : $this->sessionStorage->commentSorting;
    }
}