Open CaesarChan opened 6 years ago
$api->get('roles', ['middleware' => ['ability:Sales,system.role.view,false'], 'uses' => 'RoleAPIController@index']);
` class TokenEntrustAbility extends EntrustBaseMiddleware {
/** * @param $request * @param Closure $next * @param $roles * @param $permissions * @param bool $validateAll * @return mixed */ public function handle($request, Closure $next,$roles, $permissions, $validateAll = false) { $user = $this->getAuthUser($request,$next); if(!($user instanceof User)){ return $user; } /** * dd($validateAll) : 'false' */ $validateAll = filter_var($validateAll,FILTER_VALIDATE_BOOLEAN); if (!$user->ability(explode('|', $roles), explode('|', $permissions), ['validate_all' => $validateAll])) { return $this->respond('tymon.jwt.invalid', 'Forbidden', 403, 'Forbidden'); } $this->events->fire('tymon.jwt.valid', $user); return $next($request); }
} `
// Set up default values and validate options. if (!isset($options['validate_all'])) { $options['validate_all'] = false; } else { //must be boolean if ($options['validate_all'] !== true && $options['validate_all'] !== false) { throw new InvalidArgumentException(); } } if (!isset($options['return_type'])) { $options['return_type'] = 'boolean'; } else { if ($options['return_type'] != 'boolean' && $options['return_type'] != 'array' && $options['return_type'] != 'both') { throw new InvalidArgumentException(); } }
hope usefull for anyone else.
Route
$api->get('roles', ['middleware' => ['ability:Sales,system.role.view,false'], 'uses' => 'RoleAPIController@index']);
Middleware
` class TokenEntrustAbility extends EntrustBaseMiddleware {
} `
EntrustUserTrait
// Set up default values and validate options. if (!isset($options['validate_all'])) { $options['validate_all'] = false; } else { //must be boolean if ($options['validate_all'] !== true && $options['validate_all'] !== false) { throw new InvalidArgumentException(); } } if (!isset($options['return_type'])) { $options['return_type'] = 'boolean'; } else { if ($options['return_type'] != 'boolean' && $options['return_type'] != 'array' && $options['return_type'] != 'both') { throw new InvalidArgumentException(); } }
so , if you use middleware ,you should convert string to boolean use filter_var().
hope usefull for anyone else.