Fixed Model structure and User session
This commit is contained in:
parent
117b6a9217
commit
c276e3ff58
10 changed files with 113 additions and 36 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
.idea/
|
||||
.idea/
|
||||
config/config.php
|
|
@ -1,9 +1,8 @@
|
|||
<?php
|
||||
if($_SESSION['user']->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':
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
if($_SESSION['user']->isLoggedIn()) {
|
||||
if($_SESSION['USER']->isLoggedIn()) {
|
||||
if (!isset($path[1])) {
|
||||
require_once(VIEW_PATH . $path[0] . '.php');
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
if(!$_SESSION['user']->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{
|
||||
/*
|
||||
|
|
13
index.php
13
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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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']));
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
<?php
|
||||
|
||||
?>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#"><?= WEBSITE_NAME; ?></a>
|
||||
|
@ -8,10 +11,16 @@
|
|||
<div class="collapse navbar-collapse" id="navbarText">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<?php
|
||||
if($_SESSION['user']->isLoggedIn()){
|
||||
echo navItem('Ajout Hebergement',genURL('accomodation/add'));
|
||||
echo navItem('Edition de l\'hebergement',genURL('accomodation/edit'));
|
||||
echo navItem('Gestion des disponibilités',genURL('accomodation/manager'));
|
||||
if($_SESSION['USER']->isLoggedIn()){
|
||||
switch($_SESSION['USER']->getType()){
|
||||
case 'AccomodationOwner':
|
||||
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{
|
||||
echo navItem('Login',genURL('login'));
|
||||
}
|
||||
|
@ -19,9 +28,9 @@
|
|||
</ul>
|
||||
<span class="navbar-text">
|
||||
<?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>
|
||||
<?php
|
||||
}
|
||||
|
|
Reference in a new issue