This repository has been archived on 2025-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
szurubooru/src/Controllers/UserController.php

432 lines
11 KiB
PHP
Raw Normal View History

2013-10-05 19:24:08 +02:00
<?php
2014-05-16 21:38:33 +02:00
class UserController extends AbstractController
2013-10-05 19:24:08 +02:00
{
2014-05-04 10:32:32 +02:00
public function listView($filter = 'order:alpha,asc', $page = 1)
2013-10-05 19:24:08 +02:00
{
2014-05-04 10:32:32 +02:00
$ret = Api::run(
new ListUsersJob(),
[
JobArgs::ARG_PAGE_NUMBER => $page,
JobArgs::ARG_QUERY => $filter,
2014-05-04 10:32:32 +02:00
]);
2013-10-16 13:07:01 +02:00
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
2014-05-04 10:32:32 +02:00
$context->filter = $filter;
$context->transport->users = $ret->entities;
$context->transport->paginator = $ret;
2014-05-16 21:38:33 +02:00
$this->renderView('user-list');
2013-10-05 19:24:08 +02:00
}
2014-05-12 10:31:34 +02:00
public function genericView($identifier, $tab = 'favs', $page = 1)
{
2014-05-16 21:38:33 +02:00
$this->prepareGenericView($identifier, $tab, $page);
$this->renderView('user-view');
}
2014-05-16 21:38:33 +02:00
public function settingsAction($identifier)
{
$this->prepareGenericView($identifier, 'settings');
2014-05-04 16:27:15 +02:00
2014-05-16 21:38:33 +02:00
try
2014-05-06 19:03:13 +02:00
{
2014-05-16 21:38:33 +02:00
$suppliedSafety = InputHelper::get('safety');
$desiredSafety = PostSafety::makeFlags($suppliedSafety);
2014-05-16 21:38:33 +02:00
$user = Api::run(
new EditUserSettingsJob(),
$this->appendUserIdentifierArgument(
2014-05-04 12:01:14 +02:00
[
2014-05-16 21:38:33 +02:00
JobArgs::ARG_NEW_SETTINGS =>
[
UserSettings::SETTING_SAFETY => $desiredSafety,
UserSettings::SETTING_ENDLESS_SCROLLING => InputHelper::get('endless-scrolling'),
UserSettings::SETTING_POST_TAG_TITLES => InputHelper::get('post-tag-titles'),
UserSettings::SETTING_HIDE_DISLIKED_POSTS => InputHelper::get('hide-disliked-posts'),
]
], $identifier));
Core::getContext()->transport->user = $user;
if ($user->getId() == Auth::getCurrentUser()->getId())
Auth::setCurrentUser($user);
2014-05-04 12:01:14 +02:00
2014-05-16 21:38:33 +02:00
Messenger::success('Browsing settings updated!');
2014-04-30 08:08:24 +02:00
}
2014-05-16 21:38:33 +02:00
catch (SimpleException $e)
{
\Chibi\Util\Headers::setCode(400);
2014-05-16 21:38:33 +02:00
Messenger::fail($e->getMessage());
}
2014-05-20 20:54:31 +02:00
if ($this->isAjax())
$this->renderAjax();
else
$this->renderView('user-view');
}
2014-05-16 21:38:33 +02:00
public function editAction($identifier)
2013-10-22 00:17:06 +02:00
{
2014-05-16 21:38:33 +02:00
$this->prepareGenericView($identifier, 'edit');
2014-05-04 12:01:14 +02:00
2014-05-16 21:38:33 +02:00
try
{
$this->requirePasswordConfirmation();
2014-04-30 08:08:24 +02:00
2014-05-16 21:38:33 +02:00
if (InputHelper::get('password1') != InputHelper::get('password2'))
throw new SimpleException('Specified passwords must be the same');
$args =
[
2014-05-16 21:38:33 +02:00
JobArgs::ARG_NEW_USER_NAME => InputHelper::get('name'),
JobArgs::ARG_NEW_PASSWORD => InputHelper::get('password1'),
JobArgs::ARG_NEW_EMAIL => InputHelper::get('email'),
JobArgs::ARG_NEW_ACCESS_RANK => InputHelper::get('access-rank'),
2014-05-20 19:46:05 +02:00
Jobargs::ARG_NEW_AVATAR_STYLE => InputHelper::get('avatar-style'),
2014-05-16 21:38:33 +02:00
];
2014-05-20 19:46:05 +02:00
if (!empty($_FILES['avatar-content']['name']))
{
$file = $_FILES['avatar-content'];
TransferHelper::handleUploadErrors($file);
$args[JobArgs::ARG_NEW_AVATAR_CONTENT] = new ApiFileInput(
$file['tmp_name'],
$file['name']);
}
2014-05-16 21:38:33 +02:00
$args = $this->appendUserIdentifierArgument($args, $identifier);
2014-04-30 08:08:24 +02:00
2014-05-16 21:38:33 +02:00
$args = array_filter($args);
$user = Api::run(new EditUserJob(), $args);
2014-05-04 12:01:14 +02:00
2014-05-20 19:46:05 +02:00
Core::getContext()->transport->user = $user;
2014-05-16 21:38:33 +02:00
if (Auth::getCurrentUser()->getId() == $user->getId())
Auth::setCurrentUser($user);
2013-10-22 00:17:06 +02:00
2014-05-16 21:38:33 +02:00
$message = 'Account settings updated!';
if (Mailer::getMailCounter() > 0)
$message .= ' You will be sent an e-mail address confirmation message soon.';
2014-05-04 12:01:14 +02:00
2014-05-16 21:38:33 +02:00
Messenger::success($message);
}
catch (SimpleException $e)
{
\Chibi\Util\Headers::setCode(400);
2014-05-16 21:38:33 +02:00
Messenger::fail($e->getMessage());
}
2014-05-20 20:54:31 +02:00
if ($this->isAjax())
$this->renderAjax();
else
$this->renderView('user-view');
2014-05-16 21:38:33 +02:00
}
2014-04-30 08:08:24 +02:00
2014-05-16 21:38:33 +02:00
public function deleteAction($identifier)
{
$this->prepareGenericView($identifier, 'delete');
2013-11-16 16:24:38 +01:00
2014-05-16 21:38:33 +02:00
try
{
$this->requirePasswordConfirmation();
2014-04-30 08:08:24 +02:00
2014-05-16 21:38:33 +02:00
Api::run(
new DeleteUserJob(),
$this->appendUserIdentifierArgument([], $identifier));
$user = UserModel::tryGetById(Auth::getCurrentUser()->getId());
if (!$user)
Auth::logOut();
2014-04-30 08:08:24 +02:00
2014-05-16 21:38:33 +02:00
$this->redirectToMainPage();
}
catch (SimpleException $e)
{
\Chibi\Util\Headers::setCode(400);
2014-05-16 21:38:33 +02:00
Messenger::fail($e->getMessage());
2014-05-20 20:54:31 +02:00
if ($this->isAjax())
$this->renderAjax();
else
$this->renderView('user-view');
2014-05-16 21:38:33 +02:00
}
}
2014-05-16 19:31:04 +02:00
public function toggleSafetyAction($safety)
{
$safety = new PostSafety($safety);
$safety->validate();
$user = Auth::getCurrentUser();
$user->getSettings()->enableSafety($safety, !$user->getSettings()->hasEnabledSafety($safety));
$desiredSafety = $user->getSettings()->get(UserSettings::SETTING_SAFETY);
$user = Api::run(
new EditUserSettingsJob(),
[
JobArgs::ARG_USER_ENTITY => Auth::getCurrentUser(),
JobArgs::ARG_NEW_SETTINGS => [UserSettings::SETTING_SAFETY => $desiredSafety],
]);
Auth::setCurrentUser($user);
$this->redirectToLastVisitedUrl();
}
2014-05-12 10:31:34 +02:00
public function flagAction($identifier)
2014-05-04 12:01:14 +02:00
{
2014-05-12 10:31:34 +02:00
Api::run(
new FlagUserJob(),
$this->appendUserIdentifierArgument([], $identifier));
2014-05-16 19:31:04 +02:00
$this->redirectToGenericView($identifier);
2014-05-04 12:01:14 +02:00
}
2013-10-14 10:22:53 +02:00
2014-05-12 10:31:34 +02:00
public function banAction($identifier)
2014-05-04 12:01:14 +02:00
{
2014-05-12 10:31:34 +02:00
Api::run(
new ToggleUserBanJob(),
$this->appendUserIdentifierArgument([
JobArgs::ARG_NEW_STATE => true
], $identifier));
2014-05-16 19:31:04 +02:00
$this->redirectToGenericView($identifier);
2014-05-04 12:01:14 +02:00
}
2014-05-12 10:31:34 +02:00
public function unbanAction($identifier)
2014-05-04 12:01:14 +02:00
{
2014-05-12 10:31:34 +02:00
Api::run(
new ToggleUserBanJob(),
$this->appendUserIdentifierArgument([
JobArgs::ARG_NEW_STATE => true
], $identifier));
2014-05-16 19:31:04 +02:00
$this->redirectToGenericView($identifier);
2014-05-04 12:01:14 +02:00
}
2014-05-12 10:31:34 +02:00
public function acceptRegistrationAction($identifier)
2014-05-04 12:01:14 +02:00
{
2014-05-12 10:31:34 +02:00
Api::run(
new AcceptUserRegistrationJob(),
$this->appendUserIdentifierArgument([], $identifier));
2014-05-16 19:31:04 +02:00
$this->redirectToGenericView($identifier);
2013-10-05 19:24:08 +02:00
}
2013-10-14 00:25:40 +02:00
2014-05-04 14:57:44 +02:00
public function registrationView()
2013-10-16 18:07:23 +02:00
{
2014-05-01 16:12:37 +02:00
if (Auth::isLoggedIn())
2014-05-16 21:38:33 +02:00
$this->redirectToMainPage();
$this->renderView('user-registration');
2014-05-04 14:57:44 +02:00
}
2013-10-16 18:07:23 +02:00
2014-05-04 14:57:44 +02:00
public function registrationAction()
{
2014-05-16 21:38:33 +02:00
try
{
if (InputHelper::get('password1') != InputHelper::get('password2'))
throw new SimpleException('Specified passwords must be the same');
2013-10-16 18:07:23 +02:00
2014-05-16 21:38:33 +02:00
$user = Api::run(new AddUserJob(),
[
JobArgs::ARG_NEW_USER_NAME => InputHelper::get('name'),
JobArgs::ARG_NEW_PASSWORD => InputHelper::get('password1'),
JobArgs::ARG_NEW_EMAIL => InputHelper::get('email'),
]);
2013-10-16 18:07:23 +02:00
2014-05-16 21:38:33 +02:00
if (!$this->isAnyAccountActivationNeeded())
{
Auth::setCurrentUser($user);
}
$message = 'Congratulations, your account was created.';
if (Mailer::getMailCounter() > 0)
{
$message .= ' Please wait for activation e-mail.';
if (Core::getConfig()->registration->staffActivation)
$message .= ' After this, your registration must be confirmed by staff.';
}
elseif (Core::getConfig()->registration->staffActivation)
$message .= ' Your registration must be now confirmed by staff.';
Messenger::success($message);
2014-04-30 08:08:24 +02:00
}
2014-05-16 21:38:33 +02:00
catch (SimpleException $e)
2014-04-30 08:08:24 +02:00
{
\Chibi\Util\Headers::setCode(400);
2014-05-16 21:38:33 +02:00
Messenger::fail($e->getMessage());
2014-04-30 08:08:24 +02:00
}
2013-10-16 18:07:23 +02:00
2014-05-16 21:38:33 +02:00
$this->renderView('user-registration');
2013-10-16 18:07:23 +02:00
}
public function activationView()
2013-10-16 18:07:23 +02:00
{
2014-05-16 21:38:33 +02:00
$this->assets->setSubTitle('account activation');
$this->renderView('user-select');
2013-10-16 18:07:23 +02:00
}
public function activationAction($tokenText)
{
2014-05-16 21:38:33 +02:00
$this->assets->setSubTitle('account activation');
2014-05-12 10:31:34 +02:00
$identifier = InputHelper::get('identifier');
2013-11-16 19:24:33 +01:00
2014-05-16 21:38:33 +02:00
try
{
if (empty($tokenText))
{
Api::run(
new ActivateUserEmailJob(),
$this->appendUserIdentifierArgument([], $identifier));
Messenger::success('Activation e-mail resent.');
}
else
{
$user = Api::run(new ActivateUserEmailJob(), [
JobArgs::ARG_TOKEN => $tokenText ]);
$message = 'Activation completed successfully.';
if (Core::getConfig()->registration->staffActivation)
$message .= ' However, your account still must be confirmed by staff.';
Messenger::success($message);
if (!Core::getConfig()->registration->staffActivation)
Auth::setCurrentUser($user);
}
2014-05-16 21:38:33 +02:00
}
catch (SimpleException $e)
{
\Chibi\Util\Headers::setCode(400);
2014-05-16 21:38:33 +02:00
Messenger::fail($e->getMessage());
}
$this->renderView('message');
2013-11-16 19:24:33 +01:00
}
public function passwordResetView()
2013-11-16 19:24:33 +01:00
{
2014-05-16 21:38:33 +02:00
$this->assets->setSubTitle('password reset');
$this->renderView('user-select');
2013-11-16 19:24:33 +01:00
}
public function passwordResetAction($tokenText)
2013-11-16 19:24:33 +01:00
{
2014-05-16 21:38:33 +02:00
$this->assets->setSubTitle('password reset');
2014-05-12 10:31:34 +02:00
$identifier = InputHelper::get('identifier');
2013-11-16 19:24:33 +01:00
2014-05-16 21:38:33 +02:00
try
{
2014-05-16 21:38:33 +02:00
if (empty($tokenText))
{
Api::run(
new PasswordResetJob(),
$this->appendUserIdentifierArgument([], $identifier));
Messenger::success('E-mail sent. Follow instructions to reset password.');
}
else
{
$ret = Api::run(new PasswordResetJob(), [ JobArgs::ARG_TOKEN => $tokenText ]);
Messenger::success(sprintf(
'Password reset successful. Your new password is **%s**.',
$ret->newPassword));
Auth::setCurrentUser($ret->user);
}
}
catch (SimpleException $e)
{
\Chibi\Util\Headers::setCode(400);
2014-05-16 21:38:33 +02:00
Messenger::fail($e->getMessage());
}
$this->renderView('message');
}
private function prepareGenericView($identifier, $tab, $page = 1)
{
$user = Api::run(
new GetUserJob(),
$this->appendUserIdentifierArgument([], $identifier));
$flagged = in_array(TextHelper::reprUser($user), SessionHelper::get('flagged', []));
2014-04-30 08:08:24 +02:00
2014-05-16 21:38:33 +02:00
if ($tab == 'uploads')
$query = 'submit:' . $user->getName();
elseif ($tab == 'favs')
$query = 'fav:' . $user->getName();
elseif ($tab == 'delete')
{
Access::assert(new Privilege(
Privilege::DeleteUser,
Access::getIdentity($user)));
}
2014-05-16 21:38:33 +02:00
elseif ($tab == 'settings')
{
2014-05-16 21:38:33 +02:00
Access::assert(new Privilege(
Privilege::EditUserSettings,
2014-05-16 21:38:33 +02:00
Access::getIdentity($user)));
}
elseif ($tab == 'edit' and !(new EditUserJob)->canEditAnything(Auth::getCurrentUser()))
Access::fail();
2014-05-16 21:38:33 +02:00
$context = Core::getContext();
$context->flagged = $flagged;
$context->transport->tab = $tab;
$context->transport->user = $user;
2014-04-30 08:08:24 +02:00
2014-05-16 21:38:33 +02:00
if (isset($query))
{
$ret = Api::run(
new ListPostsJob(),
[
JobArgs::ARG_PAGE_NUMBER => $page,
JobArgs::ARG_QUERY => $query
]);
$context->transport->posts = $ret->entities;
$context->transport->paginator = $ret;
$context->transport->lastSearchQuery = $query;
}
}
2014-05-04 12:01:14 +02:00
2014-05-16 21:38:33 +02:00
private function isAnyAccountActivationNeeded()
{
$config = Core::getConfig();
return ($config->registration->needEmailForRegistering
or $config->registration->staffActivation);
}
2014-05-04 12:01:14 +02:00
private function requirePasswordConfirmation()
{
2014-05-15 10:32:53 +02:00
$user = Core::getContext()->transport->user;
if (Auth::getCurrentUser()->getId() == $user->getId())
2014-05-04 12:01:14 +02:00
{
$suppliedPassword = InputHelper::get('current-password');
2014-05-07 00:34:02 +02:00
$suppliedPasswordHash = UserModel::hashPassword($suppliedPassword, $user->getPasswordSalt());
if ($suppliedPasswordHash != $user->getPasswordHash())
2014-05-04 12:01:14 +02:00
throw new SimpleException('Must supply valid password');
}
}
2014-05-12 10:31:34 +02:00
private function appendUserIdentifierArgument(array $arguments, $userIdentifier)
{
if (strpos($userIdentifier, '@') !== false)
$arguments[JobArgs::ARG_USER_EMAIL] = $userIdentifier;
else
$arguments[JobArgs::ARG_USER_NAME] = $userIdentifier;
return $arguments;
}
2014-05-16 19:31:04 +02:00
2014-05-16 21:38:33 +02:00
private function redirectToMainPage()
2014-05-16 19:31:04 +02:00
{
2014-05-16 21:38:33 +02:00
$this->redirect(\Chibi\Router::linkTo(['StaticPagesController', 'mainPageView']));
2014-05-16 19:31:04 +02:00
exit;
}
private function redirectToGenericView($identifier)
{
2014-05-16 21:38:33 +02:00
$this->redirect(\Chibi\Router::linkTo(
2014-05-16 19:31:04 +02:00
['UserController', 'genericView'],
['identifier' => $identifier]));
}
2013-10-05 19:24:08 +02:00
}