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(),
|
|
|
|
[
|
2014-05-12 00:13:18 +02:00
|
|
|
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-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-12 10:31:34 +02:00
|
|
|
public function genericView($identifier, $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(),
|
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', []));
|
2013-10-15 13:14:48 +02:00
|
|
|
|
2014-05-04 16:27:15 +02:00
|
|
|
if ($tab == 'uploads')
|
2014-05-04 20:09:03 +02:00
|
|
|
$query = 'submit:' . $user->getName();
|
2014-05-04 16:27:15 +02:00
|
|
|
elseif ($tab == 'favs')
|
2014-05-04 20:09:03 +02:00
|
|
|
$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-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 (isset($query))
|
2013-10-15 13:14:48 +02:00
|
|
|
{
|
2014-05-04 12:01:14 +02:00
|
|
|
$ret = Api::run(
|
|
|
|
new ListPostsJob(),
|
|
|
|
[
|
2014-05-12 00:13:18 +02:00
|
|
|
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
|
|
|
}
|
2013-10-15 13:14:48 +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');
|
2014-05-14 17:21:12 +02:00
|
|
|
$desiredSafety = PostSafety::makeFlags($suppliedSafety);
|
2014-04-30 08:08:24 +02:00
|
|
|
|
2014-05-14 17:21:12 +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-14 17:21:12 +02:00
|
|
|
getContext()->transport->user = $user;
|
2014-05-05 21:20:40 +02:00
|
|
|
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
|
|
|
|
2014-05-01 22:29:36 +02:00
|
|
|
Messenger::message('Browsing settings updated!');
|
2013-10-22 00:17:06 +02:00
|
|
|
}
|
|
|
|
|
2014-05-14 17:21:12 +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);
|
|
|
|
}
|
|
|
|
|
2014-05-12 10:31:34 +02:00
|
|
|
public function editAction($identifier)
|
2013-10-15 13:14:48 +02:00
|
|
|
{
|
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');
|
2013-10-15 13:14:48 +02:00
|
|
|
|
2014-05-04 13:39:00 +02:00
|
|
|
$args =
|
|
|
|
[
|
2014-05-12 00:13:18 +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-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
|
|
|
|
2014-05-05 21:20:40 +02: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);
|
2013-10-15 00:41:04 +02:00
|
|
|
}
|
|
|
|
|
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));
|
2014-04-27 14:42:39 +02:00
|
|
|
|
2014-05-08 08:54:08 +02:00
|
|
|
$user = UserModel::tryGetById(Auth::getCurrentUser()->getId());
|
2014-05-04 12:01:14 +02:00
|
|
|
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-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-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-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-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));
|
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-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(),
|
|
|
|
[
|
2014-05-12 00:13:18 +02:00
|
|
|
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-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:43:38 +02:00
|
|
|
public function activationAction($tokenText)
|
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');
|
2014-05-12 10:31:34 +02:00
|
|
|
$identifier = InputHelper::get('identifier');
|
2013-11-16 19:24:33 +01:00
|
|
|
|
2014-05-04 15:43:38 +02: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));
|
2014-05-04 15:43:38 +02:00
|
|
|
|
2014-05-04 15:10:51 +02:00
|
|
|
Messenger::message('Activation e-mail resent.');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-12 10:31:34 +02:00
|
|
|
$user = Api::run(new ActivateUserEmailJob(), [
|
|
|
|
JobArgs::ARG_TOKEN => $tokenText ]);
|
2014-05-04 15:43:38 +02:00
|
|
|
|
2014-05-04 15:10:51 +02:00
|
|
|
$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)
|
2014-05-04 15:43:38 +02:00
|
|
|
Auth::setCurrentUser($user);
|
2014-05-04 15:10:51 +02:00
|
|
|
}
|
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:43:38 +02:00
|
|
|
public function passwordResetAction($tokenText)
|
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');
|
2014-05-12 10:31:34 +02:00
|
|
|
$identifier = InputHelper::get('identifier');
|
2013-11-16 19:24:33 +01:00
|
|
|
|
2014-05-04 15:43:38 +02:00
|
|
|
if (empty($tokenText))
|
2014-05-04 15:10:51 +02:00
|
|
|
{
|
2014-05-12 10:31:34 +02:00
|
|
|
Api::run(
|
|
|
|
new PasswordResetJob(),
|
|
|
|
$this->appendUserIdentifierArgument([], $identifier));
|
2014-04-30 08:08:24 +02:00
|
|
|
|
2014-05-04 15:10:51 +02:00
|
|
|
Messenger::message('E-mail sent. Follow instructions to reset password.');
|
|
|
|
}
|
|
|
|
else
|
2013-11-16 18:51:34 +01:00
|
|
|
{
|
2014-05-12 00:13:18 +02:00
|
|
|
$ret = Api::run(new PasswordResetJob(), [ JobArgs::ARG_TOKEN => $tokenText ]);
|
2014-05-04 15:10:51 +02:00
|
|
|
|
2014-05-04 15:43:38 +02:00
|
|
|
Messenger::message(sprintf(
|
|
|
|
'Password reset successful. Your new password is **%s**.',
|
|
|
|
$ret->newPassword));
|
2014-04-30 08:08:24 +02:00
|
|
|
|
2014-05-04 15:43:38 +02:00
|
|
|
Auth::setCurrentUser($ret->user);
|
|
|
|
}
|
2013-11-16 18:51:34 +01:00
|
|
|
}
|
2014-05-04 12:01:14 +02:00
|
|
|
|
|
|
|
private function requirePasswordConfirmation()
|
|
|
|
{
|
|
|
|
$user = getContext()->transport->user;
|
2014-05-05 21:20:40 +02:00
|
|
|
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;
|
|
|
|
}
|
2013-10-05 19:24:08 +02:00
|
|
|
}
|