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 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
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
|
||||
$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(){
|
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user