mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange.git
synced 2026-08-24 14:54:38 +00:00
added entrust role based permission
This commit is contained in:
@@ -58,5 +58,8 @@ class Kernel extends HttpKernel
|
||||
'can' => \Illuminate\Auth\Middleware\Authorize::class,
|
||||
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
'ability' => 'App\Http\Middleware\TokenEntrustAbility',
|
||||
'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
|
||||
'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Tymon\JWTAuth\Exceptions\JWTException;
|
||||
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
|
||||
use Tymon\JWTAuth\Middleware\BaseMiddleware;
|
||||
|
||||
class TokenEntrustAbility extends BaseMiddleware
|
||||
{
|
||||
public function handle($request, Closure $next, $roles, $permissions, $validateAll = false)
|
||||
{
|
||||
|
||||
if (! $token = $this->auth->setRequest($request)->getToken()) {
|
||||
return $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$user = $this->auth->authenticate($token);
|
||||
} catch (TokenExpiredException $e) {
|
||||
return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
|
||||
} catch (JWTException $e) {
|
||||
return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
|
||||
}
|
||||
|
||||
if (! $user) {
|
||||
return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
|
||||
}
|
||||
|
||||
if (!$request->user()->ability(explode('|', $roles), explode('|', $permissions), array('validate_all' => $validateAll))) {
|
||||
return $this->respond('tymon.jwt.invalid', 'token_invalid', 401, 'Unauthorized');
|
||||
}
|
||||
|
||||
$this->events->fire('tymon.jwt.valid', $user);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user