Can I authorize a method in a request class request for a personalized message for the HandlesAuthorization function?

I have the following code in the Request class to check if the user is allowed to upgrade.

HandlesAuthorization trait , the default message is displayed. Is there any way to return a customized message? I saw that the authorization method only in Request class can return boolean .

 class UpdateRoleRequest extends Request { private $UserPermissionsSession; public function __construct(IRole $Role) { $this->UserPermissionsSession = new UserPermissionsSession(); } public function authorize() { $UserID = \Auth::user()->UserID; return $this->UserPermissionsSession->CheckPermissionExists($UserID); } } 
+7
laravel laravel-5 laravel-request
source share
1 answer

I believe that you should not look at the HandlesAuthorization . All you have to do is implement the failedAuthorization method in your query class.

In the FormRequest class FormRequest it is defined as follows:

 /** * Handle a failed authorization attempt. * * @return void * * @throws \Illuminate\Auth\Access\AuthorizationException */ protected function failedAuthorization() { throw new AuthorizationException('This action is unauthorized.'); } 

so all you need is to override this method in your UpdateRoleRequest class, for example, as follows:

 protected function failedAuthorization() { throw new \Illuminate\Auth\Access\AuthorizationException('User has to be an admin.'); } 
+2
source share

All Articles