Skip to content

Updating a model's relationships where the policy denies updating the relationship gives 200 OK, but doesn't update the relationship #281

Open
@Azeirah

Description

@Azeirah

I have the following Schema:

class UpsellSchema extends Schema
{
  /**
   * The model the schema corresponds to.
   *
   * @var string
   */
  public static string $model = Upsell::class;

  /**
   * Get the resource fields.
   *
   * @return array
   */
  public function fields(): array
  {
    return [
      ID::make(),
      Str::make("name"),
      Str::make("upsell_variant"),
      BelongsTo::make("salesarea"),
      BelongsToMany::make("products"),
      DateTime::make("createdAt")
        ->sortable()
        ->readOnly(),
      DateTime::make("updatedAt")
        ->sortable()
        ->readOnly(),
    ];
  }

Note the "BelongsToMany" relationship with "products".

My policy for Upsell is as follows (in short, it allows any updates to the model):

class UpsellPolicy
{
  use HandlesAuthorization;

  /**
   * Determine whether the user can view any models.
   */
  public function viewAny(?User $user): bool
  {
    return true;
  }

  /**
   * Determine whether the user can view the model.
   */
  public function view(?User $user, Upsell $upsell): bool
  {
    return true;
  }

  /**
   * Determine whether the user can create models.
   */
  public function create(User $user): bool
  {
    return true;
  }

  /**
   * Determine whether the user can update the model.
   */
  public function update(User $user, Upsell $upsell): bool
  {
    return true;
  }

  /**
   * Determine whether the user can delete the model.
   */
  public function delete(User $user, Upsell $upsell): bool
  {
    return true;
  }

  /**
   * Determine whether the user can restore the model.
   */
  public function restore(User $user, Upsell $upsell): bool
  {
    return true;
  }

  /**
   * Determine whether the user can permanently delete the model.
   */
  public function forceDelete(User $user, Upsell $upsell): bool
  {
    return true;
  }
}

Now when I make the following request, intending to replace an upsell's products relationship

PATCH http://mysite.com/jsonapi/upsells/20

BODY
{
   "data":{
      "type":"upsells",
      "id":"20",
      "attributes":{
         "name":"MyTesty :Daa",
         "upsell_variant":"upsell"
      },
      "relationships":{
         "products":{
            "data":[
               {
                  "type":"products",
                  "id":"135219"
               },
               {
                  "type":"products",
                  "id":"135189"
               },
               {
                  "type":"products",
                  "id":"135191"
               }
            ]
         }
      }
   }
}

I get a 200 OK, but the upsell#20.products relationship is not updated.

Once I added the following function to the UpsellPolicy, it does get updated.

  public function updateProducts(User $user, Upsell $upsell): bool
  {
    return true;
  }

I'm expecting a 401 Unauthorized exception, not a 200 OK.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions