Skip to content

@canResolved acts different with @hasMany and @find #2714

@SlyDave

Description

@SlyDave

Describe the bug

If you define a field as nullable and the resolved value is null or an empty collection you get different behaviour.

if it's a collection @canResolved will not call $authroize because it will receive an empty array ([]) and Utils::applyEach which calls $authorize($model); will be skipped because foreach([]) doesn't do anything.

But if the resolved value is null, it will call $authroize as Utils::applyEach will simply call $callback($valueOrValues); and not be skilled. calling $authroize on null will always result in an Auth error as the Gate code will not be able to find the model class and so doesn't know what Policy to check.

me {
    field(id: ID @eq): Field @find @canResolved(ability: "view")
    collection: [Field!] @hasMany @canResolved(ability: "view")
}

Expected behavior/Solution

Consistency...

the hard part is deciding which one is correct.

  1. if the field is nullable then the resolved value is null, and you can't check permissions, should that be a rejection by default? (that is what Gate would do)

  2. or do you say, well, the value is null, so it doesn't matter (but then does this leak information, you know something doesn't exist and you were not checked for access beforehand)

If the second option, how does one allow a nullable field that checks permission to access the field, maybe if null a second ability option should be provided, such as viewAny

@canResolved(ability: "view", abilityOnNull: "viewAny")

This would require the model class to be determined/known to be able to find the correct Policy, so?

@canResolved(ability: "view", abilityOnNull: "viewAny", modelClassonNull: "\\App\\Models\\Field") ??

ugly.

Steps to reproduce

me @auth {
    field(id: ID @eq): Field @find @canResolved(ability: "view")
    collection: [Field!] @hasMany @canResolved(ability: "view")
}
  me {
    field {
      column
    }
    collection {
      column
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionRequires input from multiple people

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions