Merge pull request #50 from ThomasRubini/session_module

This commit is contained in:
Thomas Rubini 2023-01-22 22:30:22 +01:00 committed by GitHub
commit ae99e8e0c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 29 deletions

View File

@ -14,6 +14,8 @@ final class Constants
const CONTROLLERS_DIR = '/Controllers/';
const MODULES_DIR = '/Modules/';
public static function rootDir() {
return realpath(__DIR__ . '/../');
@ -39,4 +41,8 @@ final class Constants
return self::rootDir() . self::CONTROLLERS_DIR;
}
public static function modulesDir() {
return self::rootDir() . self::MODULES_DIR;
}
}

View File

@ -1,6 +1,6 @@
<?php
final class UserModel
final class UserModel extends UserSessionModel
{
public function createUser($S_email, $S_username, $S_password_hash){
@ -57,30 +57,6 @@ final class UserModel
if ($row === false) return null;
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){
$O_model = Model::get();

28
Modules/AutoLoad.php Normal file
View 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');

View File

@ -47,8 +47,8 @@ final class Session
}
// ensure account has not been deleted/disabled in the meantime
$O_userModel = new UserModel();
$B_userActive = $O_userModel->isUserActive($_SESSION["ID"]);
$O_userSessionModel = new UserSessionModel();
$B_userActive = $O_userSessionModel->isUserActive($_SESSION["ID"]);
return $B_userActive;
}
@ -68,8 +68,8 @@ final class Session
public static function is_admin(){
if (!self::is_login()) return false;
$O_userModel = new UserModel();
return $O_userModel->isUserAdmin($_SESSION["ID"]);
$O_userSessionModel = new UserSessionModel();
return $O_userSessionModel->isUserAdmin($_SESSION["ID"]);
}
public static function admin_or_die(){

View 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;
}
}

View File

@ -3,6 +3,7 @@
require 'vendor/autoload.php';
require 'Kernel/AutoLoad.php';
require 'Modules/AutoLoad.php';
$dotenv = Dotenv\Dotenv::createImmutable(Constants::rootDir());
$dotenv->load();