szurubooru/src/Controllers/AuthController.php

150 lines
3.6 KiB
PHP
Raw Normal View History

2013-10-05 12:55:03 +02:00
<?php
2013-10-05 21:24:20 +02:00
class AuthController
2013-10-05 12:55:03 +02:00
{
2013-10-30 23:24:27 +01:00
private static function redirectAfterLog()
{
if (isset($_SESSION['login-redirect-url']))
{
2014-04-29 21:35:29 +02:00
\Chibi\Util\Url::forward(\Chibi\Util\Url::makeAbsolute($_SESSION['login-redirect-url']));
2013-10-30 23:24:27 +01:00
unset($_SESSION['login-redirect-url']);
return;
}
2014-04-29 21:35:29 +02:00
\Chibi\Util\Url::forward(\Chibi\Router::linkTo(['IndexController', 'indexAction']));
2013-10-30 23:24:27 +01:00
}
2013-10-22 11:40:10 +02:00
public static function tryLogin($name, $password)
{
2014-04-29 21:35:29 +02:00
$config = getConfig();
$context = getContext();
2013-10-22 11:40:10 +02:00
$dbUser = UserModel::findByNameOrEmail($name, false);
2013-10-22 11:40:10 +02:00
if ($dbUser === null)
throw new SimpleException('Invalid username');
$passwordHash = UserModel::hashPassword($password, $dbUser->passSalt);
if ($passwordHash != $dbUser->passHash)
2013-10-22 11:40:10 +02:00
throw new SimpleException('Invalid password');
if (!$dbUser->staffConfirmed and $config->registration->staffActivation)
2013-10-22 11:40:10 +02:00
throw new SimpleException('Staff hasn\'t confirmed your registration yet');
if ($dbUser->banned)
throw new SimpleException('You are banned');
if ($config->registration->needEmailForRegistering)
Access::requireEmail($dbUser);
2013-10-22 11:40:10 +02:00
$context->user = $dbUser;
self::doReLog();
2013-10-22 11:40:10 +02:00
return $dbUser;
}
public static function tryAutoLogin()
{
if (!isset($_COOKIE['auth']))
return;
$token = TextHelper::decrypt($_COOKIE['auth']);
list ($name, $password) = array_map('base64_decode', explode('|', $token));
return self::tryLogin($name, $password);
}
2013-10-05 12:55:03 +02:00
public function loginAction()
{
2014-04-29 21:35:29 +02:00
$context = getContext();
$context->handleExceptions = true;
2013-10-05 21:22:28 +02:00
2013-10-05 12:55:03 +02:00
//check if already logged in
2014-04-29 21:35:29 +02:00
if ($context->loggedIn)
2013-10-05 12:55:03 +02:00
{
2013-10-30 23:24:27 +01:00
self::redirectAfterLog();
2013-10-05 12:55:03 +02:00
return;
}
2013-10-22 11:40:10 +02:00
if (InputHelper::get('submit'))
2013-10-05 12:55:03 +02:00
{
2013-10-22 11:40:10 +02:00
$suppliedName = InputHelper::get('name');
$suppliedPassword = InputHelper::get('password');
$dbUser = self::tryLogin($suppliedName, $suppliedPassword);
2013-10-05 12:55:03 +02:00
2013-10-22 11:40:10 +02:00
if (InputHelper::get('remember'))
{
$token = implode('|', [base64_encode($suppliedName), base64_encode($suppliedPassword)]);
setcookie('auth', TextHelper::encrypt($token), time() + 365 * 24 * 3600, '/');
}
2013-11-16 18:40:26 +01:00
StatusHelper::success();
2013-10-30 23:24:27 +01:00
self::redirectAfterLog();
2013-10-05 12:55:03 +02:00
}
}
public function logoutAction()
{
2014-04-29 21:35:29 +02:00
$context = getContext();
$context->viewName = null;
$context->layoutName = null;
self::doLogOut();
2013-10-22 11:40:10 +02:00
setcookie('auth', false, 0, '/');
2014-04-29 21:35:29 +02:00
\Chibi\Util\Url::forward(\Chibi\Router::linkTo(['IndexController', 'indexAction']));
2013-10-05 12:55:03 +02:00
}
public static function doLogOut()
{
unset($_SESSION['user']);
}
public static function doLogIn()
{
2014-04-29 21:35:29 +02:00
$context = getContext();
if (!isset($_SESSION['user']))
{
if (!empty($context->user) and $context->user->id)
{
$dbUser = UserModel::findById($context->user->id);
2014-03-02 19:09:05 +01:00
$dbUser->lastLoginDate = time();
UserModel::save($dbUser);
$_SESSION['user'] = serialize($dbUser);
}
else
{
$dummy = UserModel::spawn();
$dummy->name = UserModel::getAnonymousName();
$dummy->accessRank = AccessRank::Anonymous;
$_SESSION['user'] = serialize($dummy);
}
}
$context->user = unserialize($_SESSION['user']);
$context->loggedIn = $context->user->accessRank != AccessRank::Anonymous;
if (!$context->loggedIn)
{
try
{
self::tryAutoLogin();
}
catch (Exception $e)
{
}
}
}
public static function doReLog()
{
2014-04-29 21:35:29 +02:00
$context = getContext();
if ($context->user !== null)
self::doLogOut();
self::doLogIn();
}
2013-10-30 23:24:27 +01:00
public static function observeWorkFinish()
{
2014-04-29 21:35:29 +02:00
if (strpos(\Chibi\Util\Headers::get('Content-Type'), 'text/html') === false)
2013-10-30 23:24:27 +01:00
return;
2014-04-29 21:35:29 +02:00
if (\Chibi\Util\Headers::getCode() != 200)
return;
$context = getContext();
if ($context->simpleControllerName == 'auth')
2013-10-30 23:24:27 +01:00
return;
$_SESSION['login-redirect-url'] = $context->query;
}
2013-10-05 12:55:03 +02:00
}