Moved user flagging to API

This commit is contained in:
Marcin Kurczewski 2014-05-04 10:09:21 +02:00
parent d2319465c1
commit c86854dcb1
5 changed files with 63 additions and 27 deletions

View file

@ -132,18 +132,20 @@ $tagValidation =
\Chibi\Router::register(['TagController', 'mergeView'], 'GET', '/tags-merge', $tagValidation);
\Chibi\Router::register(['TagController', 'mergeAction'], 'POST', '/tags-merge', $tagValidation);
$userValidations =
[
'name' => '[^\/]+',
'page' => '\d*',
'tab' => 'favs|uploads',
'filter' => '[^\/]+',
];
\Chibi\Router::register(['UserController', 'flagAction'], 'POST', '/user/{name}/flag', $userValidations);
foreach (['GET', 'POST'] as $method)
{
\Chibi\Router::register(['TagController', 'massTagRedirectAction'], $method, '/mass-tag-redirect', $tagValidation);
$userValidations =
[
'name' => '[^\/]+',
'page' => '\d*',
'tab' => 'favs|uploads',
'filter' => '[^\/]+',
];
\Chibi\Router::register(['UserController', 'registrationAction'], $method, '/register', $userValidations);
\Chibi\Router::register(['UserController', 'viewAction'], $method, '/user/{name}/{tab}', $userValidations);
\Chibi\Router::register(['UserController', 'viewAction'], $method, '/user/{name}/{tab}/{page}', $userValidations);
@ -151,7 +153,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', 'flagAction'], $method, '/user/{name}/flag', $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);

View file

@ -6,6 +6,7 @@ abstract class AbstractJob
const POST_NAME = 'post-name';
const TAG_NAME = 'tag-name';
const TAG_NAMES = 'tags';
const USER_NAME = 'user-name';
const TEXT = 'text';
const PAGE_NUMBER = 'page-number';
const QUERY = 'query';

View file

@ -0,0 +1,11 @@
<?php
abstract class AbstractUserJob extends AbstractJob
{
protected $user;
public function prepare()
{
$userName = $this->getArgument(self::USER_NAME);
$this->user = UserModel::findByNameOrEmail($userName);
}
}

View file

@ -0,0 +1,40 @@
<?php
class FlagUserJob extends AbstractUserJob
{
public function execute()
{
$user = $this->user;
$key = TextHelper::reprUser($user);
$flagged = SessionHelper::get('flagged', []);
if (in_array($key, $flagged))
throw new SimpleException('You already flagged this user');
$flagged []= $key;
SessionHelper::set('flagged', $flagged);
LogHelper::log('{user} flagged {subject} for moderator attention', [
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'subject' => TextHelper::reprUser($user)]);
return $user;
}
public function requiresPrivilege()
{
return
[
Privilege::FlagUser,
Access::getIdentity($this->user)
];
}
public function requiresAuthentication()
{
return false;
}
public function requiresConfirmedEmail()
{
return false;
}
}

View file

@ -28,24 +28,7 @@ class UserController
public function flagAction($name)
{
$user = UserModel::findByNameOrEmail($name);
Access::assert(
Privilege::FlagUser,
Access::getIdentity($user));
if (!InputHelper::get('submit'))
return;
$key = TextHelper::reprUser($user);
$flagged = SessionHelper::get('flagged', []);
if (in_array($key, $flagged))
throw new SimpleException('You already flagged this user');
$flagged []= $key;
SessionHelper::set('flagged', $flagged);
LogHelper::log('{user} flagged {subject} for moderator attention', [
'subject' => TextHelper::reprUser($user)]);
Api::run(new FlagUserJob(), [FlagUserJob::USER_NAME => $name]);
}
public function banAction($name)