/app/Controller/User.php

https://gitlab.com/x33n/kanboard · PHP · 407 lines · 254 code · 63 blank · 90 comment · 32 complexity · 189f63d24c6496971f45b5815435caef MD5 · raw file

  1. <?php
  2. namespace Controller;
  3. /**
  4. * User controller
  5. *
  6. * @package controller
  7. * @author Frederic Guillot
  8. */
  9. class User extends Base
  10. {
  11. /**
  12. * Common layout for user views
  13. *
  14. * @access protected
  15. * @param string $template Template name
  16. * @param array $params Template parameters
  17. * @return string
  18. */
  19. protected function layout($template, array $params)
  20. {
  21. $content = $this->template->render($template, $params);
  22. $params['user_content_for_layout'] = $content;
  23. $params['board_selector'] = $this->projectPermission->getAllowedProjects($this->userSession->getId());
  24. if (isset($params['user'])) {
  25. $params['title'] = ($params['user']['name'] ?: $params['user']['username']).' (#'.$params['user']['id'].')';
  26. }
  27. return $this->template->layout('user/layout', $params);
  28. }
  29. /**
  30. * List all users
  31. *
  32. * @access public
  33. */
  34. public function index()
  35. {
  36. $paginator = $this->paginator
  37. ->setUrl('user', 'index')
  38. ->setMax(30)
  39. ->setOrder('username')
  40. ->setQuery($this->user->getQuery())
  41. ->calculate();
  42. $this->response->html(
  43. $this->template->layout('user/index', array(
  44. 'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()),
  45. 'title' => t('Users').' ('.$paginator->getTotal().')',
  46. 'paginator' => $paginator,
  47. )));
  48. }
  49. /**
  50. * Display a form to create a new user
  51. *
  52. * @access public
  53. */
  54. public function create(array $values = array(), array $errors = array())
  55. {
  56. $is_remote = $this->request->getIntegerParam('remote') == 1 || (isset($values['is_ldap_user']) && $values['is_ldap_user'] == 1);
  57. $this->response->html($this->template->layout($is_remote ? 'user/create_remote' : 'user/create_local', array(
  58. 'timezones' => $this->config->getTimezones(true),
  59. 'languages' => $this->config->getLanguages(true),
  60. 'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()),
  61. 'projects' => $this->project->getList(),
  62. 'errors' => $errors,
  63. 'values' => $values,
  64. 'title' => t('New user')
  65. )));
  66. }
  67. /**
  68. * Validate and save a new user
  69. *
  70. * @access public
  71. */
  72. public function save()
  73. {
  74. $values = $this->request->getValues();
  75. list($valid, $errors) = $this->user->validateCreation($values);
  76. if ($valid) {
  77. $project_id = empty($values['project_id']) ? 0 : $values['project_id'];
  78. unset($values['project_id']);
  79. $user_id = $this->user->create($values);
  80. if ($user_id !== false) {
  81. $this->projectPermission->addMember($project_id, $user_id);
  82. $this->session->flash(t('User created successfully.'));
  83. $this->response->redirect($this->helper->url->to('user', 'show', array('user_id' => $user_id)));
  84. }
  85. else {
  86. $this->session->flashError(t('Unable to create your user.'));
  87. $values['project_id'] = $project_id;
  88. }
  89. }
  90. $this->create($values, $errors);
  91. }
  92. /**
  93. * Display user information
  94. *
  95. * @access public
  96. */
  97. public function show()
  98. {
  99. $user = $this->getUser();
  100. $this->response->html($this->layout('user/show', array(
  101. 'user' => $user,
  102. 'timezones' => $this->config->getTimezones(true),
  103. 'languages' => $this->config->getLanguages(true),
  104. )));
  105. }
  106. /**
  107. * Display timesheet
  108. *
  109. * @access public
  110. */
  111. public function timesheet()
  112. {
  113. $user = $this->getUser();
  114. $subtask_paginator = $this->paginator
  115. ->setUrl('user', 'timesheet', array('user_id' => $user['id'], 'pagination' => 'subtasks'))
  116. ->setMax(20)
  117. ->setOrder('start')
  118. ->setDirection('DESC')
  119. ->setQuery($this->subtaskTimeTracking->getUserQuery($user['id']))
  120. ->calculateOnlyIf($this->request->getStringParam('pagination') === 'subtasks');
  121. $this->response->html($this->layout('user/timesheet', array(
  122. 'subtask_paginator' => $subtask_paginator,
  123. 'user' => $user,
  124. )));
  125. }
  126. /**
  127. * Display last connections
  128. *
  129. * @access public
  130. */
  131. public function last()
  132. {
  133. $user = $this->getUser();
  134. $this->response->html($this->layout('user/last', array(
  135. 'last_logins' => $this->lastLogin->getAll($user['id']),
  136. 'user' => $user,
  137. )));
  138. }
  139. /**
  140. * Display user sessions
  141. *
  142. * @access public
  143. */
  144. public function sessions()
  145. {
  146. $user = $this->getUser();
  147. $this->response->html($this->layout('user/sessions', array(
  148. 'sessions' => $this->authentication->backend('rememberMe')->getAll($user['id']),
  149. 'user' => $user,
  150. )));
  151. }
  152. /**
  153. * Remove a "RememberMe" token
  154. *
  155. * @access public
  156. */
  157. public function removeSession()
  158. {
  159. $this->checkCSRFParam();
  160. $user = $this->getUser();
  161. $this->authentication->backend('rememberMe')->remove($this->request->getIntegerParam('id'));
  162. $this->response->redirect($this->helper->url->to('user', 'session', array('user_id' => $user['id'])));
  163. }
  164. /**
  165. * Display user notifications
  166. *
  167. * @access public
  168. */
  169. public function notifications()
  170. {
  171. $user = $this->getUser();
  172. if ($this->request->isPost()) {
  173. $values = $this->request->getValues();
  174. $this->notification->saveSettings($user['id'], $values);
  175. $this->session->flash(t('User updated successfully.'));
  176. $this->response->redirect($this->helper->url->to('user', 'notifications', array('user_id' => $user['id'])));
  177. }
  178. $this->response->html($this->layout('user/notifications', array(
  179. 'projects' => $this->projectPermission->getMemberProjects($user['id']),
  180. 'notifications' => $this->notification->readSettings($user['id']),
  181. 'user' => $user,
  182. )));
  183. }
  184. /**
  185. * Display external accounts
  186. *
  187. * @access public
  188. */
  189. public function external()
  190. {
  191. $user = $this->getUser();
  192. $this->response->html($this->layout('user/external', array(
  193. 'last_logins' => $this->lastLogin->getAll($user['id']),
  194. 'user' => $user,
  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. if ($switch === 'enable' || $switch === 'disable') {
  207. $this->checkCSRFParam();
  208. if ($this->user->{$switch.'PublicAccess'}($user['id'])) {
  209. $this->session->flash(t('User updated successfully.'));
  210. } else {
  211. $this->session->flashError(t('Unable to update this user.'));
  212. }
  213. $this->response->redirect($this->helper->url->to('user', 'share', array('user_id' => $user['id'])));
  214. }
  215. $this->response->html($this->layout('user/share', array(
  216. 'user' => $user,
  217. 'title' => t('Public access'),
  218. )));
  219. }
  220. /**
  221. * Password modification
  222. *
  223. * @access public
  224. */
  225. public function password()
  226. {
  227. $user = $this->getUser();
  228. $values = array('id' => $user['id']);
  229. $errors = array();
  230. if ($this->request->isPost()) {
  231. $values = $this->request->getValues();
  232. list($valid, $errors) = $this->user->validatePasswordModification($values);
  233. if ($valid) {
  234. if ($this->user->update($values)) {
  235. $this->session->flash(t('Password modified successfully.'));
  236. }
  237. else {
  238. $this->session->flashError(t('Unable to change the password.'));
  239. }
  240. $this->response->redirect($this->helper->url->to('user', 'show', array('user_id' => $user['id'])));
  241. }
  242. }
  243. $this->response->html($this->layout('user/password', array(
  244. 'values' => $values,
  245. 'errors' => $errors,
  246. 'user' => $user,
  247. )));
  248. }
  249. /**
  250. * Display a form to edit a user
  251. *
  252. * @access public
  253. */
  254. public function edit()
  255. {
  256. $user = $this->getUser();
  257. $values = $user;
  258. $errors = array();
  259. unset($values['password']);
  260. if ($this->request->isPost()) {
  261. $values = $this->request->getValues();
  262. if ($this->userSession->isAdmin()) {
  263. $values += array('is_admin' => 0, 'is_project_admin' => 0);
  264. }
  265. else {
  266. // Regular users can't be admin
  267. if (isset($values['is_admin'])) {
  268. unset($values['is_admin']);
  269. }
  270. if (isset($values['is_project_admin'])) {
  271. unset($values['is_project_admin']);
  272. }
  273. }
  274. list($valid, $errors) = $this->user->validateModification($values);
  275. if ($valid) {
  276. if ($this->user->update($values)) {
  277. $this->session->flash(t('User updated successfully.'));
  278. }
  279. else {
  280. $this->session->flashError(t('Unable to update your user.'));
  281. }
  282. $this->response->redirect($this->helper->url->to('user', 'show', array('user_id' => $user['id'])));
  283. }
  284. }
  285. $this->response->html($this->layout('user/edit', array(
  286. 'values' => $values,
  287. 'errors' => $errors,
  288. 'user' => $user,
  289. 'timezones' => $this->config->getTimezones(true),
  290. 'languages' => $this->config->getLanguages(true),
  291. )));
  292. }
  293. /**
  294. * Display a form to edit authentication
  295. *
  296. * @access public
  297. */
  298. public function authentication()
  299. {
  300. $user = $this->getUser();
  301. $values = $user;
  302. $errors = array();
  303. unset($values['password']);
  304. if ($this->request->isPost()) {
  305. $values = $this->request->getValues() + array('disable_login_form' => 0, 'is_ldap_user' => 0);
  306. list($valid, $errors) = $this->user->validateModification($values);
  307. if ($valid) {
  308. if ($this->user->update($values)) {
  309. $this->session->flash(t('User updated successfully.'));
  310. }
  311. else {
  312. $this->session->flashError(t('Unable to update your user.'));
  313. }
  314. $this->response->redirect($this->helper->url->to('user', 'authentication', array('user_id' => $user['id'])));
  315. }
  316. }
  317. $this->response->html($this->layout('user/authentication', array(
  318. 'values' => $values,
  319. 'errors' => $errors,
  320. 'user' => $user,
  321. )));
  322. }
  323. /**
  324. * Remove a user
  325. *
  326. * @access public
  327. */
  328. public function remove()
  329. {
  330. $user = $this->getUser();
  331. if ($this->request->getStringParam('confirmation') === 'yes') {
  332. $this->checkCSRFParam();
  333. if ($this->user->remove($user['id'])) {
  334. $this->session->flash(t('User removed successfully.'));
  335. } else {
  336. $this->session->flashError(t('Unable to remove this user.'));
  337. }
  338. $this->response->redirect($this->helper->url->to('user', 'index'));
  339. }
  340. $this->response->html($this->layout('user/remove', array(
  341. 'user' => $user,
  342. )));
  343. }
  344. }