HttpException in Handler.php line 133: This action is unauthorized

≯℡__Kan透↙ 提交于 2019-12-08 15:40:06

问题


I have created an application using laravel 5.3 and it is working fine on localhost but after I uploded all my code on a server I have this error:

Symfony\Component\HttpKernel\Exception\HttpException in /home/project/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php line 133: This action is unauthorized.

This is happening when I try to call functions whithin my controllers using post.

This is one example:

Route

Route::group(['middleware' => 'auth'], function () {
    Route::group(['middleware' => 'admin'], function () {
         Route::post('admin/store/', 'Admin\AnnouncementController@store');
    });
});

Controller

protected function store(AnnouncementRequest $request) {
    return Auth::user()->id;
}

How can I fix this? Why is this not happening on my localhost?

Thanks in advance.


回答1:


Check that your AnnouncementRequest file is set to return true from authorize function. The default is to return false.




回答2:


If you can use CustomRequest method for validation then make sure to your authorize() return true. If you can set false then its never call your function as well throw the error This action is unauthorized

Solution

class CopyRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
         return true;   //Default false .Now set return true;
    }
}



回答3:


Well, for what I saw, there can be a lot of situations for this scenario. In my case, I was using a custom FormRequest named AnnouncementRequest. Inside that class I was checking for a role property on the auth user.

// before
public function authorize() {
    if(Auth::user()->role_id === 1) {
        return true;
    }

    return false;
}

My mistake was to use === instead == for validation. So after fixing that everything is working just fine.

// after
public function authorize() {
    if(Auth::user()->role_id == 1) {
        return true;
    }

    return false;
}

Anyway why did it worked on localhost but did not on the server remains a mystery for me...




回答4:


Default function return false so change it as shown below

public function authorize()
    {
        return true;
    }

or also can use Auth in the Request

use Illuminate\Support\Facades\Auth;
 public function authorize()
    {
        return Auth::check();
    }



回答5:


The authorize() function by default it returns false, return true, your issue will be resolved




回答6:


In your Request file default not enable authorisation

public function authorize()
    {
        return false;
    }

if you enable your request file here showing that code.

public function authorize()
    {
        return true;
    }


来源:https://stackoverflow.com/questions/42215055/httpexception-in-handler-php-line-133-this-action-is-unauthorized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!