Skip to content

Instantly share code, notes, and snippets.

@ziyahan
Last active May 25, 2016 20:09
Show Gist options
  • Save ziyahan/e76b5ed2548592741e2707d37097a1ac to your computer and use it in GitHub Desktop.
Save ziyahan/e76b5ed2548592741e2707d37097a1ac to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use Closure;
class CheckPermission
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$aclInstance = app()->make("Acl");
$Response = app()->make("Response");
$resource =$this->getResource($request);
if($request->user()) {
$userRole = $request->user()->role;
do {
if($aclInstance->isAllowed($userRole->role_slug, $resource)) {
return $next($request);
}
}
while($userRole = $userRole->parents);
return $Response::view('errors.403', array(), 403);
} else {
if($aclInstance->isAllowed("guest", $resource)) {
return $next($request);
} else {
return redirect()->route("Home");
}
}
}
function getResource(&$request) {
return $request->route()->getAction()["as"];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment