samedi 4 janvier 2020

phpuint Illuminate\Auth\Access\AuthorizationException: This action is unauthorized

I have index method in controller like this

 public function index()
{
     $this->authorize('index', Contact::class);
  ....
}

and index method in ContactPolicy

public function index()
{
    return Auth::user()->can('view_Contact');
}

and test method like this

/** @test */
public function a_user_without_permission_can_not_see_contacts()
{
    $this->login(['no_permission']);
    $this->get('/contacts')
        ->assertStatus(403);
}

When I run my test show me this error

1) Tests\Feature\AccountTest::a_user_without_permission_can_not_see_contacts Illuminate\Auth\Access\AuthorizationException: This action is unauthorized.

Note1: When I change My controller to this it work correctly and show me green

public function index()
{
    if(Auth::user()->can('view_Contact')){
      ........
    }else
    return response()->view('403',['msg' => 'you_not_have_not_permission'])->setStatusCode(403) ;

Note2:
In login method in test class I send user permissions parameters and it work correctly.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire