From 65d07ba64d45c77d2db16272710ec5702b34d641 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Tue, 25 Apr 2023 03:40:09 +0300 Subject: front-end: putting everything together --- public/app/controllers/Auth.php | 48 +++++++---- public/app/controllers/Product.php | 52 ------------ public/app/controllers/Resolve.php | 91 -------------------- public/app/controllers/admin/Course_admin.php | 33 ++------ public/app/controllers/cms/Course.php | 116 +++++++++++++++++++++++++- 5 files changed, 153 insertions(+), 187 deletions(-) delete mode 100644 public/app/controllers/Product.php delete mode 100644 public/app/controllers/Resolve.php (limited to 'public/app/controllers') diff --git a/public/app/controllers/Auth.php b/public/app/controllers/Auth.php index b00843c..0570a5e 100644 --- a/public/app/controllers/Auth.php +++ b/public/app/controllers/Auth.php @@ -186,7 +186,7 @@ class Auth { $manager = new Classroom_manager(); if ($manager->hasUserToken()) { - echo 'user is connected'; + // user is connected; $token = $manager->getUserToken(); $user = $token->getUser(); @@ -226,9 +226,19 @@ class Auth { * to access the source * */ - public static function hasPermition($permit = []) + public static function hasPermition($permit = [0]) { - if ($permit == []) return true; + if (in_array(0, $permit)) { // permision 0 means public + return true; // permision 0 is always granted + } + + if ($user = $this->is_connected() === flase) { // if not connected + return false; // then no other permition is granted + } + + if ($user instanceof UserInterface) { + return ( !empty( array_intersect($permit, $user->getPrivileges()) ) ); + } } @@ -243,11 +253,11 @@ class Auth { * example: * [ * [ - * role => ['editor','designer'] + * role => [2, 3] * permition => ['10', '12', '18'] * ], * [ - * role => ['admin' , 'developερ'] + * role => [1 , 4] * ], * [ * permition => [ 3 ] @@ -256,12 +266,12 @@ class Auth { * * defines (and parses to) a requirements rule of: * [ - * user should be editor or designer - * and have permition 10 or 12 or 18 + * user should be creator or editor + * _AND_ have permition 10 or 12 or 18 * ] * OR * [ - * user should be an administratoe or developer + * user should be an administrator or developer * ] * OR * [ @@ -274,17 +284,25 @@ class Auth { { $authorized = false; foreach($requirements as $required) { - if ( (self::isGranted($required['role'] ?? [])) - && (self::hasPermition($required['permition'] ?? [])) ) { - $authorized = true; + + if (isset($required['role'])) { // if a role is required + if ( (self::isGranted($required['role'])) // authorize both role + && (self::hasPermition($required['permition'] ?? [ 0 ])) ) { // and permition + // $authorized = true; + return true; + } + + } else { // else, if not is not required + if (self::hasPermition($required['permition'] ?? [ 0 ])) { // authorize permition + // $authorized = true; + return true; + } } } return $authorized; } - - public static function forgot_pass() { } @@ -322,10 +340,10 @@ class Auth { } - public static function is_admin() + public static function in_admin_group() { $manager = new Classroom_manager(); - return ($manager->isGranted([1,2,3])); + return ($manager->isGranted([1, 2, 3])); } diff --git a/public/app/controllers/Product.php b/public/app/controllers/Product.php deleted file mode 100644 index 0193ed9..0000000 --- a/public/app/controllers/Product.php +++ /dev/null @@ -1,52 +0,0 @@ - 'Of all the things I\'ve lost, I miss my mind the most.']); - return ; - } - - // render page - load_template('products', [ 'data' => [ - 'title' => $product['Title'], - 'seo' => [], - 'tree' => proxy( - [\app\models\market\ProductCategories_model::class, 'tree'], - [], CACHE_ROOT_TTL - ), - 'hierarchyPath' => explode('.', substr($product['Hierarchy'], 1, -1)), - 'sections' => [ - [ - 'view' => 'content/product_list/Product_List_Filters', - 'key' => 'filters', - 'data' => [] - ], - [ - 'view' => 'content/product/product_details', - 'key' => 'product', - 'data' => $product - ] - ] - ] - ]); - - } - - -} diff --git a/public/app/controllers/Resolve.php b/public/app/controllers/Resolve.php deleted file mode 100644 index 7b9222c..0000000 --- a/public/app/controllers/Resolve.php +++ /dev/null @@ -1,91 +0,0 @@ - [ - 'title' => $page['Title'], - 'seo' => [], - 'sections' => [ - [ - 'view' => 'content/static', - 'key' => 'page', - 'data' => $page - ] - ] - ] - ]); - return ; - } - - - if (!PRODUCTION) Benchmark::add_spot('proxy-category'); - - // proxying check if products category with this url - $category = proxy( - [\app\models\market\ProductCategories_model::class, 'categoryFromUrl'], - [ $fullUrl ], CACHE_CATEGORY_TTL - ); - // if category exist, construct category - if ($category !== false) { - // render page - load_template('products', [ 'data' => [ - 'title' => $category['Title'], - 'seo' => [], - 'tree' => proxy( - [\app\models\market\ProductCategories_model::class, 'tree'], - [], CACHE_ROOT_TTL - ), - 'hierarchyPath' => explode('.', substr($category['Hierarchy'], 1, -1)), - 'sections' => [ - [ - 'view' => 'content/product_list/Product_List_Filters', - 'key' => 'filters', - 'data' => $category - ], - [ - 'view' => 'content/product_list/product_list', - 'key' => 'products', - 'data' => $category['products'] - ] - ] - ] - ]); - return ; - } - - - // not page nor category?: reply 404 - header("HTTP/1.0 404 Not Found"); - render_view('error/404', ['message' => 'Say something I\'m givin\' up on you.']); - - } - -} \ No newline at end of file diff --git a/public/app/controllers/admin/Course_admin.php b/public/app/controllers/admin/Course_admin.php index bb4deb0..c2e06f2 100644 --- a/public/app/controllers/admin/Course_admin.php +++ b/public/app/controllers/admin/Course_admin.php @@ -5,6 +5,7 @@ namespace app\controllers\admin; use Registry; use Render; use app\models\cms\Course_model; +use app\extends\Cache_service; class Course_admin { @@ -22,15 +23,7 @@ class Course_admin { */ public static function breadcrumbs() { - $tree = proxy( // cache-get categories - [\app\models\cms\Course_model::class, 'category_tree'], - [], CACHE_ROOT_TTL - ); - $breadcrumbs = proxy( // cache-get breadcrumbs - [\app\models\cms\Course_model::class, 'breadcrumbs'], - [$tree], CACHE_ROOT_TTL - ); - Render::json($breadcrumbs); // send as json + Render::json( Cache_service::courses_struct()['breadcrumbs'] ); } /** add course @@ -75,26 +68,15 @@ class Course_admin { /** update courses cache * --- -- -- - - - + * Forces re-caching of courses tree * Shall run if anything changes to courses * * @param void - * @return (array): the two course caches + * @return (array): ['tree' => ..., 'breadcrumbs' => ... ] */ public static function update_courses_cache() { - $tree = proxy( // cache-get categories - [\app\models\cms\Course_model::class, 'category_tree'], - [], CACHE_ROOT_TTL, PROXY_IGNORE_CACHE - ); - $breadcrumbs = proxy( // cache-get breadcrumbs - [\app\models\cms\Course_model::class, 'breadcrumbs'], - [$tree], CACHE_ROOT_TTL, PROXY_IGNORE_CACHE - ); - - return [ - 'tree' => $tree, - 'breadcrumbs' => $breadcrumbs - ]; + return Cache_service::courses_struct(PROXY_IGNORE_CACHE); } @@ -289,10 +271,7 @@ class Course_admin { */ public static function files() { - return proxy( // cache-get files - [\app\models\cms\Course_model::class, 'files'], - [], CACHE_ROOT_TTL - ); + return Cache_service::files_attributes(); } diff --git a/public/app/controllers/cms/Course.php b/public/app/controllers/cms/Course.php index b671a5f..7e53d3a 100644 --- a/public/app/controllers/cms/Course.php +++ b/public/app/controllers/cms/Course.php @@ -6,9 +6,45 @@ use Registry; use Render; use app\controllers\Auth; use app\models\cms\Course_model; +use app\extends\Cache_service; class Course { + + /** PERMITION + * ------------------------------------------------------------------------- + */ + + + /** is admin or permited + * + * shortcut method for checking access authorization + * + * returns true if user belogns to the admin group + * or has the specified permition/privilege + * + * @param $privilege_id (int) + * + * NOTE: + * unlike the other authorization methods ... + * $privilege_id is NOT an array but a single privilege id + * + * @return true|false + */ + private static function is_admin_or_permited( $privilege_id) + { + return ( + Auth::in_admin_group() + || Auth::has_Permition([ $privilege_id ]) + ); + } + + + + /** FILES + * ------------------------------------------------------------------------- + */ + /** serve file by file_path * (request is valid only for admin users) * @@ -17,8 +53,10 @@ class Course { */ public static function serve_file($file_path) { - // check privileges - if (Auth::is_admin()) { // TODO: -OR- has prvivilege + $file = self::get_file_attributes($file_path); + + // if user is authorized + if (is_admin_or_permited($file['privilege_id'])) { $media_type = Registry::get('REQUEST')->GET['type']; // get media-type $real_path = MEDIA_STORAGE_ROOT . $file_path; // construct real path @@ -33,4 +71,78 @@ class Course { } } + + /** get_file_attributes + * + * returns attributes of a file + * (medias are proxied for speed optimization) + * + * @param $path (string) : file path + * @return $file attributes --or-- false + */ + private static function get_file_attributes($path) + { + $medias = Cache_service::files_attributes(); + + foreach($medias as $key => $medi) { + if ($medi['path'] == $path) { + return $medi; + } + } + + return false; + } + + + + /** COURSES + * ------------------------------------------------------------------------- + */ + + + public static function course($id) + { + $cache = Cache_service::courses_struct(); + $tree = $cache['tree']; + $breadcrumbs = $cache['breadcrumbs']; + $lessons = Course_model::lessons_of_course(intval($id)); + $id = intval($id); + + // print_r($breadcrumbs); die(); + + // find parent_ids of current category (to open the menu tree) + $course_path = []; + foreach($breadcrumbs[$id]['parents'] as $key => $parent) { + $course_path[] = intval($parent['id']); + } + $course_path[] = intval($id); + + Render::view('templates/course', [ + 'id' => $id, + 'title' => $breadcrumbs[ $id ]['rec']['label'], + 'categories' => $tree, + 'breadcrumbs' => $breadcrumbs, + 'lessons' => $lessons, + 'course_path' => $course_path + ]); + } + + + + + + + /** LESSONS + * ------------------------------------------------------------------------ + */ + + + + + + + + + + } \ No newline at end of file -- cgit v1.2.3