szurubooru/src/Controllers/AuthController.php

58 lines
1.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
{
/**
* @route /auth/login
*/
public function loginAction()
{
2013-10-12 22:37:18 +02:00
$this->context->handleExceptions = true;
2013-10-12 19:28:52 +02:00
$this->context->stylesheets []= 'auth.css';
2013-10-06 18:50:06 +02:00
$this->context->subTitle = 'authentication form';
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-05 19:24:08 +02:00
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
2013-10-05 12:55:03 +02:00
return;
}
$suppliedName = InputHelper::get('name');
$suppliedPassword = InputHelper::get('password');
if ($suppliedName !== null and $suppliedPassword !== null)
2013-10-05 12:55:03 +02:00
{
$dbUser = R::findOne('user', 'name = ?', [$suppliedName]);
2013-10-05 12:55:03 +02:00
if ($dbUser === null)
throw new SimpleException('Invalid username');
$suppliedPasswordHash = Model_User::hashPassword($suppliedPassword, $dbUser->pass_salt);
if ($suppliedPasswordHash != $dbUser->pass_hash)
2013-10-05 12:55:03 +02:00
throw new SimpleException('Invalid password');
if (!$dbUser->staff_confirmed and $this->config->registration->staffActivation)
throw new SimpleException('Staff hasn\'t confirmed your registration yet');
2013-10-05 12:55:03 +02:00
if ($dbUser->banned)
throw new SimpleException('You are banned');
2013-10-16 18:07:23 +02:00
if ($this->config->registration->needEmailForRegistering)
PrivilegesHelper::confirmEmail($dbUser);
2013-10-05 12:55:03 +02:00
$_SESSION['user-id'] = $dbUser->id;
2013-10-05 19:24:08 +02:00
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
2013-10-05 12:55:03 +02:00
$this->context->transport->success = true;
}
}
/**
* @route /auth/logout
*/
public function logoutAction()
{
$this->context->viewName = null;
$this->context->viewName = null;
unset($_SESSION['user-id']);
2013-10-05 19:24:08 +02:00
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
2013-10-05 12:55:03 +02:00
}
}