From c276e3ff58f266596c286af925804f0c8afd8b8b Mon Sep 17 00:00:00 2001 From: p1907961 Date: Sun, 20 Dec 2020 03:56:05 +0100 Subject: [PATCH] Fixed Model structure and User session --- .gitignore | 3 ++- controller/accomodation.php | 3 +-- controller/index.php | 2 +- controller/login.php | 5 ++-- index.php | 13 +++++++-- models/Accomodation.php | 31 +++++++++++++++++++--- models/AccomodationServices.php | 6 +---- models/User.php | 47 ++++++++++++++++++++++++++++----- src/model.php | 18 ++++++++----- view/template/navbar.php | 21 ++++++++++----- 10 files changed, 113 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 62c8935..0635ab9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea/ \ No newline at end of file +.idea/ +config/config.php \ No newline at end of file diff --git a/controller/accomodation.php b/controller/accomodation.php index 75932cd..60c0165 100644 --- a/controller/accomodation.php +++ b/controller/accomodation.php @@ -1,9 +1,8 @@ isLoggedIn()) { +if($_SESSION['USER']->isLoggedIn()) { if(isset($path[1])) { switch ($path[1]) { case 'add': - $services = (new AccomodationServices())->getAll(); require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php'); break; case 'edit': diff --git a/controller/index.php b/controller/index.php index 50200f2..7aa60d8 100644 --- a/controller/index.php +++ b/controller/index.php @@ -1,5 +1,5 @@ isLoggedIn()) { +if($_SESSION['USER']->isLoggedIn()) { if (!isset($path[1])) { require_once(VIEW_PATH . $path[0] . '.php'); } diff --git a/controller/login.php b/controller/login.php index 2d9fbe2..35b7296 100644 --- a/controller/login.php +++ b/controller/login.php @@ -1,6 +1,6 @@ isLoggedIn() && !isset($path[1])) { +if(!$_SESSION['USER']->isLoggedIn() && !isset($path[1])) { if(isset($_POST['email'],$_POST['password'])){ $user = new User; $userArray = $user->fetch( @@ -13,7 +13,8 @@ if(!$_SESSION['user']->isLoggedIn() && !isset($path[1])) { /* * Successful login */ - $_SESSION['user'] = $userArray[0]; // Pass the returned User type data into Session + + $_SESSION['USER'] = $userArray[0]; // Pass the returned User type data into Session redirect(); }else{ /* diff --git a/index.php b/index.php index c570134..f110a7e 100644 --- a/index.php +++ b/index.php @@ -11,8 +11,17 @@ define('__PATH', str_replace(WEBSITE_PATH, '', $_SERVER['REQUEST_URI'])); * User session */ -if(!isset($_SESSION['user'])){ - $_SESSION['user'] = new User(); +if(!isset($_SESSION['USER'])){ + $_SESSION['USER'] = new User(); +}else{ + /* + * Init DB login + */ + Model::initDatabase(); + /* + * Check if user still exist + */ + $_SESSION['USER']->update(); } /* * Lang diff --git a/models/Accomodation.php b/models/Accomodation.php index a145a24..2805194 100644 --- a/models/Accomodation.php +++ b/models/Accomodation.php @@ -3,9 +3,32 @@ class Accomodation extends Model { - public function getByEmail($email){ - $q = Accomodation::$db->prepare('SELECT * FROM Accomodation WHERE UserEmail = ?'); - $q->execute(array($email)); - return $q->fetch(PDO::FETCH_ASSOC); + private $data; + + public function __construct($data = null) + { + parent::__construct(); + $_col = get_class()::getColumns(); + if($data !== null){ + forEach($data as $key=>$value){ + if(!key_exists($key, $_col)){ + throw new Exception('Invalid data entry'); + }else{ + $this->data[$key] = $value; + } + } + } + return $this; + } + + public static function getByUser(User $user) + { + if($user->getAccomodationId()) { + $data = Accomodation::fetch(array(['id','=', $user->getAccomodationId()])); + if(count($data) === 1){ + return $data[0]; + } + } + return false; } } \ No newline at end of file diff --git a/models/AccomodationServices.php b/models/AccomodationServices.php index 3f68a4b..556636d 100644 --- a/models/AccomodationServices.php +++ b/models/AccomodationServices.php @@ -3,9 +3,5 @@ class AccomodationServices extends Model { - public static function getAll(){ - $q = AccomodationServices::$db->prepare('SELECT * FROM AccomodationServices'); - $q->execute(); - return $q->fetchAll(PDO::FETCH_ASSOC); - } + } \ No newline at end of file diff --git a/models/User.php b/models/User.php index 43cf14f..44562f2 100644 --- a/models/User.php +++ b/models/User.php @@ -23,30 +23,63 @@ class User extends Model /* * Getters */ - public function getEmail(){ + public function getEmail(): string + { if(isset($this->data['email'])) return $this->data['email']; return false; } - public function getFirstName(){ + public function getFirstName(): string + { if(isset($this->data['firstName'])) return $this->data['firstName']; return false; } - public function getLastName(){ + public function getLastName(): string + { if(isset($this->data['lastName'])) return $this->data['lastName']; return false; } - public function getPhoneNumber(){ + public function getPhoneNumber(): string + { if(isset($this->data['phoneNumber'])) return $this->data['phoneNumber']; return false; } - public function isLoggedIn(){ + public function getType(): string + { + if(isset($this->data['UserTypeName'])) + return $this->data['UserTypeName']; + return false; + } + public function getAccomodationId(): string + { + if(isset($this->data['AccomodationId'])) + return $this->data['AccomodationId']; + return false; + } + /* + * Session + */ + public function update(): void + { + if (isset($this->data['email'])){ + $exist = User::fetch(array(['email','=',$this->data['email']])); + if(count($exist) === 1){ + + }else{ + /* + * Account must have been deleted + */ + $_SESSION = array(); + session_destroy(); + } + } + } + public function isLoggedIn(): bool + { return (isset($this->data['email'])); } - - } \ No newline at end of file diff --git a/src/model.php b/src/model.php index 7316964..06a8765 100644 --- a/src/model.php +++ b/src/model.php @@ -2,13 +2,17 @@ class Model { protected static $db = null; - public function __construct(){ + public function __construct() + { + $this->initDatabase(); + } + public static function initDatabase(){ try { /* * Init connection to the DB */ - $this::$db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD); - $this::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + Model::$db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD); + Model::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(Exception $e){ var_dump($e); } @@ -16,14 +20,15 @@ class Model { /* * GET/SELECT Query */ - public static function fetch($filters = []){ + public static function fetch($filters = []): array + { $query = 'SELECT * FROM ' . get_called_class(); $args = []; $c = 0; $output = []; $_col = get_called_class()::getColumns(); forEach($filters as $filter){ - if (count($filter) === 3 && array_key_exists($filter[0], $_col) && in_array($filter[1], ['=','<','>','<>','LIKE'])){ + if (is_array($filter) && count($filter) === 3 && array_key_exists($filter[0], $_col) && in_array($filter[1], ['=','<','>','<>','LIKE'])){ if($c === 0){ $query .= ' WHERE'; }else{ @@ -45,7 +50,8 @@ class Model { } return $output; } - protected function getColumns(){ + protected function getColumns(): array + { $q = Model::$db->prepare('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?'); $q->execute(array(get_called_class())); $d = $q->fetchAll(PDO::FETCH_ASSOC); diff --git a/view/template/navbar.php b/view/template/navbar.php index 596f76f..f01c9b8 100644 --- a/view/template/navbar.php +++ b/view/template/navbar.php @@ -1,3 +1,6 @@ +