Skip to content

Instantly share code, notes, and snippets.

@ivanhoe011
Last active August 29, 2015 14:15
Show Gist options
  • Save ivanhoe011/5c81a149cf2d4ab890b0 to your computer and use it in GitHub Desktop.
Save ivanhoe011/5c81a149cf2d4ab890b0 to your computer and use it in GitHub Desktop.
How to authorize different types of users in L4/5
// in User.php model add few checks
public function is_admin()
{
return ($this->account_type === 'admin'); // account_type is ENUM field
}
public function has_rights($acc_type)
{
if ($this->is_admin()) { // if admin always true
return true;
}
return ($this->account_type === $acc_type);
}
// in filters.php define filter
Route::filter('authUserType', function($route, $request, $user_type) {
if (Auth::guest()) {
return Redirect::guest(route('login'));
}
if (Auth::user()->has_rights($user_type)) {
return;
}
App::abort(403, "Sorry, you're not authorized to access this page.");
});
// and then in routes.php you can check it like this:
Route::group(array('before' => 'authUserType:manager'), function() {
// only admins and managers can see these routes here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment