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/config/init_routes.php | 12 ++ public/app/controllers/Admin.php | 58 +++++++ public/app/controllers/Auth.php | 68 ++++---- public/app/extends/Classroom_user.php | 35 +++- public/app/models/cms/Course_model.php | 219 ++++++++++++++++++++++++ public/app/routes/backend.php | 44 ++++- public/app/routes/frontend.php | 60 +------ public/app/routes/user.php | 84 +++++++++ public/app/views/admin/admin-menu.php | 47 +++++ public/app/views/admin/categories.php | 97 +++++++++++ public/app/views/admin/skeleton.php | 40 ++--- public/app/views/components/header_includes.php | 33 ++++ public/app/views/components/user-management.php | 35 +++- public/app/views/error/404.php | 2 +- public/app/views/error/general.php | 2 +- public/app/views/welcome.php | 15 +- 16 files changed, 724 insertions(+), 127 deletions(-) create mode 100644 public/app/config/init_routes.php create mode 100644 public/app/controllers/Admin.php create mode 100644 public/app/models/cms/Course_model.php create mode 100644 public/app/routes/user.php create mode 100644 public/app/views/admin/admin-menu.php create mode 100644 public/app/views/admin/categories.php (limited to 'public/app') diff --git a/public/app/config/init_routes.php b/public/app/config/init_routes.php new file mode 100644 index 0000000..22d6ec0 --- /dev/null +++ b/public/app/config/init_routes.php @@ -0,0 +1,12 @@ +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 + } + } + + } diff --git a/public/app/extends/Classroom_user.php b/public/app/extends/Classroom_user.php index 1c26e73..c27c4bf 100644 --- a/public/app/extends/Classroom_user.php +++ b/public/app/extends/Classroom_user.php @@ -34,12 +34,19 @@ class Classroom_user extends User */ private $privileges = []; + /** user name + * @var string + */ + private $name; /** SETTERS AND GETTERS - * for id and privileges attributes + * for id, name, privileges attributes * ------------------------------------------------------------------------- */ + // id + // --- -- -- - - - + /** getID * @return int */ @@ -59,8 +66,34 @@ class Classroom_user extends User $this->id = $id; return $this; } + + + // name + // --- -- -- - - - + + /** getName + * @return int + */ + public function getName(): string + { + return $this->name; + } + + /** setName() + * + * @param string : user's full name + * + * @return User + */ + public function setName(string $name): self + { + $this->name = $name; + return $this; + } + // privileges + // --- -- -- - - - /** getPrivileges * diff --git a/public/app/models/cms/Course_model.php b/public/app/models/cms/Course_model.php new file mode 100644 index 0000000..4fb49c0 --- /dev/null +++ b/public/app/models/cms/Course_model.php @@ -0,0 +1,219 @@ +runQuery( + "SELECT * FROM course", + [] + ); + } + + + /** constuct a category_tree + * + * returns a tree representation of the categories + * + * NOTE: + * category_tree() is an expensive method; + * it calls 2 other methods implementing recursive algorithms + * thus it uses many sources to run (particularly RAM). + * Caching the result is strogly recommended. + * + */ + public static function category_tree() + { + $categories = self::get_categories(); // get all categories + + $tree = self::to_tree($categories); // format to a tree + + $tree_wParents = self::tree_parents($tree); // add parents section for each tree-node + + return $tree_wParents; + } + + + /** to_tree + * + * constructs a tree from raw-table data; + * this is a private method and uses a recursive algorithm + * + * @param $dataset (array): flar array of records with id/parent-id pairs + * @return $root (array): id of root category + * + * (**) each node has 2 parts: + * .... .. rec : all record attributes/data as passed into $dataset + * .... .. childs : array of (children) nodes + */ + private static function to_tree($dataset, $root = 0) + { + $return = []; + + // loop data ; search for direct children of root + foreach($dataset as $key => $rec) { + + $child = $rec['id']; + $parent = $rec['parent_id']; + + if ($parent == $root) { // a direct child is found + + unset($dataset[$key]); // remove item (no need to traverse again) + + // Append the child into result array ; parse its children + $return[] = [ + 'rec' => [ + 'id' => $rec['id'], + 'label' => $rec['label'], + 'parent' => $rec['parent_id'] + ], + 'childs' => self::to_tree($dataset, $child) // recursively + ]; + } + } + return empty($return) ? [] : $return; + } + + + /** tree_parents + * + * adds a section to each tree node with all parents of each node + * + * @param $tree (array) : nodes array (each node has `rec` and `childs` sections ) + * @param $parents (array); DO NOT SET IT (takes values automaticaly) + * @return array of nodes with an extra node[parents] section + * + */ + private static function tree_parents($tree, $parents = []) + { + $tree_with_parents = []; + + foreach($tree as $key => $node) { + // parents to be pushed for node's children + $push_parents = $parents; // parents so far + $push_parents[] = $node['rec']; // this record will be a new parent + + $tree_with_parents[$key] = [ + 'rec' => $node['rec'], + 'parents' => $parents, + 'childs' => ($node['childs'] == []) + ? [] + : self::tree_parents($node['childs'], $push_parents) + ]; + } + + return $tree_with_parents; + } + + + /** all_breadcrumbs + * ------------------------------------------------------------------------- + * + * returns an array of all breadcrumbs + * where array-key of each record is category[id] + * + * NOTE: + * --- + * Course_model::all_breadcrumbs returns an indexed super-array; + * each array item includes a banch of information: [ + * breadcrumb, + * rec: [ id , title ], + * parents: [ [id, title] , ... ] + * childs: [ [id, title] , ... ], + * level + * ] + * + * Use Cases: + * --- + * as a super-array, the output can be used in many cases + * for example... + * into form elements + * .. while selecting category for a post + * .. or editing a category + * or directry referring to category's parents/childs + * + * Arguments: + * --- + * @param $tree (array) : category tree (with childs and parents parts) + * @param $detimiter (string, optional) : string to split breadcrumb's path-nodes + * @param $exception (int, optional) : id of category to exclude (subcategories shall be excluded too) + * @param $l (int, not-pass) : depth level of the node; DO NOT SET (takes values automaticaly) + * @return array of breadcrumbs + * ------------------------------------------------------------------------- + */ + static public function breadcrumbs($tree, $delimiter = " / ", $exception = 0, $l = 0) + { + $all = []; // results array + + foreach($tree as $node) { // loop through all nodes + + if (intval($node['rec']['id']) != $exception) { // if node is not exception + + // construct breadcrumb html of node + // --- -- -- - - - + $breadcrumb = ""; + foreach($node['parents'] as $par) { // first: join path titles + $breadcrumb .= $par['label'] . $delimiter; + } + $breadcrumb .= $node['rec']['label']; // last: append title + + // make a new super record + // --- -- -- - - - + $all[$node['rec']['id']] = [ // set record is as key + 'breadcrumb' => $breadcrumb, // add breadcrump to results + 'rec' => $node['rec'], // + node info + 'parents' => $node['parents'], // + parents array + 'childs' => self::first_level_childs($node), // + direct childs + 'level' => $l // + level + ]; + + // recursively traverse children nodes + // --- -- -- - - - + if (isset($node['childs']) && $node['childs'] != []) { + $child_breadcrumbs = self::breadcrumbs( + $node['childs'], + $delimiter, + $exception, + $l+1 + ); + + $all = $all + $child_breadcrumbs; // concatenate arrays (keep array-keys) + } + } + + } + return $all; + + } + + /** first_level_childs + * --- -- -- - - - + * used by all_breadcrumbs() + */ + static private function first_level_childs($node) + { + $childs = []; + if ($node['childs'] == []) { + return []; + } + foreach($node['childs'] as $key => $kid) { + $childs[] = [ + 'id' => $kid['rec']['id'], + 'label' => $kid['rec']['label'] + ]; + } + return $childs; + } + + + +} diff --git a/public/app/routes/backend.php b/public/app/routes/backend.php index 9b90cf1..700fa14 100644 --- a/public/app/routes/backend.php +++ b/public/app/routes/backend.php @@ -1,9 +1,41 @@ 'Διαχείριση Μαθημάτων', + 'action' => 'lessons', + ]); +}); + + +// admin cateogies +// --- +Route::add('/admin/categories', function () { + Auth::allowRoles([1, 2, 3]); + Render::view('admin/skeleton', [ + 'title' => 'Διαχείριση Κατηγοριών', + 'action' => 'categories', + ]); +}); + + +Route::add('/admin/api/categories', function () { + Auth::allowRoles([1, 2, 3]); + $tree = proxy( + [\app\models\cms\Course_model::class, 'category_tree'], + [], CACHE_ROOT_TTL + ); + + $breadcrumbs = proxy( + [\app\models\cms\Course_model::class, 'breadcrumbs'], + [$tree], CACHE_ROOT_TTL + ); + // $breadcrumbs = Course_model::breadcrumbs($tree); + Render::json($breadcrumbs); +}); \ No newline at end of file diff --git a/public/app/routes/frontend.php b/public/app/routes/frontend.php index 56db23a..dde0fcd 100644 --- a/public/app/routes/frontend.php +++ b/public/app/routes/frontend.php @@ -1,14 +1,18 @@ proxy( + [\app\models\cms\Course_model::class, 'category_tree'], + [], CACHE_ROOT_TTL + ) + ]); // header('Location: /some/default/url', true, 302); }); @@ -38,56 +42,6 @@ Route::notFound( function() { // [..] order -Route::add('/login', function() { Render::view('user/login'); }); -Route::add('/registration', function() { Render::view('user/registration'); }); - - -Route::add('/account/check-login', function() { - $response = Auth::login(); - Render::json(['status' => $response]); - }, - 'post' -); - -// user sends a registration form; -Route::add('/account/register', function() { - $response = Auth::register(); - Render::json($response); - }, - 'post' -); - -// user requests activation -Route::add('/account/activate', function() { Auth::activate(); } ); - -// user profile -Route::add('/account/profile', function() { - $user = Auth::is_connected(); - if ($user === false) { - Render::view('error/general', - [ - 'title' => 'Nope!', - 'message' => "

