1
0
Fork 0

Fixed Model structure and User session

This commit is contained in:
p1907961 2020-12-20 03:56:05 +01:00
parent 117b6a9217
commit c276e3ff58
10 changed files with 113 additions and 36 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
.idea/ .idea/
config/config.php

View file

@ -1,9 +1,8 @@
<?php <?php
if($_SESSION['user']->isLoggedIn()) { if($_SESSION['USER']->isLoggedIn()) {
if(isset($path[1])) { if(isset($path[1])) {
switch ($path[1]) { switch ($path[1]) {
case 'add': case 'add':
$services = (new AccomodationServices())->getAll();
require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php'); require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php');
break; break;
case 'edit': case 'edit':

View file

@ -1,5 +1,5 @@
<?php <?php
if($_SESSION['user']->isLoggedIn()) { if($_SESSION['USER']->isLoggedIn()) {
if (!isset($path[1])) { if (!isset($path[1])) {
require_once(VIEW_PATH . $path[0] . '.php'); require_once(VIEW_PATH . $path[0] . '.php');
} }

View file

@ -1,6 +1,6 @@
<?php <?php
if(!$_SESSION['user']->isLoggedIn() && !isset($path[1])) { if(!$_SESSION['USER']->isLoggedIn() && !isset($path[1])) {
if(isset($_POST['email'],$_POST['password'])){ if(isset($_POST['email'],$_POST['password'])){
$user = new User; $user = new User;
$userArray = $user->fetch( $userArray = $user->fetch(
@ -13,7 +13,8 @@ if(!$_SESSION['user']->isLoggedIn() && !isset($path[1])) {
/* /*
* Successful login * 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(); redirect();
}else{ }else{
/* /*

View file

@ -11,8 +11,17 @@ define('__PATH', str_replace(WEBSITE_PATH, '', $_SERVER['REQUEST_URI']));
* User session * User session
*/ */
if(!isset($_SESSION['user'])){ if(!isset($_SESSION['USER'])){
$_SESSION['user'] = new User(); $_SESSION['USER'] = new User();
}else{
/*
* Init DB login
*/
Model::initDatabase();
/*
* Check if user still exist
*/
$_SESSION['USER']->update();
} }
/* /*
* Lang * Lang

View file

@ -3,9 +3,32 @@
class Accomodation extends Model class Accomodation extends Model
{ {
public function getByEmail($email){ private $data;
$q = Accomodation::$db->prepare('SELECT * FROM Accomodation WHERE UserEmail = ?');
$q->execute(array($email)); public function __construct($data = null)
return $q->fetch(PDO::FETCH_ASSOC); {
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;
} }
} }

View file

@ -3,9 +3,5 @@
class AccomodationServices extends Model class AccomodationServices extends Model
{ {
public static function getAll(){
$q = AccomodationServices::$db->prepare('SELECT * FROM AccomodationServices');
$q->execute();
return $q->fetchAll(PDO::FETCH_ASSOC);
}
} }

View file

@ -23,30 +23,63 @@ class User extends Model
/* /*
* Getters * Getters
*/ */
public function getEmail(){ public function getEmail(): string
{
if(isset($this->data['email'])) if(isset($this->data['email']))
return $this->data['email']; return $this->data['email'];
return false; return false;
} }
public function getFirstName(){ public function getFirstName(): string
{
if(isset($this->data['firstName'])) if(isset($this->data['firstName']))
return $this->data['firstName']; return $this->data['firstName'];
return false; return false;
} }
public function getLastName(){ public function getLastName(): string
{
if(isset($this->data['lastName'])) if(isset($this->data['lastName']))
return $this->data['lastName']; return $this->data['lastName'];
return false; return false;
} }
public function getPhoneNumber(){ public function getPhoneNumber(): string
{
if(isset($this->data['phoneNumber'])) if(isset($this->data['phoneNumber']))
return $this->data['phoneNumber']; return $this->data['phoneNumber'];
return false; 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'])); return (isset($this->data['email']));
} }
} }

View file

@ -2,13 +2,17 @@
class Model { class Model {
protected static $db = null; protected static $db = null;
public function __construct(){ public function __construct()
{
$this->initDatabase();
}
public static function initDatabase(){
try { try {
/* /*
* Init connection to the DB * Init connection to the DB
*/ */
$this::$db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD); Model::$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->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(Exception $e){ } catch(Exception $e){
var_dump($e); var_dump($e);
} }
@ -16,14 +20,15 @@ class Model {
/* /*
* GET/SELECT Query * GET/SELECT Query
*/ */
public static function fetch($filters = []){ public static function fetch($filters = []): array
{
$query = 'SELECT * FROM ' . get_called_class(); $query = 'SELECT * FROM ' . get_called_class();
$args = []; $args = [];
$c = 0; $c = 0;
$output = []; $output = [];
$_col = get_called_class()::getColumns(); $_col = get_called_class()::getColumns();
forEach($filters as $filter){ 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){ if($c === 0){
$query .= ' WHERE'; $query .= ' WHERE';
}else{ }else{
@ -45,7 +50,8 @@ class Model {
} }
return $output; return $output;
} }
protected function getColumns(){ protected function getColumns(): array
{
$q = Model::$db->prepare('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?'); $q = Model::$db->prepare('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?');
$q->execute(array(get_called_class())); $q->execute(array(get_called_class()));
$d = $q->fetchAll(PDO::FETCH_ASSOC); $d = $q->fetchAll(PDO::FETCH_ASSOC);

View file

@ -1,3 +1,6 @@
<?php
?>
<nav class="navbar navbar-expand-lg navbar-light bg-light"> <nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid"> <div class="container-fluid">
<a class="navbar-brand" href="#"><?= WEBSITE_NAME; ?></a> <a class="navbar-brand" href="#"><?= WEBSITE_NAME; ?></a>
@ -8,10 +11,16 @@
<div class="collapse navbar-collapse" id="navbarText"> <div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav me-auto mb-2 mb-lg-0"> <ul class="navbar-nav me-auto mb-2 mb-lg-0">
<?php <?php
if($_SESSION['user']->isLoggedIn()){ if($_SESSION['USER']->isLoggedIn()){
echo navItem('Ajout Hebergement',genURL('accomodation/add')); switch($_SESSION['USER']->getType()){
echo navItem('Edition de l\'hebergement',genURL('accomodation/edit')); case 'AccomodationOwner':
echo navItem('Gestion des disponibilités',genURL('accomodation/manager')); echo navItem('Ajout Hebergement',genURL('accomodation/add'));
echo navItem('Edition de l\'hebergement',genURL('accomodation/edit'));
break;
case 'Staff':
echo navItem('Gestion des disponibilités',genURL('accomodation/manager'));
break;
}
}else{ }else{
echo navItem('Login',genURL('login')); echo navItem('Login',genURL('login'));
} }
@ -19,9 +28,9 @@
</ul> </ul>
<span class="navbar-text"> <span class="navbar-text">
<?php <?php
if($_SESSION['user']->isLoggedIn()){ if($_SESSION['USER']->isLoggedIn()){
?> ?>
M. <?=htmlspecialchars(strtoupper($_SESSION['user']->getLastName()))?> <?=htmlspecialchars($_SESSION['user']->getFirstName())?> M. <?=htmlspecialchars(strtoupper($_SESSION['USER']->getLastName()))?> <?=htmlspecialchars($_SESSION['USER']->getFirstName())?>
<a href="<?=genURL('logout')?>">Déconnexion</a> <a href="<?=genURL('logout')?>">Déconnexion</a>
<?php <?php
} }