Skip to content

Per-query level verify_pundit_policy_scoped #728

@MyklClason

Description

@MyklClason

Have similar issue as this one, but have a solution in mind: #663

The issue is to be explicit and potentially excessive. Though we can make use of lazy query logic to prevent doing too much work: Basically given this:

records = Record.all # Lazy, so doesn't run here unless you try to view the result
records.pluck(:name) # This actually runs the database query

We want it to raise an error unless it has something like one of these two:

records = policy_scope(Record.all)
records.pluck(:name)

or

records = Record.all
policy_scope(records).pluck(:name)

This might be as simple as policy_scope setting a "pundit_policy_scoped" flag on Record.all (or worst case, a global/instance variable or just using the cache) and to_sql (or another method that is called when actually sending the query to the database) raising an error if the flag isn't set. We can skip doing both unless policy_scope is defined.

Even if it's not an official solution, it would be good to have a code example that allows for it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions