Skip to content

Instantly share code, notes, and snippets.

@cyberfly
Last active November 28, 2017 06:37
Show Gist options
  • Select an option

  • Save cyberfly/53fc592ce0220ff69fddc34a04cbbd8c to your computer and use it in GitHub Desktop.

Select an option

Save cyberfly/53fc592ce0220ff69fddc34a04cbbd8c to your computer and use it in GitHub Desktop.
Laravel Middleware check Role using Laratrust/Entrust
protected $routeMiddleware = [
'check_user_role' => \App\Http\Middleware\CheckUserRole::class,
'check_product_ownership' => \App\Http\Middleware\CheckProductOwnership::class,
];
<?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);
}
}
<?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