paranoid
paranoid

Reputation: 7105

Laravel 5.8 403 This action is unauthorized

I have update method like this

  public function update(Contact  $contact)
{

    $this->authorize('ownItems', $contact);
    ......
}

and ContactPolicy :

public function ownItem(User $user,Contact $contact)
{
  return true;
} 

It work correctly but when I replace Contcact to ContactRequest in my update method show me this :

403 This action is unauthorized.

update method :

public function update(ContactRequest  $contact)
{
    $this->authorize('ownItems', $contact);
    .......
}

authorize method in ContactRequest:

public function authorize()
{
    return true;
}

Upvotes: 0

Views: 6407

Answers (2)

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

ContactRequest is a laravel Request class instance


public function update(ContactRequest $request,Contact  $contact)
{
    $this->authorize('ownItems', $contact);
    .......
}

Upvotes: 2

Oleg Shakhov
Oleg Shakhov

Reputation: 445

You misspelled method name in $this->authorize('ownItems', $contact);, it should be "ownItem"

UPD

ContactRequest is probably instance of Illuminate\Http\Request but authorize method waiting for Model instance, if you do not have model identifier in yout request. first you should to find model: $model = Contact::find($contact->input('id')) and than check your policy with $this->authorize('ownItems', $model)

Upvotes: 1

Related Questions