Merge pull request #50 from ThomasRubini/session_module
This commit is contained in:
commit
ae99e8e0c2
@ -14,6 +14,8 @@ final class Constants
|
|||||||
|
|
||||||
const CONTROLLERS_DIR = '/Controllers/';
|
const CONTROLLERS_DIR = '/Controllers/';
|
||||||
|
|
||||||
|
const MODULES_DIR = '/Modules/';
|
||||||
|
|
||||||
|
|
||||||
public static function rootDir() {
|
public static function rootDir() {
|
||||||
return realpath(__DIR__ . '/../');
|
return realpath(__DIR__ . '/../');
|
||||||
@ -39,4 +41,8 @@ final class Constants
|
|||||||
return self::rootDir() . self::CONTROLLERS_DIR;
|
return self::rootDir() . self::CONTROLLERS_DIR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function modulesDir() {
|
||||||
|
return self::rootDir() . self::MODULES_DIR;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
final class UserModel
|
final class UserModel extends UserSessionModel
|
||||||
{
|
{
|
||||||
|
|
||||||
public function createUser($S_email, $S_username, $S_password_hash){
|
public function createUser($S_email, $S_username, $S_password_hash){
|
||||||
@ -58,30 +58,6 @@ final class UserModel
|
|||||||
return $row["USERNAME"];
|
return $row["USERNAME"];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isUserActive($I_id)
|
|
||||||
{
|
|
||||||
$O_model = Model::get();
|
|
||||||
$stmt = $O_model->prepare("SELECT DISABLED FROM USER WHERE ID=:id");
|
|
||||||
$stmt->bindParam("id", $I_id);
|
|
||||||
$stmt->execute();
|
|
||||||
|
|
||||||
$row = $stmt->fetch();
|
|
||||||
if ($row === false) return false;
|
|
||||||
return $row["DISABLED"] !== 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isUserAdmin($I_id)
|
|
||||||
{
|
|
||||||
$O_model = Model::get();
|
|
||||||
$stmt = $O_model->prepare("SELECT ADMIN FROM USER WHERE ID=:id");
|
|
||||||
$stmt->bindParam("id", $I_id);
|
|
||||||
$stmt->execute();
|
|
||||||
|
|
||||||
$row = $stmt->fetch();
|
|
||||||
if ($row === false) return false;
|
|
||||||
return $row["ADMIN"] === 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function updateEmailByID($I_id, $S_newEmail){
|
public function updateEmailByID($I_id, $S_newEmail){
|
||||||
$O_model = Model::get();
|
$O_model = Model::get();
|
||||||
$stmt = $O_model->prepare("UPDATE USER SET EMAIL=:new_email WHERE ID=:id");
|
$stmt = $O_model->prepare("UPDATE USER SET EMAIL=:new_email WHERE ID=:id");
|
||||||
|
28
Modules/AutoLoad.php
Normal file
28
Modules/AutoLoad.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class ModulesAutoLoad
|
||||||
|
{
|
||||||
|
public static function loadModuleClass($S_className)
|
||||||
|
{
|
||||||
|
$dir = Constants::modulesDir();
|
||||||
|
foreach (scandir($dir) as $path) {
|
||||||
|
if($path === ".." || $path === ".") continue;
|
||||||
|
$subdir = "$dir/$path";
|
||||||
|
if (is_dir($subdir)) {
|
||||||
|
static::_load("$subdir/$S_className.php");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function _load($S_path)
|
||||||
|
{
|
||||||
|
if (is_readable($S_path))
|
||||||
|
{
|
||||||
|
require $S_path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
spl_autoload_register('ModulesAutoLoad::loadModuleClass');
|
@ -47,8 +47,8 @@ final class Session
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ensure account has not been deleted/disabled in the meantime
|
// ensure account has not been deleted/disabled in the meantime
|
||||||
$O_userModel = new UserModel();
|
$O_userSessionModel = new UserSessionModel();
|
||||||
$B_userActive = $O_userModel->isUserActive($_SESSION["ID"]);
|
$B_userActive = $O_userSessionModel->isUserActive($_SESSION["ID"]);
|
||||||
return $B_userActive;
|
return $B_userActive;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,8 +68,8 @@ final class Session
|
|||||||
public static function is_admin(){
|
public static function is_admin(){
|
||||||
if (!self::is_login()) return false;
|
if (!self::is_login()) return false;
|
||||||
|
|
||||||
$O_userModel = new UserModel();
|
$O_userSessionModel = new UserSessionModel();
|
||||||
return $O_userModel->isUserAdmin($_SESSION["ID"]);
|
return $O_userSessionModel->isUserAdmin($_SESSION["ID"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function admin_or_die(){
|
public static function admin_or_die(){
|
30
Modules/Session/UserSessionModel.php
Normal file
30
Modules/Session/UserSessionModel.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class UserSessionModel
|
||||||
|
{
|
||||||
|
|
||||||
|
public function isUserActive($I_id)
|
||||||
|
{
|
||||||
|
$O_model = Model::get();
|
||||||
|
$stmt = $O_model->prepare("SELECT DISABLED FROM USER WHERE ID=:id");
|
||||||
|
$stmt->bindParam("id", $I_id);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
$row = $stmt->fetch();
|
||||||
|
if ($row === false) return false;
|
||||||
|
return $row["DISABLED"] !== 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isUserAdmin($I_id)
|
||||||
|
{
|
||||||
|
$O_model = Model::get();
|
||||||
|
$stmt = $O_model->prepare("SELECT ADMIN FROM USER WHERE ID=:id");
|
||||||
|
$stmt->bindParam("id", $I_id);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
$row = $stmt->fetch();
|
||||||
|
if ($row === false) return false;
|
||||||
|
return $row["ADMIN"] === 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
require 'Kernel/AutoLoad.php';
|
require 'Kernel/AutoLoad.php';
|
||||||
|
require 'Modules/AutoLoad.php';
|
||||||
|
|
||||||
$dotenv = Dotenv\Dotenv::createImmutable(Constants::rootDir());
|
$dotenv = Dotenv\Dotenv::createImmutable(Constants::rootDir());
|
||||||
$dotenv->load();
|
$dotenv->load();
|
||||||
|
Loading…
Reference in New Issue
Block a user