Moved user (un)banning to API

This commit is contained in:
Marcin Kurczewski 2014-05-04 10:16:05 +02:00
parent c86854dcb1
commit 588efcb908
3 changed files with 46 additions and 26 deletions

View file

@ -141,6 +141,8 @@ $userValidations =
]; ];
\Chibi\Router::register(['UserController', 'flagAction'], 'POST', '/user/{name}/flag', $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) 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/{page}', $userValidations);
\Chibi\Router::register(['UserController', 'listAction'], $method, '/users/{filter}', $userValidations); \Chibi\Router::register(['UserController', 'listAction'], $method, '/users/{filter}', $userValidations);
\Chibi\Router::register(['UserController', 'listAction'], $method, '/users/{filter}/{page}', $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', 'acceptRegistrationAction'], $method, '/user/{name}/accept-registration', $userValidations);
\Chibi\Router::register(['UserController', 'deleteAction'], $method, '/user/{name}/delete', $userValidations); \Chibi\Router::register(['UserController', 'deleteAction'], $method, '/user/{name}/delete', $userValidations);
\Chibi\Router::register(['UserController', 'settingsAction'], $method, '/user/{name}/settings', $userValidations); \Chibi\Router::register(['UserController', 'settingsAction'], $method, '/user/{name}/settings', $userValidations);

View file

@ -0,0 +1,38 @@
<?php
class ToggleUserBanJob extends AbstractUserJob
{
public function execute()
{
$user = $this->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;
}
}

View file

@ -33,34 +33,16 @@ class UserController
public function banAction($name) public function banAction($name)
{ {
$user = UserModel::findByNameOrEmail($name); Api::run(new ToggleUserBanJob(), [
Access::assert( ToggleUserBanJob::USER_NAME => $name,
Privilege::BanUser, ToggleUserBanJob::STATE => true]);
Access::getIdentity($user));
if (!InputHelper::get('submit'))
return;
$user->banned = true;
UserModel::save($user);
LogHelper::log('{user} banned {subject}', ['subject' => TextHelper::reprUser($user)]);
} }
public function unbanAction($name) public function unbanAction($name)
{ {
$user = UserModel::findByNameOrEmail($name); Api::run(new ToggleUserBanJob(), [
Access::assert( ToggleUserBanJob::USER_NAME => $name,
Privilege::BanUser, ToggleUserBanJob::STATE => false]);
Access::getIdentity($user));
if (!InputHelper::get('submit'))
return;
$user->banned = false;
UserModel::save($user);
LogHelper::log('{user} unbanned {subject}', ['subject' => TextHelper::reprUser($user)]);
} }
public function acceptRegistrationAction($name) public function acceptRegistrationAction($name)