Last active
November 28, 2017 06:37
-
-
Save cyberfly/53fc592ce0220ff69fddc34a04cbbd8c to your computer and use it in GitHub Desktop.
Laravel Middleware check Role using Laratrust/Entrust
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
| protected $routeMiddleware = [ | |
| 'check_user_role' => \App\Http\Middleware\CheckUserRole::class, | |
| 'check_product_ownership' => \App\Http\Middleware\CheckProductOwnership::class, | |
| ]; |
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
| <?php | |
| namespace App\Http\Middleware; | |
| use Closure; | |
| class CheckUserRole | |
| { | |
| /** | |
| * Handle an incoming request. | |
| * | |
| * @param \Illuminate\Http\Request $request | |
| * @param \Closure $next | |
| * @return mixed | |
| */ | |
| public function handle($request, Closure $next, $role) | |
| { | |
| //get current logged in user info | |
| $user = auth()->user(); | |
| //check user role, break if role not allowed | |
| if (!$user->hasRole($role)) { | |
| dd('Anda tak boleh akses kawasan ini'); | |
| } | |
| return $next($request); | |
| } | |
| } |
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
| <?php | |
| namespace App\Http\Controllers; | |
| use Illuminate\Http\Request; | |
| class ProductsController extends Controller | |
| { | |
| public function __construct(){ | |
| //check user role, except method that can be access by public user | |
| $this->middleware('check_user_role:members')->except('index', 'getStateAreas', 'getCategorySubcategories','show'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment