[LINKED][Improvement] A more flexible Auth:checkAuthentication with arguments #701
Description
EDITED to address some concerns, see below
After seeing the new Auth::checkAdminAuthentication()
I was excited because I had already implemented a similar class elsewhere and decided to merge the two.
The idea is that you only need the one function Auth:checkAuthentication()
and you can supply arguments to it. If left alone with no argument, it will perform the usual check to see if a user is logged in, and if so, it will treat the user as having a default account 1
.
Yet if you supply an argument, say 7
, it will redirect the user to the dashboard if their user account type does not equal 7
, else it will allow the protected view to be rendered. All assuming the user is actually logged in in the first place.
So, to use it is simple....very simple. Add the function to any constructor of a controller or inside a controller-method to limit the users access.
I would love to know what you think!!
Here is an Example of how to use it
class AdminController extends Controller
{
public function __construct()
{
parent::__construct();
Auth::checkAuthentication(7);
}
public function index()
{
$this->View->render('admin/index');
}
}
And Here is the actual Auth Class
class Auth
{
// Notice the argument $type is set to 1 which is the default user account type, adjust accordingly
public static function checkAuthentication($type = 1)
{
// Initialize Session if it doesn't exist
Session::init();
//If user is not logged in, end of story, redirect
if (!Session::userIsLoggedIn()) {
// Destroy the session and redirect
Session::destroy();
header('location: ' . Config::get('URL') . 'login?redirect=' . urlencode($_SERVER['REQUEST_URI']));
exit();
}
/**
* If user account type is not 1 (default) and user type does not match supplied argument, redirect to dashboard
* Notice: the default user account type is set to 1 here as well
*/
if( 1 != $type AND Session::get("user_account_type") != $type){
/**
* Important! Do not supply an argument when using a controller that matches the redirect or this will
* cause a loop ending in a redirect failure!
* ie. dashboard or dashboard/index can use Auth::checkAuthentication() but not Auth::checkAuthentication($type)
*/
header('location: ' . Config::get('URL') . 'dashboard/index');
exit();
}
}
}
EDITS - For those who like keeping the additional Auth::checkAdminAuthentication()
, and would also like a way to check other account types
If you want to keep the Admin separated
public static function checkAdminAuthentication()
{
$admin = 7;
self::checkAuthentication($admin);
}
If you want to add other levels such as a Premium Member, or whatever you may desire.
public static function checkPremiumAuthentication()
{
$premium = 3;
self::checkAuthentication($premium);
}
With this proposed class, you can perform shortcuts to the above same methods using...
Auth::checkAuthentication() // Basic Member
Auth::checkAuthentication(3) // Premium Member
Auth::checkAuthentication(7) // Admin
...without the need to make any specific methods such as...
public static function checkPremiumAuthentication() {}
// or
public static function checkAdminAuthentication() {}