szurubooru/src/Controllers/AuthController.php

152 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']))
{
\Chibi\UrlHelper::forward($_SESSION['login-redirect-url']);
unset($_SESSION['login-redirect-url']);
return;
}
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
}
2013-10-22 11:40:10 +02:00
public static function tryLogin($name, $password)
{
$config = \Chibi\Registry::getConfig();
$context = \Chibi\Registry::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)
PrivilegesHelper::confirmEmail($dbUser);
$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
/**
* @route /auth/login
*/
public function loginAction()
{
2013-10-12 22:37:18 +02:00
$this->context->handleExceptions = true;
2013-10-05 21:22:28 +02:00
2013-10-05 12:55:03 +02:00
//check if already logged in
if ($this->context->loggedIn)
{
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
}
}
/**
* @route /auth/logout
*/
public function logoutAction()
{
$this->context->viewName = null;
$this->context->layoutName = null;
self::doLogOut();
2013-10-22 11:40:10 +02:00
setcookie('auth', false, 0, '/');
2013-10-05 19:24:08 +02:00
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
2013-10-05 12:55:03 +02:00
}
public static function doLogOut()
{
unset($_SESSION['user']);
}
public static function doLogIn()
{
$context = \Chibi\Registry::getContext();
if (!isset($_SESSION['user']))
{
if (!empty($context->user) and $context->user->id)
{
$dbUser = UserModel::findById($context->user->id);
2014-02-13 09:10:24 +01:00
$context->user->lastLoginDate = time();
UserModel::save($context->user);
$_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()
{
$context = \Chibi\Registry::getContext();
if ($context->user !== null)
self::doLogOut();
self::doLogIn();
}
2013-10-30 23:24:27 +01:00
public static function observeWorkFinish()
{
if (strpos(\Chibi\HeadersHelper::get('Content-Type'), 'text/html') === false)
return;
$context = \Chibi\Registry::getContext();
if ($context->route->simpleControllerName == 'auth')
return;
$_SESSION['login-redirect-url'] = $context->query;
}
2013-10-05 12:55:03 +02:00
}