Created
June 27, 2016 06:29
-
-
Save blackavec/08dca145d75068206ca03b32512be074 to your computer and use it in GitHub Desktop.
Dummy code for checking user's role in order to verify it's permission to specific route
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
| if (!function_exists('check_route_user_access')) { | |
| /** | |
| * Checks if current user has right access to following route | |
| * | |
| * @param $route | |
| * | |
| * @return bool | |
| */ | |
| function check_route_user_access($route) : bool | |
| { | |
| $route = app('router')->getRoutes()->getByName($route); | |
| $user = app('auth')->user(); | |
| if (!$user || !$route) { | |
| return false; | |
| } | |
| $middlewares = $route->middleware(); | |
| $permissions = explode(':', $middlewares[0]); | |
| unset($middlewares); | |
| $permissions = explode(',', $permissions[1]); | |
| static $roles = []; | |
| if ($roles === []) { | |
| foreach ($permissions as $role) { | |
| $roles += config('acl.' . $role); | |
| } | |
| } | |
| return in_array($user->user_role, $roles); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment