diff options
Diffstat (limited to 'html')
| -rw-r--r-- | html/app/controllers/Auth.php | 28 | ||||
| -rw-r--r-- | html/app/extends/Classroom_user.php | 36 | ||||
| -rw-r--r-- | html/app/models/admin/User_model.php | 76 | ||||
| -rw-r--r-- | html/app/routes/frontend.php | 45 | ||||
| -rw-r--r-- | html/app/views/user/login.php | 15 |
5 files changed, 94 insertions, 106 deletions
diff --git a/html/app/controllers/Auth.php b/html/app/controllers/Auth.php index 6555f1c..b9314ff 100644 --- a/html/app/controllers/Auth.php +++ b/html/app/controllers/Auth.php @@ -31,25 +31,43 @@ class Auth { $req = Registry::get('REQUEST'); // get the record of the target user - $record = User_model::checkUser('email', $req->POST['email']); + $record = User_model::checkUser($req->POST['email']); // if no user exists, return false if ($record === false) return false; - + + // user is valid; check user password // create a user object - $user = (new User()) + $user = (new Classroom_user()) + ->setID($record['id']) ->setUserName($record['email']) ->setPassword($record['password']) - ->setRoles(json_decode($record['roles'])) ->setEnabled($record['active']); // let user manager to validate user credentials - $userManager = new UserManager(); + $userManager = new Classroom_manager(); if ($userManager->isPasswordValid($user, $req->POST['password'])) { + + // get user's security attributes + $attributes = User_Model::getUser($record['id']); + $user + ->setRoles(json_decode($attributes['Roles_json'])) + ->setPrivileges( + explode( // example transform `[2,7] . [[1],[5,6]]` to `[2,7,1,5,6]` + str_replace( + ['[', ']'], // remove square parenthesis + '', + $attributes['RootPrivileges_json'] . $attributes['SubPrivileges_json'] + ), + ',' + ) + ); + // login OK, set Token in session $userManager->createUserToken($user); + return true; } else { return false; diff --git a/html/app/extends/Classroom_user.php b/html/app/extends/Classroom_user.php index c51c79b..1c26e73 100644 --- a/html/app/extends/Classroom_user.php +++ b/html/app/extends/Classroom_user.php @@ -24,10 +24,44 @@ use Registry; class Classroom_user extends User { - // @var array + /** user id + * @var int + */ + private $id; + + /** user privileges + * @var array + */ private $privileges = []; + /** SETTERS AND GETTERS + * for id and privileges attributes + * ------------------------------------------------------------------------- + */ + + /** getID + * @return int + */ + public function getID(): int + { + return $this->id; + } + + /** setID() + * + * @param int : user id + * + * @return User + */ + public function setID(int $id): self + { + $this->id = $id; + return $this; + } + + + /** getPrivileges * * @return privileges (array) diff --git a/html/app/models/admin/User_model.php b/html/app/models/admin/User_model.php index 397f198..62d99db 100644 --- a/html/app/models/admin/User_model.php +++ b/html/app/models/admin/User_model.php @@ -13,18 +13,13 @@ class User_model * @param $indexKey (string) : key for user identification * @param $value (string) * - * @param $identity (array): pair of [index_key => identity_value] - * example: [ 'email' => 'geo@roptron.gr' ] + * @param $email (string): user's email */ - public static function checkUser($identity) + public static function checkUser($email) { - $indexKey = array_key_first($identity); - $user = Registry::use('database')->query( - "SELECT * FROM user WHERE {$indexKey} = :val", - [ - ':val' => $identity[$indexKey] - ] + "SELECT * FROM user WHERE email = :email AND active = 1", + [ ':email' => $email ] )->getFirst(); // if no user, return false @@ -72,27 +67,17 @@ class User_model WHERE user_privilege.user_id = :id ) ) AS RootPrivileges_json, - ( - SELECT CONCAT( -- array of array of sub-privileges + ( -- construct array of (root-)privileges granted to user + SELECT CONCAT( '[', - GROUP_CONCAT( - ( - SELECT CONCAT( -- array (json) of subprivileges - '[', - GROUP_CONCAT(included_id), - ']' - ) - FROM privilege_includes - WHERE privilege_id = privilege.id - ) - ), + GROUP_CONCAT(privilege.includes), ']' ) FROM privilege WHERE privilege.id IN ( SELECT user_privilege.privilege_id FROM user_privilege - WHERE user_id = :id + WHERE user_privilege.user_id = :id ) ) AS SubPrivileges_json FROM user @@ -222,51 +207,6 @@ class User_model } - /** set_user_privileges - * - * @param $user_id (int) - * @param $privileges (array) - */ - public static function set_user_privileges($user_id, $privileges) - { - - foreach($privileges as $pri) { - Registry::use('database')->runQuery( - "INSERT INTO user_privilege (user_id, privilege_id) - VALUES (:user, :privilege)", - [ - ':user' => $user_id, - ':privilege' => $pri - ] - ); - } - - return true; - } - - /** inherited privileges - * - * list of all inhereted (sub-)privileges - * from a list of root-privileges - * - * @param $list (array of int): list of root privilege id(s) - */ - public static function inheritedPrivileges($list) - { - $rootList = "(". implode(', ', $list) .")"; - - $subPrivileges = Registry::use('database')->runQuery( - "SELECT privilege.id, ( - SELECT CONCAT('[', GROUP_CONCAT(included_id), ']') FROM privilege_includes - WHERE privilege_id = privilege.id - ) as subprivilege_json - FROM privilege - WHERE privilege.id IN {$rootlist}", [] - ); - } - - - } /* example query getUser (super-array) diff --git a/html/app/routes/frontend.php b/html/app/routes/frontend.php index ba831d6..67f4655 100644 --- a/html/app/routes/frontend.php +++ b/html/app/routes/frontend.php @@ -25,29 +25,37 @@ Route::notFound( function() { // Account /////////////////////////////////////////////////////////////////////////////////////////////// -// login -// register -// activate -// ask-reset -// reset -// profile -// edit-profile -// subscriptions -// payments -// pay -// order +// [ok] login +// [ok] register +// [ok] activate +// [..] ask-reset +// [..] reset +// [..] profile +// [..] edit-profile +// [..] subscriptions +// [..] payments +// [..] pay +// [..] order Route::add('/login', function() { Render::view('user/login'); }); Route::add('/registration', function() { Render::view('user/registration'); }); - - -Route::add('/account/check-login', function() { Auth::login(); }); +Route::add('/account/check-login', function() { + $response = Auth::login(); + Render::json(['status' => $response]); + }, + 'post' +); // user sends a registration form; -Route::add('/account/register', function() { Auth::register(); }, 'post'); +Route::add('/account/register', function() { + $response = Auth::register(); + Render::json($response); + }, + 'post' +); // user requests activation Route::add('/account/activate', function() { Auth::activate(); } ); @@ -67,13 +75,6 @@ Route::add('/account/reset_password', function() { Auth::reset_password(); } ); -Route::add('/login-check', function() { - $user = new Classroom_user(); - $user->register_user(); - }, - 'post' -); - // Resolve urls // --- -- -- - - - diff --git a/html/app/views/user/login.php b/html/app/views/user/login.php index dbfb2e5..c4f223f 100644 --- a/html/app/views/user/login.php +++ b/html/app/views/user/login.php @@ -4,15 +4,10 @@ <meta charset="utf-8"> <title><?=SITE_TITLE?> - Είσοδος Χρήστη</title> - <link rel="preconnect" href="https://fonts.googleapis.com"> - <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> - <link href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@400&display=swap" rel="stylesheet"> - - <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-aFq/bzH65dt+w6FI2ooMVUpc+21e0SRygnTpmBvdBgSdnuTN7QbdgL+OapgHtvPp" crossorigin="anonymous"> - - <link href="/assets/css/overides.css" rel="stylesheet"> - - <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/js/bootstrap.bundle.min.js" integrity="sha384-qKXV1j0HvMUeCBQ+QVp7JcfGl760yU08IQ+GpUo5hlbpg51QRiuqHAJz8+BrxE/N" crossorigin="anonymous"></script> + <?php // header includes + //////////////////////////////////////////////////////////////////////// + Render::view('components/header_includes'); + ?> <style> html,body{ @@ -45,7 +40,7 @@ <div class='content'> <h4>Είσοδος Χρήστη</h4> <hr /> - <form method="post" action="/login-check"> + <form method="post"> <div class="mb-3"> <label for="exampleInputEmail1" class="form-label">Email</label> <input type="email" class="form-control form-control-sm" name="email" aria-describedby="emailHelp" required> |
