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

353 lines
9.4 KiB
PHP
Raw Normal View History

2013-10-05 19:24:08 +02:00
<?php
2013-10-05 21:24:20 +02:00
class UserController
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();
2013-10-16 13:07:01 +02:00
2014-05-04 10:32:32 +02:00
$context->filter = $filter;
$context->transport->users = $ret->entities;
$context->transport->paginator = $ret;
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-04 12:45:26 +02:00
$user = Api::run(
new GetUserJob(),
2014-05-12 10:31:34 +02:00
$this->appendUserIdentifierArgument([], $identifier));
2014-05-04 12:45:26 +02:00
2014-05-04 12:01:14 +02:00
$flagged = in_array(TextHelper::reprUser($user), SessionHelper::get('flagged', []));
2014-05-04 16:27:15 +02:00
if ($tab == 'uploads')
$query = 'submit:' . $user->getName();
2014-05-04 16:27:15 +02:00
elseif ($tab == 'favs')
$query = 'fav:' . $user->getName();
2014-05-04 16:27:15 +02:00
elseif ($tab == 'delete')
2014-05-06 19:03:13 +02:00
{
Access::assert(new Privilege(
Privilege::DeleteUser,
Access::getIdentity($user)));
}
2014-05-04 16:27:15 +02:00
elseif ($tab == 'settings')
2014-05-06 19:03:13 +02:00
{
Access::assert(new Privilege(
Privilege::ChangeUserSettings,
Access::getIdentity($user)));
}
2014-05-04 16:27:15 +02:00
elseif ($tab == 'edit' and !(new EditUserJob)->canEditAnything(Auth::getCurrentUser()))
Access::fail();
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
2014-05-04 12:01:14 +02:00
$context->flagged = $flagged;
$context->transport->tab = $tab;
$context->transport->user = $user;
$context->handleExceptions = true;
$context->viewName = 'user-view';
2014-05-04 12:01:14 +02:00
if (isset($query))
{
2014-05-04 12:01:14 +02:00
$ret = Api::run(
new ListPostsJob(),
[
JobArgs::ARG_PAGE_NUMBER => $page,
JobArgs::ARG_QUERY => $query
2014-05-04 12:01:14 +02:00
]);
$context->transport->posts = $ret->entities;
$context->transport->paginator = $ret;
$context->transport->lastSearchQuery = $query;
2014-04-30 08:08:24 +02:00
}
}
2014-05-12 10:31:34 +02:00
public function settingsAction($identifier)
2013-10-22 00:17:06 +02:00
{
2014-05-12 10:31:34 +02:00
$this->genericView($identifier, 'settings');
2014-05-04 12:01:14 +02:00
2014-04-30 08:08:24 +02:00
$suppliedSafety = InputHelper::get('safety');
$desiredSafety = PostSafety::makeFlags($suppliedSafety);
2014-04-30 08:08:24 +02:00
$user = Api::run(
new EditUserSettingsJob(),
$this->appendUserIdentifierArgument(
[
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));
2014-04-30 08:08:24 +02:00
2014-05-15 10:32:53 +02:00
Core::getContext()->transport->user = $user;
if ($user->getId() == Auth::getCurrentUser()->getId())
2014-05-01 16:12:37 +02:00
Auth::setCurrentUser($user);
2014-05-04 12:01:14 +02:00
Messenger::message('Browsing settings updated!');
2013-10-22 00:17:06 +02:00
}
2014-05-12 10:31:34 +02:00
public function editAction($identifier)
{
2014-05-12 10:31:34 +02:00
$this->genericView($identifier, 'edit');
2014-05-04 12:01:14 +02:00
$this->requirePasswordConfirmation();
2014-05-04 13:39:00 +02:00
if (InputHelper::get('password1') != InputHelper::get('password2'))
throw new SimpleException('Specified passwords must be the same');
2014-05-04 13:39:00 +02:00
$args =
[
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-04 13:39:00 +02:00
];
2014-05-12 10:31:34 +02:00
$args = $this->appendUserIdentifierArgument($args, $identifier);
2014-04-30 08:08:24 +02:00
2014-05-04 13:39:00 +02:00
$args = array_filter($args);
$user = Api::run(new EditUserJob(), $args);
2013-11-16 16:24:38 +01:00
if (Auth::getCurrentUser()->getId() == $user->getId())
2014-05-04 12:01:14 +02:00
Auth::setCurrentUser($user);
2014-04-30 08:08:24 +02:00
2014-05-04 12:01:14 +02:00
$message = 'Account settings updated!';
2014-05-04 13:39:00 +02:00
if (Mailer::getMailCounter() > 0)
2014-05-04 12:01:14 +02:00
$message .= ' You will be sent an e-mail address confirmation message soon.';
2014-04-30 08:08:24 +02:00
2014-05-04 12:01:14 +02:00
Messenger::message($message);
}
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 deleteAction($identifier)
2013-10-05 19:24:08 +02:00
{
2014-05-12 10:31:34 +02:00
$this->genericView($identifier, 'delete');
2014-05-04 12:01:14 +02:00
$this->requirePasswordConfirmation();
2013-10-14 10:22:53 +02:00
2014-05-12 10:31:34 +02:00
Api::run(
new DeleteUserJob(),
$this->appendUserIdentifierArgument([], $identifier));
$user = UserModel::tryGetById(Auth::getCurrentUser()->getId());
2014-05-04 12:01:14 +02:00
if (!$user)
Auth::logOut();
2014-05-04 12:01:14 +02:00
\Chibi\Util\Url::forward(\Chibi\Router::linkTo(['StaticPagesController', 'mainPageView']));
exit;
}
2013-10-14 10:22:53 +02:00
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-15 10:32:53 +02:00
$context = Core::getContext();
2014-04-29 21:35:29 +02:00
$context->handleExceptions = true;
2013-10-16 18:07:23 +02:00
//check if already logged in
2014-05-01 16:12:37 +02:00
if (Auth::isLoggedIn())
2013-10-16 18:07:23 +02:00
{
\Chibi\Util\Url::forward(\Chibi\Router::linkTo(['StaticPagesController', 'mainPageView']));
2014-05-03 23:27:00 +02:00
exit;
2013-10-16 18:07:23 +02:00
}
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()
{
$this->registrationView();
2013-10-16 18:07:23 +02:00
2014-05-04 14:57:44 +02:00
if (InputHelper::get('password1') != InputHelper::get('password2'))
2014-04-30 08:08:24 +02:00
throw new SimpleException('Specified passwords must be the same');
2013-10-16 18:07:23 +02:00
2014-05-04 14:57:44 +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'),
2014-05-04 14:57:44 +02:00
]);
2013-10-16 18:07:23 +02:00
2014-05-15 10:32:53 +02:00
if (!Core::getConfig()->registration->needEmailForRegistering and !Core::getConfig()->registration->staffActivation)
2014-04-30 08:08:24 +02:00
{
2014-05-04 14:57:44 +02:00
Auth::setCurrentUser($user);
2014-04-30 08:08:24 +02:00
}
2013-11-16 16:24:38 +01:00
2014-04-30 08:08:24 +02:00
$message = 'Congratulations, your account was created.';
2014-05-04 13:39:00 +02:00
if (Mailer::getMailCounter() > 0)
2014-04-30 08:08:24 +02:00
{
$message .= ' Please wait for activation e-mail.';
2014-05-15 10:32:53 +02:00
if (Core::getConfig()->registration->staffActivation)
2014-04-30 08:08:24 +02:00
$message .= ' After this, your registration must be confirmed by staff.';
}
2014-05-15 10:32:53 +02:00
elseif (Core::getConfig()->registration->staffActivation)
2014-04-30 08:08:24 +02:00
$message .= ' Your registration must be now confirmed by staff.';
2013-10-16 18:07:23 +02:00
Messenger::message($message);
2013-10-16 18:07:23 +02:00
}
public function activationView()
2013-10-16 18:07:23 +02:00
{
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
$context->viewName = 'user-select';
2014-04-29 21:35:29 +02:00
Assets::setSubTitle('account activation');
2013-10-16 18:07:23 +02:00
}
public function activationAction($tokenText)
{
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
2014-04-29 21:35:29 +02:00
$context->viewName = 'message';
Assets::setSubTitle('account activation');
2014-05-12 10:31:34 +02:00
$identifier = InputHelper::get('identifier');
2013-11-16 19:24:33 +01:00
if (empty($tokenText))
2013-11-16 19:24:33 +01:00
{
2014-05-12 10:31:34 +02:00
Api::run(
new ActivateUserEmailJob(),
$this->appendUserIdentifierArgument([], $identifier));
Messenger::message('Activation e-mail resent.');
}
else
{
2014-05-12 10:31:34 +02:00
$user = Api::run(new ActivateUserEmailJob(), [
JobArgs::ARG_TOKEN => $tokenText ]);
$message = 'Activation completed successfully.';
2014-05-15 10:32:53 +02:00
if (Core::getConfig()->registration->staffActivation)
$message .= ' However, your account still must be confirmed by staff.';
Messenger::message($message);
2014-05-15 10:32:53 +02:00
if (!Core::getConfig()->registration->staffActivation)
Auth::setCurrentUser($user);
}
2013-11-16 19:24:33 +01:00
}
public function passwordResetView()
2013-11-16 19:24:33 +01:00
{
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
2014-04-29 21:35:29 +02:00
$context->viewName = 'user-select';
Assets::setSubTitle('password reset');
2013-11-16 19:24:33 +01:00
}
public function passwordResetAction($tokenText)
2013-11-16 19:24:33 +01:00
{
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
$context->viewName = 'message';
Assets::setSubTitle('password reset');
2014-05-12 10:31:34 +02:00
$identifier = InputHelper::get('identifier');
2013-11-16 19:24:33 +01:00
if (empty($tokenText))
{
2014-05-12 10:31:34 +02:00
Api::run(
new PasswordResetJob(),
$this->appendUserIdentifierArgument([], $identifier));
2014-04-30 08:08:24 +02:00
Messenger::message('E-mail sent. Follow instructions to reset password.');
}
else
{
$ret = Api::run(new PasswordResetJob(), [ JobArgs::ARG_TOKEN => $tokenText ]);
Messenger::message(sprintf(
'Password reset successful. Your new password is **%s**.',
$ret->newPassword));
2014-04-30 08:08:24 +02:00
Auth::setCurrentUser($ret->user);
}
}
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
private function redirectToLastVisitedUrl()
{
$lastUrl = SessionHelper::getLastVisitedUrl();
if ($lastUrl)
\Chibi\Util\Url::forward($lastUrl);
exit;
}
private function redirectToGenericView($identifier)
{
\Chibi\Util\Url::forward(\Chibi\Router::linkTo(
['UserController', 'genericView'],
['identifier' => $identifier]));
exit;
}
2013-10-05 19:24:08 +02:00
}