Last active
August 29, 2015 14:15
-
-
Save ivanhoe011/5c81a149cf2d4ab890b0 to your computer and use it in GitHub Desktop.
How to authorize different types of users in L4/5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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