diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-04-17 12:12:22 +0300 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-04-17 12:12:22 +0300 |
| commit | 68fc9e55e538e03f94509731ab41a0c8cf96710f (patch) | |
| tree | 3edb1e81864ac3eb35ba0fe8087ed24da1b812bf /html/app/controllers/Auth.php | |
| parent | 84b2da85a1b452922dadb6a609533b9945afe51d (diff) | |
| download | classroom-68fc9e55e538e03f94509731ab41a0c8cf96710f.tar.gz classroom-68fc9e55e538e03f94509731ab41a0c8cf96710f.tar.bz2 classroom-68fc9e55e538e03f94509731ab41a0c8cf96710f.zip | |
setup a real server environment (apache DocumentRoot=/var/www/public)
Diffstat (limited to 'html/app/controllers/Auth.php')
| -rw-r--r-- | html/app/controllers/Auth.php | 282 |
1 files changed, 0 insertions, 282 deletions
diff --git a/html/app/controllers/Auth.php b/html/app/controllers/Auth.php deleted file mode 100644 index b9314ff..0000000 --- a/html/app/controllers/Auth.php +++ /dev/null @@ -1,282 +0,0 @@ -<?php -namespace app\controllers; - -use Registry; -use Render; - -// user classes and models -use app\extends\Classroom_user; -use app\extends\Classroom_manager; -use app\models\admin\User_model; - -use app\extends\Send_mail; -use app\extends\Mail_jet; - - -/** class Auth - * - * handles user's Authentication and Authorizarion - * - */ -class Auth { - - /** login - * - * checks visitor's credentials; - * if valid, authenticates user - * - */ - public static function login() - { - $req = Registry::get('REQUEST'); - - // get the record of the target user - $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 Classroom_user()) - ->setID($record['id']) - ->setUserName($record['email']) - ->setPassword($record['password']) - ->setEnabled($record['active']); - - // let user manager to validate user credentials - $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; - } - } - - - /** activate - * resolves a call like: /account/activate?ticket=ca42d68cfba5fbbafeacc010b8e3a551 - */ - public static function activate() - { - $req = Registry::get('REQUEST'); - - // get the record of the target user - $check = User_model::activate($req->GET['ticket']); - - if ($check == true) { - Render::view('/error/general', [ - 'title' => ACCOUNT_ACTIVATED_TITLE, - 'message' => ACCOUNT_ACTIVATED_MESSAGE - ]); - - } else { - Render::view('/error/general', [ - 'title' => NOT_VALID_ACTIVATION_TITLE, - 'message' => NOT_VALID_ACTIVATION_MESSAGE - ]); - } - - } - - /** register - * - * Method for new user registration - * - */ - public static function register() - { - $userManager = new Classroom_manager(); - $req = Registry::get('REQUEST'); - - // create a salted password hash - $password = $userManager->cryptPassword($req->POST['password']); - - // echo $password; print_r($req->POST); die(); // OK! - - $user = (new Classroom_user()) - ->setUserName($req->POST['email']) - ->setPassword($password) - ->setRoles([ READER ]) // Role: authorized reader - ->setPrivileges([]); // none privilege until acount confirmation - - // create user record - $activation_code = User_model::registerUser($req->POST, $password); - - // TODO: - // handle error on user registration - // ... - // - // if ($activatopn_code[] == -1) { - // return [ - // 'success' => false, - // 'message' => REGISTRATION_USER_EXISTS - // ]; - // } - - $send_mail = Send_mail::send_activation_code([ - 'email' => $req->POST['email'], - 'name' => $req->POST['name'] .' '. $req->POST['surname'], - 'code' => $activation_code['activation'] - ]); - - // Send replies - if ($send_mail) { - return [ - 'success' => true, - 'message' => REGISTRATION_SUCCESS - ]; - - } else { - return [ - 'success' => false, - 'message' => 'error on sending email' - ]; - } - - } - - - - - - - public static function logout() - { - $userManager = new UserManager(); - $userManager->logout(); - } - - - - /** 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 - * to access the source - * - */ - public static function hasPermition($permit = []) - { - if ($permit == []) return true; - } - - - /** isAuthenticated() - * - * chechs if the user's roles and permitions - * satisfy the specified requirements - * to access the source - * - * @param $requirements (array of rules-array) - * - * example: - * [ - * [ - * role => ['editor','designer'] - * permition => ['10', '12', '18'] - * ], - * [ - * role => ['admin' , 'developερ'] - * ], - * [ - * permition => [ 3 ] - * ] - * ] - * - * defines (and parses to) a requirements rule of: - * [ - * user should be editor or designer - * and have permition 10 or 12 or 18 - * ] - * OR - * [ - * user should be an administratoe or developer - * ] - * OR - * [ - * user should have permition #3 - * ] - * - * - */ - public static function isAuthorized($requirements) - { - $authorized = false; - foreach($requirements as $required) { - if ( (self::isGranted($required['role'] ?? [])) - && (self::hasPermition($required['permition'] ?? [])) ) { - $authorized = true; - } - } - return $authorized; - } - - - - - public static function forgot_pass() - { - } - - - - public static function validate_otp() - { - } - - - -} - - -// NOTE: -// check: https://netcorecloud.com/tutorials/send-an-email-via-gmail-smtp-server-using-php/
\ No newline at end of file |
