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

297 lines
7.8 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(),
[
ListUsersJob::PAGE_NUMBER => $page,
ListUsersJob::QUERY => $filter,
]);
2013-10-16 13:07:01 +02:00
2014-05-04 10:32:32 +02:00
$context = 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-04 12:01:14 +02:00
public function genericView($name, $tab = 'favs', $page = 1)
{
2014-05-04 12:45:26 +02:00
$user = Api::run(
new GetUserJob(),
[
GetUserJob::USER_NAME => $name,
]);
2014-05-04 12:01:14 +02:00
$flagged = in_array(TextHelper::reprUser($user), SessionHelper::get('flagged', []));
2014-05-04 10:57:12 +02:00
$context = 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 ($tab == 'uploads')
$query = 'submit:' . $user->name;
elseif ($tab == 'favs')
$query = 'fav:' . $user->name;
2014-04-30 08:08:24 +02:00
2014-05-04 12:01:14 +02:00
if (isset($query))
{
2014-05-04 12:01:14 +02:00
$ret = Api::run(
new ListPostsJob(),
[
ListPostsJob::PAGE_NUMBER => $page,
ListPostsJob::QUERY => $query
]);
$context->transport->posts = $ret->entities;
$context->transport->paginator = $ret;
$context->transport->lastSearchQuery = $query;
2014-04-30 08:08:24 +02:00
}
}
2013-10-22 00:17:06 +02:00
public function settingsAction($name)
{
2014-05-04 12:01:14 +02:00
$this->genericView($name, 'settings');
$user = getContext()->transport->user;
Access::assert(
Privilege::ChangeUserSettings,
Access::getIdentity($user));
2013-10-22 00:17:06 +02:00
2014-04-30 08:08:24 +02:00
$suppliedSafety = InputHelper::get('safety');
if (!is_array($suppliedSafety))
$suppliedSafety = [];
foreach (PostSafety::getAll() as $safety)
$user->enableSafety($safety, in_array($safety, $suppliedSafety));
$user->enableEndlessScrolling(InputHelper::get('endless-scrolling'));
$user->enablePostTagTitles(InputHelper::get('post-tag-titles'));
$user->enableHidingDislikedPosts(InputHelper::get('hide-disliked-posts'));
if ($user->accessRank != AccessRank::Anonymous)
UserModel::save($user);
2014-05-01 16:12:37 +02:00
if ($user->id == Auth::getCurrentUser()->id)
Auth::setCurrentUser($user);
2014-05-04 12:01:14 +02:00
Messenger::message('Browsing settings updated!');
2013-10-22 00:17:06 +02:00
}
public function editAction($name)
{
2014-05-04 12:01:14 +02:00
$this->genericView($name, 'edit');
$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 =
[
EditUserNameJob::USER_NAME => $name,
EditUserNameJob::NEW_USER_NAME => InputHelper::get('name'),
EditUserPasswordJob::NEW_PASSWORD => InputHelper::get('password1'),
EditUserEmailJob::NEW_EMAIL => InputHelper::get('email'),
EditUserAccessRankJob::NEW_ACCESS_RANK => InputHelper::get('access-rank'),
];
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
2014-05-04 12:01:14 +02:00
if (Auth::getCurrentUser()->id == $user->id)
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-04 12:01:14 +02:00
public function deleteAction($name)
2013-10-05 19:24:08 +02:00
{
2014-05-04 12:01:14 +02:00
$this->genericView($name, 'delete');
$this->requirePasswordConfirmation();
2013-10-14 10:22:53 +02:00
2014-05-04 12:01:14 +02:00
Api::run(new DeleteUserJob(), [
DeleteUserJob::USER_NAME => $name]);
2014-05-04 12:01:14 +02:00
$user = UserModel::findById(Auth::getCurrentUser()->id, false);
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-04 12:01:14 +02:00
public function flagAction($name)
{
Api::run(new FlagUserJob(), [FlagUserJob::USER_NAME => $name]);
}
2013-10-14 10:22:53 +02:00
2014-05-04 12:01:14 +02:00
public function banAction($name)
{
Api::run(new ToggleUserBanJob(), [
ToggleUserBanJob::USER_NAME => $name,
ToggleUserBanJob::STATE => true]);
}
public function unbanAction($name)
{
Api::run(new ToggleUserBanJob(), [
ToggleUserBanJob::USER_NAME => $name,
ToggleUserBanJob::STATE => false]);
}
public function acceptRegistrationAction($name)
{
Api::run(new AcceptUserRegistrationJob(), [
AcceptUserRegistrationJob::USER_NAME => $name]);
2013-10-05 19:24:08 +02:00
}
2013-10-14 00:25:40 +02:00
public function toggleSafetyAction($safety)
{
2014-05-01 16:12:37 +02:00
$user = Auth::getCurrentUser();
Access::assert(
Privilege::ChangeUserSettings,
2014-05-01 16:12:37 +02:00
Access::getIdentity($user));
2013-10-14 00:25:40 +02:00
if (!in_array($safety, PostSafety::getAll()))
throw new SimpleExcetpion('Invalid safety');
2014-05-01 16:12:37 +02:00
$user->enableSafety($safety, !$user->hasEnabledSafety($safety));
2013-10-14 00:25:40 +02:00
2014-05-01 16:12:37 +02:00
if ($user->accessRank != AccessRank::Anonymous)
UserModel::save($user);
Auth::setCurrentUser($user);
2013-10-14 00:25:40 +02:00
}
2013-10-16 18:07:23 +02:00
2014-05-04 14:57:44 +02:00
public function registrationView()
2013-10-16 18:07:23 +02:00
{
2014-04-29 21:35:29 +02:00
$context = getContext();
$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(),
[
EditUserNameJob::NEW_USER_NAME => InputHelper::get('name'),
EditUserPasswordJob::NEW_PASSWORD => InputHelper::get('password1'),
EditUserEmailJob::NEW_EMAIL => InputHelper::get('email'),
]);
2013-10-16 18:07:23 +02:00
2014-05-04 14:57:44 +02:00
if (!getConfig()->registration->needEmailForRegistering and !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.';
if (getConfig()->registration->staffActivation)
$message .= ' After this, your registration must be confirmed by staff.';
}
elseif (getConfig()->registration->staffActivation)
$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-04-29 21:35:29 +02:00
$context = 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-04-29 21:35:29 +02:00
$context = getContext();
$context->viewName = 'message';
Assets::setSubTitle('account activation');
$name = InputHelper::get('name');
2013-11-16 19:24:33 +01:00
if (empty($tokenText))
2013-11-16 19:24:33 +01:00
{
Api::run(new ActivateUserEmailJob(), [ ActivateUserEmailJob::USER_NAME => $name ]);
Messenger::message('Activation e-mail resent.');
}
else
{
$user = Api::run(new ActivateUserEmailJob(), [ ActivateUserEmailJob::TOKEN => $tokenText ]);
$message = 'Activation completed successfully.';
if (getConfig()->registration->staffActivation)
$message .= ' However, your account still must be confirmed by staff.';
Messenger::message($message);
if (!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-04-29 21:35:29 +02:00
$context = getContext();
$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-04-29 21:35:29 +02:00
$context = getContext();
$context->viewName = 'message';
Assets::setSubTitle('password reset');
$name = InputHelper::get('name');
2013-11-16 19:24:33 +01:00
if (empty($tokenText))
{
Api::run(new PasswordResetJob(), [ PasswordResetJob::USER_NAME => $name ]);
2014-04-30 08:08:24 +02:00
Messenger::message('E-mail sent. Follow instructions to reset password.');
}
else
{
$ret = Api::run(new PasswordResetJob(), [ PasswordResetJob::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()
{
$user = getContext()->transport->user;
if (Auth::getCurrentUser()->id == $user->id)
{
$suppliedPassword = InputHelper::get('current-password');
$suppliedPasswordHash = UserModel::hashPassword($suppliedPassword, $user->passSalt);
if ($suppliedPasswordHash != $user->passHash)
throw new SimpleException('Must supply valid password');
}
}
2013-10-05 19:24:08 +02:00
}