Auth.php (3554B)
1 <?php 2 3 /** 4 * Class Auth 5 * Checks if user is logged in, if not then sends the user to "yourdomain.com/login". 6 * Auth::checkAuthentication() can be used in the constructor of a controller (to make the 7 * entire controller only visible for logged-in users) or inside a controller-method to make only this part of the 8 * application available for logged-in users. 9 */ 10 class Auth 11 { 12 /** 13 * The normal authentication flow, just check if the user is logged in (by looking into the session). 14 * If user is not, then he will be redirected to login page and the application is hard-stopped via exit(). 15 */ 16 public static function checkAuthentication() 17 { 18 // initialize the session (if not initialized yet) 19 Session::init(); 20 21 // self::checkSessionConcurrency(); 22 23 // if user is NOT logged in... 24 // (if user IS logged in the application will not run the code below and therefore just go on) 25 if (!Session::userIsLoggedIn()) { 26 // ... then treat user as "not logged in", destroy session, redirect to login page 27 Session::destroy(); 28 // send the user to the login form page, but also add the current page's URI (the part after the base URL) 29 // as a parameter argument, making it possible to send the user back to where he/she came from after a 30 // successful login 31 header('location: ' . Config::get('URL') . 'login?redirect=' . urlencode($_SERVER['REQUEST_URI'])); 32 // to prevent fetching views via cURL (which "ignores" the header-redirect above) we leave the application 33 // the hard way, via exit(). @see https://github.com/panique/php-login/issues/453 34 // this is not optimal and will be fixed in future releases 35 exit(); 36 } 37 } 38 39 /** 40 * The admin authentication flow, just check if the user is logged in (by looking into the session) AND has 41 * user role type 7 (currently there's only type 1 (normal user), type 2 (premium user) and 7 (admin)). 42 * If user is not, then he will be redirected to login page and the application is hard-stopped via exit(). 43 * Using this method makes only sense in controllers that should only be used by admins. 44 */ 45 public static function checkAdminAuthentication() 46 { 47 // initialize the session (if not initialized yet) 48 Session::init(); 49 50 // self::checkSessionConcurrency(); 51 52 // if user is not logged in or is not an admin (= not role type 7) 53 if (!Session::userIsLoggedIn() || Session::get("user_account_type") != 7) { 54 // ... then treat user as "not logged in", destroy session, redirect to login page 55 Session::destroy(); 56 header('location: ' . Config::get('URL') . 'login'); 57 // to prevent fetching views via cURL (which "ignores" the header-redirect above) we leave the application 58 // the hard way, via exit(). @see https://github.com/panique/php-login/issues/453 59 // this is not optimal and will be fixed in future releases 60 exit(); 61 } 62 } 63 64 /** 65 * Detects if there is concurrent session(i.e. another user logged in with the same current user credentials), 66 * If so, then logout. 67 * 68 */ 69 public static function checkSessionConcurrency(){ 70 if(Session::userIsLoggedIn()){ 71 if(Session::isConcurrentSessionExists()){ 72 LoginModel::logout(); 73 Redirect::home(); 74 exit(); 75 } 76 } 77 } 78 }