summaryrefslogtreecommitdiff
path: root/app/Controller/User.php
blob: f7d7d2e09090636e3f53f0e0e2664d4bd1f135b7 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php

namespace Kanboard\Controller;

use Kanboard\Notification\Mail as MailNotification;
use Kanboard\Model\Project as ProjectModel;
use Kanboard\Core\Security\Role;

/**
 * User controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class User extends Base
{
    /**
     * List all users
     *
     * @access public
     */
    public function index()
    {
        $paginator = $this->paginator
                ->setUrl('user', 'index')
                ->setMax(30)
                ->setOrder('username')
                ->setQuery($this->user->getQuery())
                ->calculate();

        $this->response->html(
            $this->helper->layout->app('user/index', array(
                'title' => t('Users').' ('.$paginator->getTotal().')',
                'paginator' => $paginator,
            )
        ));
    }

    /**
     * Public user profile
     *
     * @access public
     */
    public function profile()
    {
        $user = $this->user->getById($this->request->getIntegerParam('user_id'));

        if (empty($user)) {
            $this->notfound();
        }

        $this->response->html(
            $this->helper->layout->app('user/profile', array(
                'title' => $user['name'] ?: $user['username'],
                'user' => $user,
            )
        ));
    }

    /**
     * Display a form to create a new user
     *
     * @access public
     */
    public function create(array $values = array(), array $errors = array())
    {
        $is_remote = $this->request->getIntegerParam('remote') == 1 || (isset($values['is_ldap_user']) && $values['is_ldap_user'] == 1);

        $this->response->html($this->helper->layout->app($is_remote ? 'user/create_remote' : 'user/create_local', array(
            'timezones' => $this->config->getTimezones(true),
            'languages' => $this->config->getLanguages(true),
            'roles' => $this->role->getApplicationRoles(),
            'projects' => $this->project->getList(),
            'errors' => $errors,
            'values' => $values + array('role' => Role::APP_USER),
            'title' => t('New user')
        )));
    }

    /**
     * Validate and save a new user
     *
     * @access public
     */
    public function save()
    {
        $values = $this->request->getValues();
        list($valid, $errors) = $this->userValidator->validateCreation($values);

        if ($valid) {
            $project_id = empty($values['project_id']) ? 0 : $values['project_id'];
            unset($values['project_id']);

            $user_id = $this->user->create($values);

            if ($user_id !== false) {
                $this->projectUserRole->addUser($project_id, $user_id, Role::PROJECT_MEMBER);

                if (! empty($values['notifications_enabled'])) {
                    $this->userNotificationType->saveSelectedTypes($user_id, array(MailNotification::TYPE));
                }

                $this->flash->success(t('User created successfully.'));
                $this->response->redirect($this->helper->url->to('user', 'show', array('user_id' => $user_id)));
            } else {
                $this->flash->failure(t('Unable to create your user.'));
                $values['project_id'] = $project_id;
            }
        }

        $this->create($values, $errors);
    }

    /**
     * Display user information
     *
     * @access public
     */
    public function show()
    {
        $user = $this->getUser();
        $this->response->html($this->helper->layout->user('user/show', array(
            'user' => $user,
            'timezones' => $this->config->getTimezones(true),
            'languages' => $this->config->getLanguages(true),
        )));
    }

    /**
     * Display timesheet
     *
     * @access public
     */
    public function timesheet()
    {
        $user = $this->getUser();

        $subtask_paginator = $this->paginator
            ->setUrl('user', 'timesheet', array('user_id' => $user['id'], 'pagination' => 'subtasks'))
            ->setMax(20)
            ->setOrder('start')
            ->setDirection('DESC')
            ->setQuery($this->subtaskTimeTracking->getUserQuery($user['id']))
            ->calculateOnlyIf($this->request->getStringParam('pagination') === 'subtasks');

        $this->response->html($this->helper->layout->user('user/timesheet', array(
            'subtask_paginator' => $subtask_paginator,
            'user' => $user,
        )));
    }

    /**
     * Display last password reset
     *
     * @access public
     */
    public function passwordReset()
    {
        $user = $this->getUser();
        $this->response->html($this->helper->layout->user('user/password_reset', array(
            'tokens' => $this->passwordReset->getAll($user['id']),
            'user' => $user,
        )));
    }

    /**
     * Display last connections
     *
     * @access public
     */
    public function last()
    {
        $user = $this->getUser();
        $this->response->html($this->helper->layout->user('user/last', array(
            'last_logins' => $this->lastLogin->getAll($user['id']),
            'user' => $user,
        )));
    }

    /**
     * Display user sessions
     *
     * @access public
     */
    public function sessions()
    {
        $user = $this->getUser();
        $this->response->html($this->helper->layout->user('user/sessions', array(
            'sessions' => $this->rememberMeSession->getAll($user['id']),
            'user' => $user,
        )));
    }

    /**
     * Remove a "RememberMe" token
     *
     * @access public
     */
    public function removeSession()
    {
        $this->checkCSRFParam();
        $user = $this->getUser();
        $this->rememberMeSession->remove($this->request->getIntegerParam('id'));
        $this->response->redirect($this->helper->url->to('user', 'sessions', array('user_id' => $user['id'])));
    }

    /**
     * Display user notifications
     *
     * @access public
     */
    public function notifications()
    {
        $user = $this->getUser();

        if ($this->request->isPost()) {
            $values = $this->request->getValues();
            $this->userNotification->saveSettings($user['id'], $values);
            $this->flash->success(t('User updated successfully.'));
            $this->response->redirect($this->helper->url->to('user', 'notifications', array('user_id' => $user['id'])));
        }

        $this->response->html($this->helper->layout->user('user/notifications', array(
            'projects' => $this->projectUserRole->getProjectsByUser($user['id'], array(ProjectModel::ACTIVE)),
            'notifications' => $this->userNotification->readSettings($user['id']),
            'types' => $this->userNotificationType->getTypes(),
            'filters' => $this->userNotificationFilter->getFilters(),
            'user' => $user,
        )));
    }

    /**
     * Display user integrations
     *
     * @access public
     */
    public function integrations()
    {
        $user = $this->getUser();

        if ($this->request->isPost()) {
            $values = $this->request->getValues();
            $this->userMetadata->save($user['id'], $values);
            $this->flash->success(t('User updated successfully.'));
            $this->response->redirect($this->helper->url->to('user', 'integrations', array('user_id' => $user['id'])));
        }

        $this->response->html($this->helper->layout->user('user/integrations', array(
            'user' => $user,
            'values' => $this->userMetadata->getall($user['id']),
        )));
    }

    /**
     * Display external accounts
     *
     * @access public
     */
    public function external()
    {
        $user = $this->getUser();
        $this->response->html($this->helper->layout->user('user/external', array(
            'last_logins' => $this->lastLogin->getAll($user['id']),
            'user' => $user,
        )));
    }

    /**
     * Public access management
     *
     * @access public
     */
    public function share()
    {
        $user = $this->getUser();
        $switch = $this->request->getStringParam('switch');

        if ($switch === 'enable' || $switch === 'disable') {
            $this->checkCSRFParam();

            if ($this->user->{$switch.'PublicAccess'}($user['id'])) {
                $this->flash->success(t('User updated successfully.'));
            } else {
                $this->flash->failure(t('Unable to update this user.'));
            }

            $this->response->redirect($this->helper->url->to('user', 'share', array('user_id' => $user['id'])));
        }

        $this->response->html($this->helper->layout->user('user/share', array(
            'user' => $user,
            'title' => t('Public access'),
        )));
    }

    /**
     * Password modification
     *
     * @access public
     */
    public function password()
    {
        $user = $this->getUser();
        $values = array('id' => $user['id']);
        $errors = array();

        if ($this->request->isPost()) {
            $values = $this->request->getValues();
            list($valid, $errors) = $this->userValidator->validatePasswordModification($values);

            if ($valid) {
                if ($this->user->update($values)) {
                    $this->flash->success(t('Password modified successfully.'));
                } else {
                    $this->flash->failure(t('Unable to change the password.'));
                }

                $this->response->redirect($this->helper->url->to('user', 'show', array('user_id' => $user['id'])));
            }
        }

        $this->response->html($this->helper->layout->user('user/password', array(
            'values' => $values,
            'errors' => $errors,
            'user' => $user,
        )));
    }

    /**
     * Display a form to edit a user
     *
     * @access public
     */
    public function edit()
    {
        $user = $this->getUser();
        $values = $user;
        $errors = array();

        unset($values['password']);

        if ($this->request->isPost()) {
            $values = $this->request->getValues();

            if (! $this->userSession->isAdmin()) {
                if (isset($values['role'])) {
                    unset($values['role']);
                }
            }

            list($valid, $errors) = $this->userValidator->validateModification($values);

            if ($valid) {
                if ($this->user->update($values)) {
                    $this->flash->success(t('User updated successfully.'));
                } else {
                    $this->flash->failure(t('Unable to update your user.'));
                }

                $this->response->redirect($this->helper->url->to('user', 'show', array('user_id' => $user['id'])));
            }
        }

        $this->response->html($this->helper->layout->user('user/edit', array(
            'values' => $values,
            'errors' => $errors,
            'user' => $user,
            'timezones' => $this->config->getTimezones(true),
            'languages' => $this->config->getLanguages(true),
            'roles' => $this->role->getApplicationRoles(),
        )));
    }

    /**
     * Display a form to edit authentication
     *
     * @access public
     */
    public function authentication()
    {
        $user = $this->getUser();
        $values = $user;
        $errors = array();

        unset($values['password']);

        if ($this->request->isPost()) {
            $values = $this->request->getValues() + array('disable_login_form' => 0, 'is_ldap_user' => 0);
            list($valid, $errors) = $this->userValidator->validateModification($values);

            if ($valid) {
                if ($this->user->update($values)) {
                    $this->flash->success(t('User updated successfully.'));
                } else {
                    $this->flash->failure(t('Unable to update your user.'));
                }

                $this->response->redirect($this->helper->url->to('user', 'authentication', array('user_id' => $user['id'])));
            }
        }

        $this->response->html($this->helper->layout->user('user/authentication', array(
            'values' => $values,
            'errors' => $errors,
            'user' => $user,
        )));
    }
}