From 2b6970d33afd75be5bfef951dc3691d492004d43 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Tue, 18 Apr 2023 01:11:37 +0300 Subject: admin back-office environment (skeleton); admin categories --- public/app/controllers/Admin.php | 58 ++++++++++++++++++++++++++++++++++ public/app/controllers/Auth.php | 68 ++++++++++++++++++++-------------------- 2 files changed, 92 insertions(+), 34 deletions(-) create mode 100644 public/app/controllers/Admin.php (limited to 'public/app/controllers') diff --git a/public/app/controllers/Admin.php b/public/app/controllers/Admin.php new file mode 100644 index 0000000..6f6858c --- /dev/null +++ b/public/app/controllers/Admin.php @@ -0,0 +1,58 @@ +setID($record['id']) ->setUserName($record['email']) + ->setName($record['first_name'] .' '. $record['last_name']) ->setPassword($record['password']) ->setEnabled($record['active']); @@ -67,7 +68,7 @@ class Auth { // set cookie for connected user setcookie( 'cluser', - 'connected', + 'connected;'. $user->getName(), time()+60*60*8, // 8 hours '/' ); @@ -138,6 +139,7 @@ class Auth { $user = (new Classroom_user()) ->setUserName($req->POST['email']) + ->setName($req->POST['name'] .' '. $req->POST['surname']) ->setPassword($password) ->setRoles([ READER ]) // Role: authorized reader ->setPrivileges([]); // none privilege until acount confirmation @@ -197,19 +199,19 @@ class Auth { } - - - public static function logout() { - $userManager = new UserManager(); + $userManager = new Classroom_manager(); $userManager->logout(); + // regeneration session ID (prevent session fixation) + session_regenerate_id(); + // remove user-conected cookie if (isset($_COOKIE['cluser'])) { unset($_COOKIE['cluser']); - setcookie('cluser', null, -1, '/'); + setcookie('cluser', '', -1, '/'); return true; } else { return false; @@ -218,34 +220,6 @@ class Auth { - /** isGranted( ROLE ) - * - * checks if the user is granted (some of) the specified role(s) - * to access the source - * - * NOTE: - * if no roles are specified then user is granted - * (because every user is granted the 'no-role') - * - * @param $roles (array): array of roles to check (if any is granted) - * - */ - public static function isGranted($roles = []) - { - // no role required ? user is granted access - if ($roles == []) return true; - - // else, UserManager knows if user isGranted - $userManager = new UserManager(); - if ($userManager->isGranted($roles)) { - return true; - - } else { - return false; - } - } - - /** hasPermition( PERMIT ) * * checks if the user owns the specified permition @@ -322,6 +296,32 @@ class Auth { } + /** allowRoles + * + * method filters access for certain roles + * if user is not grented acces, a forbiden message is sent and app ends._ + * otherwise the method returns true (app will continue) + * + * @param $allowed (array) : array of allowed roles + * @return true or die(); + */ + public static function allowRoles($allowed) + { + $manager = new Classroom_manager(); + if ($manager->isGranted($allowed)) { // if valid, return true (continue) + return true; + + } else { // no user, no access; die._ + Render::view('error/general', [ + 'title' => 'Forbidden', + 'message' => 'Access is forbidden' + ]); + die(); + return false; // this line will never run + } + } + + } -- cgit v1.2.3