2016-01-15 03:45:22 +01:00
|
|
|
<?php
|
2016-07-04 20:57:00 +02:00
|
|
|
require_once 'libs/Validator.php';
|
2016-08-04 20:18:29 +02:00
|
|
|
require_once 'models/Session.php';
|
2016-01-15 03:45:22 +01:00
|
|
|
|
2016-03-05 00:36:11 +01:00
|
|
|
abstract class Controller {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance-related stuff
|
|
|
|
*/
|
|
|
|
abstract public function handler();
|
2016-07-04 20:57:00 +02:00
|
|
|
abstract public function validations();
|
2016-03-05 00:36:11 +01:00
|
|
|
|
|
|
|
public function getHandler() {
|
|
|
|
return function () {
|
2016-07-04 20:57:00 +02:00
|
|
|
try {
|
|
|
|
$this->validate();
|
|
|
|
} catch (ValidationException $exception) {
|
|
|
|
Response::respondError($exception->getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-05 00:36:11 +01:00
|
|
|
$this->handler();
|
|
|
|
};
|
|
|
|
}
|
2016-07-04 20:57:00 +02:00
|
|
|
|
|
|
|
public function validate() {
|
|
|
|
$validator = new Validator();
|
|
|
|
|
|
|
|
$validator->validate($this->validations());
|
|
|
|
}
|
2016-03-05 00:36:11 +01:00
|
|
|
|
2016-01-15 03:45:22 +01:00
|
|
|
public static function request($key) {
|
2016-03-05 00:36:11 +01:00
|
|
|
$app = self::getAppInstance();
|
2016-01-15 03:45:22 +01:00
|
|
|
|
|
|
|
return $app->request()->post($key);
|
|
|
|
}
|
2016-07-04 20:57:00 +02:00
|
|
|
|
2016-01-15 03:45:22 +01:00
|
|
|
public static function getLoggedUser() {
|
2016-09-25 06:16:10 +02:00
|
|
|
$session = Session::getInstance();
|
|
|
|
|
|
|
|
if ($session->isStaffLogged()) {
|
|
|
|
return Staff::getUser((int)self::request('csrf_userid'));
|
|
|
|
} else {
|
|
|
|
return User::getUser((int)self::request('csrf_userid'));
|
|
|
|
}
|
2016-01-15 03:45:22 +01:00
|
|
|
}
|
|
|
|
|
2016-08-04 20:18:29 +02:00
|
|
|
public static function isUserLogged() {
|
|
|
|
$session = Session::getInstance();
|
|
|
|
|
|
|
|
return $session->checkAuthentication(array(
|
|
|
|
'userId' => Controller::request('csrf_userid'),
|
|
|
|
'token' => Controller::request('csrf_token')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2016-09-25 06:16:10 +02:00
|
|
|
public static function isStaffLogged($level = 1) {
|
|
|
|
return Controller::isUserLogged() && (Controller::getLoggedUser()->level >= $level);
|
2016-08-04 20:18:29 +02:00
|
|
|
}
|
|
|
|
|
2016-03-05 00:36:11 +01:00
|
|
|
public static function getAppInstance() {
|
|
|
|
return \Slim\Slim::getInstance();
|
|
|
|
}
|
2016-01-15 03:45:22 +01:00
|
|
|
}
|