1<?php
2
3namespace Kanboard\Controller;
4
5use Kanboard\Core\Controller\PageNotFoundException;
6use Kanboard\Model\ProjectModel;
7
8/**
9 * Class UserViewController
10 *
11 * @package Kanboard\Controller
12 * @author  Frederic Guillot
13 */
14class UserViewController extends BaseController
15{
16    /**
17     * Public user profile
18     *
19     * @access public
20     * @throws PageNotFoundException
21     */
22    public function profile()
23    {
24        $user = $this->userModel->getById($this->request->getIntegerParam('user_id'));
25
26        if (empty($user)) {
27            throw new PageNotFoundException();
28        }
29
30        $this->response->html($this->helper->layout->app('user_view/profile', array(
31            'title' => $user['name'] ?: $user['username'],
32            'user'  => $user,
33        )));
34    }
35
36    /**
37     * Display user information
38     *
39     * @access public
40     */
41    public function show()
42    {
43        $user = $this->getUser();
44        $this->response->html($this->helper->layout->user('user_view/show', array(
45            'user'      => $user,
46            'timezones' => $this->timezoneModel->getTimezones(true),
47            'languages' => $this->languageModel->getLanguages(true),
48        )));
49    }
50
51    /**
52     * Display timesheet
53     *
54     * @access public
55     */
56    public function timesheet()
57    {
58        $user = $this->getUser();
59
60        $subtask_paginator = $this->paginator
61            ->setUrl('UserViewController', 'timesheet', array('user_id' => $user['id'], 'pagination' => 'subtasks'))
62            ->setMax(20)
63            ->setOrder('start')
64            ->setDirection('DESC')
65            ->setQuery($this->subtaskTimeTrackingModel->getUserQuery($user['id']))
66            ->calculateOnlyIf($this->request->getStringParam('pagination') === 'subtasks');
67
68        $this->response->html($this->helper->layout->user('user_view/timesheet', array(
69            'subtask_paginator' => $subtask_paginator,
70            'user'              => $user,
71        )));
72    }
73
74    /**
75     * Display last password reset
76     *
77     * @access public
78     */
79    public function passwordReset()
80    {
81        $user = $this->getUser();
82        $this->response->html($this->helper->layout->user('user_view/password_reset', array(
83            'tokens' => $this->passwordResetModel->getAll($user['id']),
84            'user'   => $user,
85        )));
86    }
87
88    /**
89     * Display last connections
90     *
91     * @access public
92     */
93    public function lastLogin()
94    {
95        $user = $this->getUser();
96        $this->response->html($this->helper->layout->user('user_view/last', array(
97            'last_logins' => $this->lastLoginModel->getAll($user['id']),
98            'user'        => $user,
99        )));
100    }
101
102    /**
103     * Display user sessions
104     *
105     * @access public
106     */
107    public function sessions()
108    {
109        $user = $this->getUser();
110        $this->response->html($this->helper->layout->user('user_view/sessions', array(
111            'sessions' => $this->rememberMeSessionModel->getAll($user['id']),
112            'user'     => $user,
113        )));
114    }
115
116    /**
117     * Remove a "RememberMe" token
118     *
119     * @access public
120     */
121    public function removeSession()
122    {
123        $this->checkCSRFParam();
124        $user = $this->getUser();
125        $this->rememberMeSessionModel->remove($this->request->getIntegerParam('id'));
126
127        if ($this->request->isAjax()) {
128            $this->sessions();
129        } else {
130            $this->response->redirect($this->helper->url->to('UserViewController', 'sessions', array('user_id' => $user['id'])), true);
131        }
132    }
133
134    /**
135     * Display user notifications
136     *
137     * @access public
138     */
139    public function notifications()
140    {
141        $user = $this->getUser();
142
143        if ($this->request->isPost()) {
144            $values = $this->request->getValues();
145            $this->userNotificationModel->saveSettings($user['id'], $values);
146            $this->flash->success(t('User updated successfully.'));
147            $this->response->redirect($this->helper->url->to('UserViewController', 'notifications', array('user_id' => $user['id'])), true);
148            return;
149        }
150
151        $this->response->html($this->helper->layout->user('user_view/notifications', array(
152            'projects'      => $this->projectUserRoleModel->getProjectsByUser($user['id'], array(ProjectModel::ACTIVE)),
153            'notifications' => $this->userNotificationModel->readSettings($user['id']),
154            'types'         => $this->userNotificationTypeModel->getTypes(),
155            'filters'       => $this->userNotificationFilterModel->getFilters(),
156            'user'          => $user,
157        )));
158    }
159
160    /**
161     * Display user integrations
162     *
163     * @access public
164     */
165    public function integrations()
166    {
167        $user = $this->getUser();
168
169        if ($this->request->isPost()) {
170            $values = $this->request->getValues();
171            $this->userMetadataModel->save($user['id'], $values);
172            $this->flash->success(t('User updated successfully.'));
173            $this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'])), true);
174            return;
175        }
176
177        $this->response->html($this->helper->layout->user('user_view/integrations', array(
178            'user'   => $user,
179            'values' => $this->userMetadataModel->getAll($user['id']),
180        )));
181    }
182
183    /**
184     * Display external accounts
185     *
186     * @access public
187     */
188    public function external()
189    {
190        $user = $this->getUser();
191        $this->response->html($this->helper->layout->user('user_view/external', array(
192            'last_logins' => $this->lastLoginModel->getAll($user['id']),
193            'user'        => $user,
194        )));
195    }
196
197    /**
198     * Public access management
199     *
200     * @access public
201     */
202    public function share()
203    {
204        $user = $this->getUser();
205        $switch = $this->request->getStringParam('switch');
206
207        if ($switch === 'enable' || $switch === 'disable') {
208            $this->checkCSRFParam();
209
210            if ($this->userModel->{$switch . 'PublicAccess'}($user['id'])) {
211                $this->flash->success(t('User updated successfully.'));
212            } else {
213                $this->flash->failure(t('Unable to update this user.'));
214            }
215
216            if (! $this->request->isAjax()) {
217                $this->response->redirect($this->helper->url->to('UserViewController', 'share', array('user_id' => $user['id'])), true);
218                return;
219            }
220
221            $user = $this->getUser();
222        }
223
224        $this->response->html($this->helper->layout->user('user_view/share', array(
225            'user'  => $user,
226            'title' => t('Public access'),
227        )));
228    }
229}
230