- #84 CREST Login (WIP)
- New CREST controller - Database restructuring - improved type-casting for some controller functions - New login process - Fixed some bugs during the setup process (/setup root) - Added CREST request caching by response headers
This commit is contained in:
@@ -7,50 +7,62 @@
|
||||
*/
|
||||
|
||||
namespace Controller;
|
||||
use Controller\Api as Api;
|
||||
use Model;
|
||||
use DB;
|
||||
|
||||
class Controller {
|
||||
|
||||
/**
|
||||
* @var \Base
|
||||
*/
|
||||
protected $f3;
|
||||
private $template;
|
||||
|
||||
/**
|
||||
* @param mixed $template
|
||||
* @var string template for render
|
||||
*/
|
||||
public function setTemplate($template){
|
||||
protected $template;
|
||||
|
||||
/**
|
||||
* @param string $template
|
||||
*/
|
||||
protected function setTemplate($template){
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return string
|
||||
*/
|
||||
public function getTemplate(){
|
||||
protected function getTemplate(){
|
||||
return $this->template;
|
||||
}
|
||||
|
||||
/**
|
||||
* set global f3 instance
|
||||
* @param null $f3
|
||||
* @return null|static
|
||||
* set $f3 base object
|
||||
* @param \Base $f3
|
||||
*/
|
||||
protected function getF3($f3 = null){
|
||||
if(is_object($f3)){
|
||||
$this->f3 = $f3;
|
||||
}else{
|
||||
$this->f3 = \Base::instance();
|
||||
}
|
||||
protected function setF3(\Base $f3){
|
||||
$this->f3 = $f3;
|
||||
}
|
||||
|
||||
/**
|
||||
* get $f3 base object
|
||||
* @return \Base
|
||||
*/
|
||||
protected function getF3(){
|
||||
if( !($this->f3 instanceof \Base) ){
|
||||
$this->setF3( \Base::instance() );
|
||||
}
|
||||
return $this->f3;
|
||||
}
|
||||
|
||||
/**
|
||||
* event handler for all "views"
|
||||
* some global template variables are set in here
|
||||
* @param $f3
|
||||
* @param \Base $f3
|
||||
*/
|
||||
function beforeroute($f3) {
|
||||
$this->getF3($f3);
|
||||
function beforeroute(\Base $f3) {
|
||||
$this->setF3($f3);
|
||||
|
||||
// initiate DB connection
|
||||
DB\Database::instance('PF');
|
||||
@@ -73,8 +85,9 @@ class Controller {
|
||||
/**
|
||||
* event handler after routing
|
||||
* -> render view
|
||||
* @param \Base $f3
|
||||
*/
|
||||
public function afterroute($f3){
|
||||
public function afterroute(\Base $f3){
|
||||
if($this->getTemplate()){
|
||||
// Ajax calls don´t need a page render..
|
||||
// this happens on client side
|
||||
@@ -85,7 +98,7 @@ class Controller {
|
||||
/**
|
||||
* set change the DB connection
|
||||
* @param string $database
|
||||
* @return mixed|void
|
||||
* @return DB\SQL
|
||||
*/
|
||||
protected function getDB($database = 'PF'){
|
||||
return DB\Database::instance()->getDB($database);
|
||||
@@ -96,49 +109,47 @@ class Controller {
|
||||
*/
|
||||
protected function initSession(){
|
||||
// init DB Session (not file based)
|
||||
if( $this->getDB('PF') instanceof \DB\SQL){
|
||||
new \DB\SQL\Session($this->getDB('PF'));
|
||||
if( $this->getDB('PF') instanceof DB\SQL){
|
||||
new DB\SQL\Session($this->getDB('PF'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get current user model
|
||||
* get current character model
|
||||
* @param int $ttl
|
||||
* @return bool|null
|
||||
* @return Model\CharacterModel|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function _getUser($ttl = 5){
|
||||
$user = false;
|
||||
public function getCharacter($ttl = 5){
|
||||
$character = null;
|
||||
|
||||
if( $this->f3->exists('SESSION.user.id') ){
|
||||
$userId = (int)$this->f3->get('SESSION.user.id');
|
||||
if( $this->getF3()->exists(Api\User::SESSION_KEY_CHARACTER_ID) ){
|
||||
$characterId = (int)$this->getF3()->get(Api\User::SESSION_KEY_CHARACTER_ID);
|
||||
if($characterId){
|
||||
/**
|
||||
* @var $characterModel \Model\CharacterModel
|
||||
*/
|
||||
$characterModel = Model\BasicModel::getNew('CharacterModel');
|
||||
$characterModel->getById($characterId, $ttl);
|
||||
|
||||
if($userId > 0){
|
||||
$userModel = Model\BasicModel::getNew('UserModel', $ttl);
|
||||
$userModel->getById($userId, $ttl);
|
||||
|
||||
if( !$userModel->dry() ){
|
||||
$user = $userModel;
|
||||
if( !$characterModel->dry() ){
|
||||
$character = &$characterModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $user;
|
||||
return $character;
|
||||
}
|
||||
|
||||
/**
|
||||
* log the current user out
|
||||
* @param $f3
|
||||
* log out current user
|
||||
* @param \Base $f3
|
||||
*/
|
||||
public function logOut($f3){
|
||||
|
||||
public function logOut(\Base $f3){
|
||||
// destroy session
|
||||
$f3->clear('SESSION');
|
||||
|
||||
if( !$f3->get('AJAX') ){
|
||||
// redirect to landing page
|
||||
$f3->reroute('@login');
|
||||
}else{
|
||||
if( $f3->get('AJAX') ){
|
||||
$params = $f3->get('POST');
|
||||
$return = (object) [];
|
||||
if(
|
||||
@@ -148,33 +159,36 @@ class Controller {
|
||||
$return->reroute = rtrim(self::getEnvironmentData('URL'), '/') . $f3->alias('login');
|
||||
}else{
|
||||
// no reroute -> errors can be shown
|
||||
$return->error[] = $this->getUserLoggedOffError();
|
||||
$return->error[] = $this->getLogoutError();
|
||||
}
|
||||
|
||||
echo json_encode($return);
|
||||
die();
|
||||
}else{
|
||||
// redirect to landing page
|
||||
$f3->reroute('@login');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* verifies weather a given username and password is valid
|
||||
* @param $userName
|
||||
* @param $password
|
||||
* @param string $userName
|
||||
* @param string $password
|
||||
* @return Model\UserModel|null
|
||||
*/
|
||||
protected function _verifyUser($userName, $password) {
|
||||
|
||||
$validUser = null;
|
||||
|
||||
/**
|
||||
* @var $user \Model\UserModel
|
||||
*/
|
||||
$user = Model\BasicModel::getNew('UserModel', 0);
|
||||
|
||||
$user->getByName($userName);
|
||||
|
||||
// check userName is valid
|
||||
if( !$user->dry() ){
|
||||
// check if password is valid
|
||||
$isValid = $user->verify($password);
|
||||
|
||||
if($isValid === true){
|
||||
$validUser = $user;
|
||||
}
|
||||
@@ -185,18 +199,16 @@ class Controller {
|
||||
|
||||
/**
|
||||
* check weather the page is IGB trusted or not
|
||||
* @return mixed
|
||||
* @return boolean
|
||||
*/
|
||||
static function isIGBTrusted(){
|
||||
|
||||
$igbHeaderData = self::getIGBHeaderData();
|
||||
|
||||
return $igbHeaderData->trusted;
|
||||
}
|
||||
|
||||
/**
|
||||
* get all eve IGB specific header data
|
||||
* @return object
|
||||
* @return \stdClass
|
||||
*/
|
||||
static function getIGBHeaderData(){
|
||||
$data = (object) [];
|
||||
@@ -229,7 +241,6 @@ class Controller {
|
||||
/**
|
||||
* Helper function to return all headers because
|
||||
* getallheaders() is not available under nginx
|
||||
*
|
||||
* @return array (string $key -> string $value)
|
||||
*/
|
||||
static function getRequestHeaders(){
|
||||
@@ -261,7 +272,7 @@ class Controller {
|
||||
/**
|
||||
* get some server information
|
||||
* @param int $ttl cache time (default: 1h)
|
||||
* @return object
|
||||
* @return \stdClass
|
||||
*/
|
||||
static function getServerData($ttl = 3600){
|
||||
$f3 = \Base::instance();
|
||||
@@ -308,25 +319,21 @@ class Controller {
|
||||
*/
|
||||
static function isIGB(){
|
||||
$isIGB = false;
|
||||
|
||||
$igbHeaderData = self::getIGBHeaderData();
|
||||
|
||||
if(count($igbHeaderData->values) > 0){
|
||||
$isIGB = true;
|
||||
}
|
||||
|
||||
return $isIGB;
|
||||
}
|
||||
|
||||
/**
|
||||
* get error object is a user is not found/logged of
|
||||
* @return object
|
||||
* @return \stdClass
|
||||
*/
|
||||
protected function getUserLoggedOffError(){
|
||||
protected function getLogoutError(){
|
||||
$userError = (object) [];
|
||||
$userError->type = 'error';
|
||||
$userError->message = 'User not found';
|
||||
|
||||
return $userError;
|
||||
}
|
||||
|
||||
@@ -341,8 +348,8 @@ class Controller {
|
||||
|
||||
/**
|
||||
* get a log controller e.g. "debug"
|
||||
* @param $loggerType
|
||||
* @return mixed
|
||||
* @param string $loggerType
|
||||
* @return \Log
|
||||
*/
|
||||
static function getLogger($loggerType){
|
||||
return LogController::getLogger($loggerType);
|
||||
@@ -351,7 +358,7 @@ class Controller {
|
||||
/**
|
||||
* removes illegal characters from a Hive-key that are not allowed
|
||||
* @param $key
|
||||
* @return mixed
|
||||
* @return string
|
||||
*/
|
||||
static function formatHiveKey($key){
|
||||
$illegalCharacters = ['-', ' '];
|
||||
@@ -360,8 +367,8 @@ class Controller {
|
||||
|
||||
/**
|
||||
* get environment specific configuration data
|
||||
* @param $key
|
||||
* @return mixed|null
|
||||
* @param string $key
|
||||
* @return string|null
|
||||
*/
|
||||
static function getEnvironmentData($key){
|
||||
$f3 = \Base::instance();
|
||||
@@ -378,7 +385,7 @@ class Controller {
|
||||
/**
|
||||
* get current server environment status
|
||||
* -> "DEVELOP" or "PRODUCTION"
|
||||
* @return mixed
|
||||
* @return string
|
||||
*/
|
||||
static function getEnvironment(){
|
||||
$f3 = \Base::instance();
|
||||
@@ -396,7 +403,7 @@ class Controller {
|
||||
/**
|
||||
* get required MySQL variable value
|
||||
* @param $key
|
||||
* @return mixed|null
|
||||
* @return string|null
|
||||
*/
|
||||
static function getRequiredMySqlVariables($key){
|
||||
$f3 = \Base::instance();
|
||||
@@ -413,7 +420,7 @@ class Controller {
|
||||
* get a program URL by alias
|
||||
* -> if no $alias given -> get "default" route (index.php)
|
||||
* @param null $alias
|
||||
* @return bool
|
||||
* @return bool|string
|
||||
*/
|
||||
protected function getRouteUrl($alias = null){
|
||||
$url = false;
|
||||
@@ -452,9 +459,9 @@ class Controller {
|
||||
* onError() callback function
|
||||
* -> on AJAX request -> return JSON with error information
|
||||
* -> on HTTP request -> render error page
|
||||
* @param $f3
|
||||
* @param \Base $f3
|
||||
*/
|
||||
public function showError($f3){
|
||||
public function showError(\Base $f3){
|
||||
// set HTTP status
|
||||
$errorCode = $f3->get('ERROR.code');
|
||||
if(!empty($errorCode)){
|
||||
@@ -510,8 +517,10 @@ class Controller {
|
||||
/**
|
||||
* Callback for framework "unload"
|
||||
* check -> config.ini
|
||||
* @param \Base $f3
|
||||
* @return bool
|
||||
*/
|
||||
public function unload($f3){
|
||||
public function unload(\Base $f3){
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user