From 588efcb908fd037c32f0cad49f9dbab71d8e37f3 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 4 May 2014 10:16:05 +0200 Subject: [PATCH] Moved user (un)banning to API --- public_html/dispatch.php | 4 ++-- src/Api/Jobs/ToggleUserBanJob.php | 38 ++++++++++++++++++++++++++++++ src/Controllers/UserController.php | 30 +++++------------------ 3 files changed, 46 insertions(+), 26 deletions(-) create mode 100644 src/Api/Jobs/ToggleUserBanJob.php diff --git a/public_html/dispatch.php b/public_html/dispatch.php index 87f7c0d5..a729ac10 100644 --- a/public_html/dispatch.php +++ b/public_html/dispatch.php @@ -141,6 +141,8 @@ $userValidations = ]; \Chibi\Router::register(['UserController', 'flagAction'], 'POST', '/user/{name}/flag', $userValidations); +\Chibi\Router::register(['UserController', 'banAction'], 'POST', '/user/{name}/ban', $userValidations); +\Chibi\Router::register(['UserController', 'unbanAction'], 'POST', '/user/{name}/unban', $userValidations); foreach (['GET', 'POST'] as $method) { @@ -153,8 +155,6 @@ foreach (['GET', 'POST'] as $method) \Chibi\Router::register(['UserController', 'listAction'], $method, '/users/{page}', $userValidations); \Chibi\Router::register(['UserController', 'listAction'], $method, '/users/{filter}', $userValidations); \Chibi\Router::register(['UserController', 'listAction'], $method, '/users/{filter}/{page}', $userValidations); - \Chibi\Router::register(['UserController', 'banAction'], $method, '/user/{name}/ban', $userValidations); - \Chibi\Router::register(['UserController', 'unbanAction'], $method, '/user/{name}/unban', $userValidations); \Chibi\Router::register(['UserController', 'acceptRegistrationAction'], $method, '/user/{name}/accept-registration', $userValidations); \Chibi\Router::register(['UserController', 'deleteAction'], $method, '/user/{name}/delete', $userValidations); \Chibi\Router::register(['UserController', 'settingsAction'], $method, '/user/{name}/settings', $userValidations); diff --git a/src/Api/Jobs/ToggleUserBanJob.php b/src/Api/Jobs/ToggleUserBanJob.php new file mode 100644 index 00000000..0d97a3f4 --- /dev/null +++ b/src/Api/Jobs/ToggleUserBanJob.php @@ -0,0 +1,38 @@ +user; + $banned = boolval($this->getArgument(self::STATE)); + + $user->banned = $banned; + UserModel::save($user); + + LogHelper::log( + $banned + ? '{user} banned {subject}' + : '{user} unbanned {subject}', [ + 'user' => TextHelper::reprUser(Auth::getCurrentUser()), + 'subject' => TextHelper::reprUser($user)]); + } + + public function requiresPrivilege() + { + return + [ + Privilege::BanUser, + Access::getIdentity($this->user) + ]; + } + + public function requiresAuthentication() + { + return false; + } + + public function requiresConfirmedEmail() + { + return false; + } +} diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 3a641e32..cf8b796e 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -33,34 +33,16 @@ class UserController public function banAction($name) { - $user = UserModel::findByNameOrEmail($name); - Access::assert( - Privilege::BanUser, - Access::getIdentity($user)); - - if (!InputHelper::get('submit')) - return; - - $user->banned = true; - UserModel::save($user); - - LogHelper::log('{user} banned {subject}', ['subject' => TextHelper::reprUser($user)]); + Api::run(new ToggleUserBanJob(), [ + ToggleUserBanJob::USER_NAME => $name, + ToggleUserBanJob::STATE => true]); } public function unbanAction($name) { - $user = UserModel::findByNameOrEmail($name); - Access::assert( - Privilege::BanUser, - Access::getIdentity($user)); - - if (!InputHelper::get('submit')) - return; - - $user->banned = false; - UserModel::save($user); - - LogHelper::log('{user} unbanned {subject}', ['subject' => TextHelper::reprUser($user)]); + Api::run(new ToggleUserBanJob(), [ + ToggleUserBanJob::USER_NAME => $name, + ToggleUserBanJob::STATE => false]); } public function acceptRegistrationAction($name)