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

View File

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

View File

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