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)
|
2013-10-15 00:41:04 +02:00
|
|
|
{
|
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', []));
|
2013-10-15 13:14:48 +02:00
|
|
|
|
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';
|
2013-10-15 00:41:04 +02:00
|
|
|
|
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))
|
2013-10-15 13:14:48 +02:00
|
|
|
{
|
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-15 13:14:48 +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;
|
|
|
|
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(
|
2014-04-27 14:42:39 +02:00
|
|
|
Privilege::ChangeUserSettings,
|
2014-04-29 23:52:17 +02:00
|
|
|
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
|
|
|
|
2014-05-01 22:29:36 +02:00
|
|
|
Messenger::message('Browsing settings updated!');
|
2013-10-22 00:17:06 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 13:14:48 +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');
|
2013-10-15 13:14:48 +02:00
|
|
|
|
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);
|
2013-10-15 00:41:04 +02:00
|
|
|
}
|
|
|
|
|
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-04-27 14:42:39 +02:00
|
|
|
|
2014-05-04 12:01:14 +02:00
|
|
|
$user = UserModel::findById(Auth::getCurrentUser()->id, false);
|
|
|
|
if (!$user)
|
|
|
|
Auth::logOut();
|
2013-10-15 13:14:48 +02:00
|
|
|
|
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();
|
|
|
|
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(
|
2014-04-27 14:42:39 +02:00
|
|
|
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
|
|
|
{
|
2014-05-02 22:30:14 +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
|
|
|
|
2014-05-01 22:29:36 +02:00
|
|
|
Messenger::message($message);
|
2013-10-16 18:07:23 +02:00
|
|
|
}
|
|
|
|
|
2014-05-04 15:10:51 +02:00
|
|
|
public function activationView()
|
2013-10-16 18:07:23 +02:00
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2014-05-04 15:10:51 +02:00
|
|
|
$context->viewName = 'user-select';
|
2014-04-29 21:35:29 +02:00
|
|
|
Assets::setSubTitle('account activation');
|
2013-10-16 18:07:23 +02:00
|
|
|
}
|
2013-11-16 18:51:34 +01:00
|
|
|
|
2014-05-04 15:10:51 +02:00
|
|
|
public function activationAction($token)
|
2013-11-16 18:51:34 +01:00
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
|
|
|
$context->viewName = 'message';
|
2014-05-04 15:10:51 +02:00
|
|
|
Assets::setSubTitle('account activation');
|
2013-11-16 19:24:33 +01:00
|
|
|
|
2014-05-04 15:10:51 +02:00
|
|
|
if (empty($token))
|
2013-11-16 19:24:33 +01:00
|
|
|
{
|
2014-05-04 15:10:51 +02:00
|
|
|
$name = InputHelper::get('name');
|
|
|
|
$user = UserModel::findByNameOrEmail($name);
|
|
|
|
if (empty($user->emailUnconfirmed))
|
|
|
|
{
|
|
|
|
if (!empty($user->emailConfirmed))
|
|
|
|
throw new SimpleException('E-mail was already confirmed; activation skipped');
|
|
|
|
else
|
|
|
|
throw new SimpleException('This user has no e-mail specified; activation cannot proceed');
|
|
|
|
}
|
|
|
|
EditUserEmailJob::sendEmail($user);
|
|
|
|
Messenger::message('Activation e-mail resent.');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$dbToken = TokenModel::findByToken($token);
|
|
|
|
TokenModel::checkValidity($dbToken);
|
|
|
|
|
|
|
|
$dbUser = $dbToken->getUser();
|
|
|
|
if (empty($dbUser->emailConfirmed))
|
|
|
|
{
|
|
|
|
$dbUser->emailConfirmed = $dbUser->emailUnconfirmed;
|
|
|
|
$dbUser->emailUnconfirmed = null;
|
|
|
|
}
|
|
|
|
$dbToken->used = true;
|
|
|
|
TokenModel::save($dbToken);
|
|
|
|
UserModel::save($dbUser);
|
|
|
|
|
|
|
|
LogHelper::log('{subject} just activated account', ['subject' => TextHelper::reprUser($dbUser)]);
|
|
|
|
$message = 'Activation completed successfully.';
|
|
|
|
if (getConfig()->registration->staffActivation)
|
|
|
|
$message .= ' However, your account still must be confirmed by staff.';
|
|
|
|
Messenger::message($message);
|
2013-11-16 18:51:34 +01:00
|
|
|
|
2014-05-04 15:10:51 +02:00
|
|
|
if (!getConfig()->registration->staffActivation)
|
|
|
|
{
|
|
|
|
Auth::setCurrentUser($dbUser);
|
|
|
|
}
|
|
|
|
}
|
2013-11-16 19:24:33 +01:00
|
|
|
}
|
|
|
|
|
2014-05-04 15:10:51 +02: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
|
|
|
}
|
|
|
|
|
2014-05-04 15:10:51 +02:00
|
|
|
public function passwordResetAction($token)
|
2013-11-16 19:24:33 +01:00
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2014-05-04 15:10:51 +02:00
|
|
|
$context->viewName = 'message';
|
|
|
|
Assets::setSubTitle('password reset');
|
2013-11-16 19:24:33 +01:00
|
|
|
|
2014-05-04 15:10:51 +02:00
|
|
|
if (empty($token))
|
|
|
|
{
|
|
|
|
$name = InputHelper::get('name');
|
|
|
|
$user = UserModel::findByNameOrEmail($name);
|
|
|
|
if (empty($user->emailConfirmed))
|
|
|
|
throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed');
|
2014-04-30 08:08:24 +02:00
|
|
|
|
2014-05-04 15:10:51 +02:00
|
|
|
self::sendPasswordResetConfirmation($user);
|
|
|
|
Messenger::message('E-mail sent. Follow instructions to reset password.');
|
|
|
|
}
|
|
|
|
else
|
2013-11-16 18:51:34 +01:00
|
|
|
{
|
2014-05-04 15:10:51 +02:00
|
|
|
$dbToken = TokenModel::findByToken($token);
|
|
|
|
TokenModel::checkValidity($dbToken);
|
|
|
|
|
|
|
|
$alphabet = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9'));
|
|
|
|
$randomPassword = join('', array_map(function($x) use ($alphabet)
|
|
|
|
{
|
|
|
|
return $alphabet[$x];
|
|
|
|
}, array_rand($alphabet, 8)));
|
|
|
|
|
|
|
|
$dbUser = $dbToken->getUser();
|
|
|
|
$dbUser->passHash = UserModel::hashPassword($randomPassword, $dbUser->passSalt);
|
|
|
|
$dbToken->used = true;
|
|
|
|
TokenModel::save($dbToken);
|
|
|
|
UserModel::save($dbUser);
|
|
|
|
|
|
|
|
LogHelper::log('{subject} just reset password', ['subject' => TextHelper::reprUser($dbUser)]);
|
|
|
|
$message = 'Password reset successful. Your new password is **' . $randomPassword . '**.';
|
|
|
|
Messenger::message($message);
|
|
|
|
|
|
|
|
Auth::setCurrentUser($dbUser);
|
2013-11-16 18:51:34 +01:00
|
|
|
}
|
2014-04-30 08:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function sendPasswordResetConfirmation($user)
|
|
|
|
{
|
|
|
|
$regConfig = getConfig()->registration;
|
|
|
|
|
2014-05-04 13:39:00 +02:00
|
|
|
$mail = new Mail();
|
|
|
|
$mail->body = $regConfig->passwordResetEmailBody;
|
|
|
|
$mail->subject = $regConfig->passwordResetEmailSubject;
|
|
|
|
$mail->senderName = $regConfig->passwordResetEmailSenderName;
|
|
|
|
$mail->senderEmail = $regConfig->passwordResetEmailSenderEmail;
|
|
|
|
$mail->recipientEmail = $user->emailConfirmed;
|
|
|
|
|
|
|
|
return Mailer::sendMailWithTokenLink(
|
2014-04-30 08:08:24 +02:00
|
|
|
$user,
|
2014-05-04 13:39:00 +02:00
|
|
|
['UserController', 'passwordResetAction'],
|
|
|
|
$mail);
|
2013-11-16 18:51:34 +01:00
|
|
|
}
|
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
|
|
|
}
|