Cleanup code

This commit is contained in:
Giuseppe Criscione 2018-10-13 13:20:15 +02:00
parent 98965fea4c
commit c433cf3f5a
3 changed files with 22 additions and 15 deletions

View File

@ -21,14 +21,14 @@ class Admin
public static $instance; public static $instance;
protected $errors;
protected $router; protected $router;
protected $users; protected $users;
protected $language; protected $language;
protected $errors;
public function __construct() public function __construct()
{ {
if (!is_null(static::$instance)) { if (!is_null(static::$instance)) {
@ -36,8 +36,6 @@ class Admin
} }
static::$instance = $this; static::$instance = $this;
$this->errors = new Controllers\Errors();
if (!Formwork::instance()->option('admin.enabled')) { if (!Formwork::instance()->option('admin.enabled')) {
$this->redirectToSite(302, true); $this->redirectToSite(302, true);
} }
@ -45,12 +43,8 @@ class Admin
$this->router = new Router(Uri::removeQuery(HTTPRequest::uri())); $this->router = new Router(Uri::removeQuery(HTTPRequest::uri()));
$this->users = Users::load(); $this->users = Users::load();
$this->loadLanguage(); $this->loadLanguages();
$this->loadErrorHandler();
set_exception_handler(function ($exception) {
$this->errors->internalServerError();
throw $exception;
});
} }
public static function instance() public static function instance()
@ -63,10 +57,11 @@ class Admin
public function isLoggedIn() public function isLoggedIn()
{ {
return !is_null($user = Session::get('FORMWORK_USERNAME')) && $this->users->has($user); $username = Session::get('FORMWORK_USERNAME');
return !empty($username) && $this->users->has($username);
} }
public function loggedUser() public function user()
{ {
$username = Session::get('FORMWORK_USERNAME'); $username = Session::get('FORMWORK_USERNAME');
return $this->users->get($username); return $this->users->get($username);
@ -97,15 +92,24 @@ class Admin
} }
} }
protected function loadLanguage() protected function loadLanguages()
{ {
$languageCode = Formwork::instance()->option('admin.lang'); $languageCode = Formwork::instance()->option('admin.lang');
if ($this->isLoggedIn()) { if ($this->isLoggedIn()) {
$languageCode = $this->loggedUser()->get('language', $languageCode); $languageCode = $this->user()->get('language', $languageCode);
} }
$this->language = Language::load($languageCode); $this->language = Language::load($languageCode);
} }
protected function loadErrorHandler()
{
$this->errors = new Controllers\Errors();
set_exception_handler(function ($exception) {
$this->errors->internalServerError();
throw $exception;
});
}
protected function validateContentLength() protected function validateContentLength()
{ {
if (!is_null(HTTPRequest::contentLength())) { if (!is_null(HTTPRequest::contentLength())) {

View File

@ -46,7 +46,7 @@ abstract class AbstractController
protected function user() protected function user()
{ {
return Admin::instance()->loggedUser(); return Admin::instance()->user();
} }
protected function escape($string) protected function escape($string)

View File

@ -41,10 +41,12 @@ class Authentication extends AbstractController
break; break;
case 'POST': case 'POST':
// Delay request processing for 0.5-1s
usleep(rand(500, 1000) * 1e3); usleep(rand(500, 1000) * 1e3);
$data = new DataGetter(HTTPRequest::postData()); $data = new DataGetter(HTTPRequest::postData());
// Ensure no required data is missing
if (!$data->has(array('username', 'password'))) { if (!$data->has(array('username', 'password'))) {
$this->error(); $this->error();
} }
@ -53,6 +55,7 @@ class Authentication extends AbstractController
$user = Admin::instance()->users()->get($data->get('username')); $user = Admin::instance()->users()->get($data->get('username'));
// Authenticate user
if (!is_null($user) && $user->authenticate($data->get('password'))) { if (!is_null($user) && $user->authenticate($data->get('password'))) {
Session::set('FORMWORK_USERNAME', $data->get('username')); Session::set('FORMWORK_USERNAME', $data->get('username'));