From 4514bc1d4b4abff23902e46da76e70f13a3647eb Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Mon, 16 May 2016 20:55:21 -0400 Subject: Improve user controllers and views --- app/Controller/Oauth.php | 6 +- app/Controller/Twofactor.php | 4 +- app/Controller/User.php | 350 ------------------------------ app/Controller/UserAjaxController.php | 52 +++++ app/Controller/UserCreationController.php | 4 +- app/Controller/UserHelper.php | 52 ----- app/Controller/UserImportController.php | 2 +- app/Controller/UserListController.php | 32 +++ app/Controller/UserStatusController.php | 6 +- app/Controller/UserViewController.php | 330 ++++++++++++++++++++++++++++ 10 files changed, 425 insertions(+), 413 deletions(-) delete mode 100644 app/Controller/User.php create mode 100644 app/Controller/UserAjaxController.php delete mode 100644 app/Controller/UserHelper.php create mode 100644 app/Controller/UserListController.php create mode 100644 app/Controller/UserViewController.php (limited to 'app/Controller') diff --git a/app/Controller/Oauth.php b/app/Controller/Oauth.php index c38654be..04adf154 100644 --- a/app/Controller/Oauth.php +++ b/app/Controller/Oauth.php @@ -49,7 +49,7 @@ class Oauth extends BaseController $this->link($provider); } else { $this->flash->failure(t('The OAuth2 state parameter is invalid')); - $this->response->redirect($this->helper->url->to('user', 'external', array('user_id' => $this->userSession->getId()))); + $this->response->redirect($this->helper->url->to('UserViewController', 'external', array('user_id' => $this->userSession->getId()))); } } else { if ($hasValidState) { @@ -75,7 +75,7 @@ class Oauth extends BaseController $this->flash->success(t('Your external account is linked to your profile successfully.')); } - $this->response->redirect($this->helper->url->to('user', 'external', array('user_id' => $this->userSession->getId()))); + $this->response->redirect($this->helper->url->to('UserViewController', 'external', array('user_id' => $this->userSession->getId()))); } /** @@ -94,7 +94,7 @@ class Oauth extends BaseController $this->flash->failure(t('Unable to unlink your external account.')); } - $this->response->redirect($this->helper->url->to('user', 'external', array('user_id' => $this->userSession->getId()))); + $this->response->redirect($this->helper->url->to('UserViewController', 'external', array('user_id' => $this->userSession->getId()))); } /** diff --git a/app/Controller/Twofactor.php b/app/Controller/Twofactor.php index 2eb61594..118613b2 100644 --- a/app/Controller/Twofactor.php +++ b/app/Controller/Twofactor.php @@ -10,7 +10,7 @@ use Kanboard\Core\Controller\AccessForbiddenException; * @package controller * @author Frederic Guillot */ -class Twofactor extends User +class Twofactor extends UserViewController { /** * Only the current user can access to 2FA settings @@ -192,7 +192,7 @@ class Twofactor extends User 'twofactor_secret' => '', )); - return $this->response->redirect($this->helper->url->to('user', 'show', array('user_id' => $user['id']))); + return $this->response->redirect($this->helper->url->to('UserViewController', 'show', array('user_id' => $user['id']))); } return $this->response->html($this->helper->layout->user('twofactor/disable', array( diff --git a/app/Controller/User.php b/app/Controller/User.php deleted file mode 100644 index 190acb61..00000000 --- a/app/Controller/User.php +++ /dev/null @@ -1,350 +0,0 @@ -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 - * @throws PageNotFoundException - */ - public function profile() - { - $user = $this->user->getById($this->request->getIntegerParam('user_id')); - - if (empty($user)) { - throw new PageNotFoundException(); - } - - $this->response->html($this->helper->layout->app('user/profile', array( - 'title' => $user['name'] ?: $user['username'], - 'user' => $user, - ))); - } - - /** - * 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->timezone->getTimezones(true), - 'languages' => $this->language->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.')); - return $this->response->redirect($this->helper->url->to('user', 'notifications', array('user_id' => $user['id']))); - } - - return $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.')); - } - - return $this->response->redirect($this->helper->url->to('user', 'share', array('user_id' => $user['id']))); - } - - return $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.')); - } - - return $this->response->redirect($this->helper->url->to('user', 'show', array('user_id' => $user['id']))); - } - } - - return $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.')); - } - - return $this->response->redirect($this->helper->url->to('user', 'show', array('user_id' => $user['id']))); - } - } - - return $this->response->html($this->helper->layout->user('user/edit', array( - 'values' => $values, - 'errors' => $errors, - 'user' => $user, - 'timezones' => $this->timezone->getTimezones(true), - 'languages' => $this->language->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.')); - } - - return $this->response->redirect($this->helper->url->to('user', 'authentication', array('user_id' => $user['id']))); - } - } - - return $this->response->html($this->helper->layout->user('user/authentication', array( - 'values' => $values, - 'errors' => $errors, - 'user' => $user, - ))); - } -} diff --git a/app/Controller/UserAjaxController.php b/app/Controller/UserAjaxController.php new file mode 100644 index 00000000..7b365667 --- /dev/null +++ b/app/Controller/UserAjaxController.php @@ -0,0 +1,52 @@ +request->getStringParam('term'); + $filter = $this->userQuery->withFilter(new UserNameFilter($search)); + $filter->getQuery()->asc(UserModel::TABLE.'.name')->asc(UserModel::TABLE.'.username'); + $this->response->json($filter->format(new UserAutoCompleteFormatter($this->container))); + } + + /** + * User mention auto-completion (Ajax) + * + * @access public + */ + public function mention() + { + $project_id = $this->request->getStringParam('project_id'); + $query = $this->request->getStringParam('q'); + $users = $this->projectPermission->findUsernames($project_id, $query); + $this->response->json($users); + } + + /** + * Check if the user is connected + * + * @access public + */ + public function status() + { + $this->response->text('OK'); + } +} diff --git a/app/Controller/UserCreationController.php b/app/Controller/UserCreationController.php index d1c554ae..49f9db54 100644 --- a/app/Controller/UserCreationController.php +++ b/app/Controller/UserCreationController.php @@ -74,10 +74,10 @@ class UserCreationController extends BaseController } $this->flash->success(t('User created successfully.')); - $this->response->redirect($this->helper->url->to('user', 'show', array('user_id' => $user_id))); + $this->response->redirect($this->helper->url->to('UserViewController', 'show', array('user_id' => $user_id))); } else { $this->flash->failure(t('Unable to create your user.')); - $this->response->redirect($this->helper->url->to('user', 'index')); + $this->response->redirect($this->helper->url->to('UserListController', 'show')); } } } diff --git a/app/Controller/UserHelper.php b/app/Controller/UserHelper.php deleted file mode 100644 index d5e0920d..00000000 --- a/app/Controller/UserHelper.php +++ /dev/null @@ -1,52 +0,0 @@ -request->getStringParam('term'); - $filter = $this->userQuery->withFilter(new UserNameFilter($search)); - $filter->getQuery()->asc(UserModel::TABLE.'.name')->asc(UserModel::TABLE.'.username'); - $this->response->json($filter->format(new UserAutoCompleteFormatter($this->container))); - } - - /** - * User mention auto-completion (Ajax) - * - * @access public - */ - public function mention() - { - $project_id = $this->request->getStringParam('project_id'); - $query = $this->request->getStringParam('q'); - $users = $this->projectPermission->findUsernames($project_id, $query); - $this->response->json($users); - } - - /** - * Check if the user is connected - * - * @access public - */ - public function status() - { - $this->response->text('OK'); - } -} diff --git a/app/Controller/UserImportController.php b/app/Controller/UserImportController.php index b3cb52d1..fec9a31d 100644 --- a/app/Controller/UserImportController.php +++ b/app/Controller/UserImportController.php @@ -43,7 +43,7 @@ class UserImportController extends BaseController $this->importFile($values, $filename); } - $this->response->redirect($this->helper->url->to('user', 'index')); + $this->response->redirect($this->helper->url->to('UserListController', 'show')); } /** diff --git a/app/Controller/UserListController.php b/app/Controller/UserListController.php new file mode 100644 index 00000000..685746fb --- /dev/null +++ b/app/Controller/UserListController.php @@ -0,0 +1,32 @@ +paginator + ->setUrl('UserListController', 'show') + ->setMax(30) + ->setOrder('username') + ->setQuery($this->user->getQuery()) + ->calculate(); + + $this->response->html($this->helper->layout->app('user_list/show', array( + 'title' => t('Users').' ('.$paginator->getTotal().')', + 'paginator' => $paginator, + ))); + } +} diff --git a/app/Controller/UserStatusController.php b/app/Controller/UserStatusController.php index 2b23505c..bc40a7dd 100644 --- a/app/Controller/UserStatusController.php +++ b/app/Controller/UserStatusController.php @@ -40,7 +40,7 @@ class UserStatusController extends BaseController $this->flash->failure(t('Unable to remove this user.')); } - $this->response->redirect($this->helper->url->to('user', 'index')); + $this->response->redirect($this->helper->url->to('UserListController', 'show')); } /** @@ -73,7 +73,7 @@ class UserStatusController extends BaseController $this->flash->failure(t('Unable to enable this user.')); } - $this->response->redirect($this->helper->url->to('user', 'index')); + $this->response->redirect($this->helper->url->to('UserListController', 'show')); } /** @@ -106,6 +106,6 @@ class UserStatusController extends BaseController $this->flash->failure(t('Unable to disable this user.')); } - $this->response->redirect($this->helper->url->to('user', 'index')); + $this->response->redirect($this->helper->url->to('UserListController', 'show')); } } diff --git a/app/Controller/UserViewController.php b/app/Controller/UserViewController.php new file mode 100644 index 00000000..dc03f419 --- /dev/null +++ b/app/Controller/UserViewController.php @@ -0,0 +1,330 @@ +user->getById($this->request->getIntegerParam('user_id')); + + if (empty($user)) { + throw new PageNotFoundException(); + } + + $this->response->html($this->helper->layout->app('user_view/profile', array( + 'title' => $user['name'] ?: $user['username'], + 'user' => $user, + ))); + } + + /** + * Display user information + * + * @access public + */ + public function show() + { + $user = $this->getUser(); + $this->response->html($this->helper->layout->user('user_view/show', array( + 'user' => $user, + 'timezones' => $this->timezone->getTimezones(true), + 'languages' => $this->language->getLanguages(true), + ))); + } + + /** + * Display timesheet + * + * @access public + */ + public function timesheet() + { + $user = $this->getUser(); + + $subtask_paginator = $this->paginator + ->setUrl('UserViewController', '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_view/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_view/password_reset', array( + 'tokens' => $this->passwordReset->getAll($user['id']), + 'user' => $user, + ))); + } + + /** + * Display last connections + * + * @access public + */ + public function lastLogin() + { + $user = $this->getUser(); + $this->response->html($this->helper->layout->user('user_view/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_view/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('UserViewController', '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.')); + return $this->response->redirect($this->helper->url->to('UserViewController', 'notifications', array('user_id' => $user['id']))); + } + + return $this->response->html($this->helper->layout->user('user_view/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('UserViewController', 'integrations', array('user_id' => $user['id']))); + } + + $this->response->html($this->helper->layout->user('user_view/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_view/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.')); + } + + return $this->response->redirect($this->helper->url->to('UserViewController', 'share', array('user_id' => $user['id']))); + } + + return $this->response->html($this->helper->layout->user('user_view/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.')); + } + + return $this->response->redirect($this->helper->url->to('UserViewController', 'show', array('user_id' => $user['id']))); + } + } + + return $this->response->html($this->helper->layout->user('user_view/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.')); + } + + return $this->response->redirect($this->helper->url->to('UserViewController', 'show', array('user_id' => $user['id']))); + } + } + + return $this->response->html($this->helper->layout->user('user_view/edit', array( + 'values' => $values, + 'errors' => $errors, + 'user' => $user, + 'timezones' => $this->timezone->getTimezones(true), + 'languages' => $this->language->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.')); + } + + return $this->response->redirect($this->helper->url->to('UserViewController', 'authentication', array('user_id' => $user['id']))); + } + } + + return $this->response->html($this->helper->layout->user('user_view/authentication', array( + 'values' => $values, + 'errors' => $errors, + 'user' => $user, + ))); + } +} -- cgit v1.2.3