Last active
May 25, 2016 20:09
-
-
Save ziyahan/e76b5ed2548592741e2707d37097a1ac to your computer and use it in GitHub Desktop.
This file contains 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 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