No profile

User is not connected" - ] - ); - - } else { - print_r($user); - } - } -); - -// test connection -// NOTE: this is only for testing purposes -if (!PRODUCTION) { - Route::add('/check/connection', function() { Auth::is_connected(); }); -} - -Route::add('/account/reset_password', function() { Auth::reset_password(); } ); - - - - // Resolve urls // --- -- -- - - - diff --git a/public/app/routes/user.php b/public/app/routes/user.php new file mode 100644 index 0000000..d419a5b --- /dev/null +++ b/public/app/routes/user.php @@ -0,0 +1,84 @@ + sends to POST:/account/check-login +Route::add('/login', function() { Render::view('user/login'); }); + +// request registration ... -> sends to POST:/account/register +Route::add('/registration', function() { Render::view('user/registration'); }); + +// request logout +Route::add('/logout', function() { + Auth::logout(); + header('Location: /'); die(); +}); + +// request activation +Route::add('/account/activate', function() { Auth::activate(); } ); + +// request password reset +Route::add('/account/reset_password', function() { Auth::reset_password(); } ); + + + +// Replies to common requests (POST method) +// --- -- -- - - - + +// user sends login form +Route::add('/account/check-login', function() { + $response = Auth::login(); + Render::json(['status' => $response]); + }, + 'post' +); + +// user sends a registration form +Route::add('/account/register', function() { + $response = Auth::register(); + Render::json($response); + }, + 'post' +); + + +// Account Management +// --- -- -- - - - + +// user profile +Route::add('/account/profile', function() { + $user = Auth::is_connected(); + if ($user === false) { + Render::view('error/general', + [ + 'title' => 'Nope!', + 'message' => "

No profile

User is not connected" + ] + ); + + } else { + print_r($user); + } + } +); + + +// temporary (on development) +// --- -- -- - -- +// NOTE: these is only for testing purposes + +// test connection +if (!PRODUCTION) { + Route::add('/check/connection', function() { Auth::is_connected(); }); +} + + + diff --git a/public/app/views/admin/admin-menu.php b/public/app/views/admin/admin-menu.php new file mode 100644 index 0000000..813420d --- /dev/null +++ b/public/app/views/admin/admin-menu.php @@ -0,0 +1,47 @@ + 'posts', + * ]) + * ----------------------------------------------------------------------------- + */ + + +$admin_options = [ + 'new' => 'Νέο Μάθημα', + 'lessons' => 'Μαθήματα', + 'categories' => 'Κατηγορίες', + 'pages' => 'Σελίδες', + 'privileges' => 'Δικαιώματα', + 'users' => 'Χρήστες' +]; + +?> + +
+ + Admin + + $label) : ?> + + + + + + + + + + + + Logout + +
\ No newline at end of file diff --git a/public/app/views/admin/categories.php b/public/app/views/admin/categories.php new file mode 100644 index 0000000..e33d9b0 --- /dev/null +++ b/public/app/views/admin/categories.php @@ -0,0 +1,97 @@ + +
+
Διαχείριση Κατηγοριών
+
+ + +
+
+ +
+
+
+ +
+ + + + + + +
Κατηγορία
+ +
+ + +
+
+
+ + + + + + diff --git a/public/app/views/admin/skeleton.php b/public/app/views/admin/skeleton.php index a9c2dcf..fea346f 100644 --- a/public/app/views/admin/skeleton.php +++ b/public/app/views/admin/skeleton.php @@ -3,35 +3,31 @@ - ΣΚΛΑΒΕΝΙΤΗΣ - <?=$page['title']?> + Classroom - <?=$title?> - - - - - - - + true]); + ?> -
- - - -
- -
- -
- -
- +
+ + $action + ]) + ?> + +
+ + diff --git a/public/app/views/components/header_includes.php b/public/app/views/components/header_includes.php index 4ee6904..e5e51cd 100644 --- a/public/app/views/components/header_includes.php +++ b/public/app/views/components/header_includes.php @@ -1,3 +1,8 @@ + @@ -10,14 +15,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/public/app/views/components/user-management.php b/public/app/views/components/user-management.php index 02a439e..9f95af8 100644 --- a/public/app/views/components/user-management.php +++ b/public/app/views/components/user-management.php @@ -18,12 +18,13 @@