Skip to content

Limiting writes based on model instance #171

Closed

Description

@ribordy

Looking at the documentation, it looks like you can pass a method to readable that accepts a model_instance. It does not appear that you can do the same for writable. Is there an alternative method of limiting writes based on permissions or authorization?

The use case here is that I'd like to enables writes on a resource only to the owner of that resource, i.e.:

class ThingResource < ApplicationResource
  attribute :id, :uuid
  attribute :name, :string, writable: :owner?

  def owner?(model_instance)
    model_instance.owner == context.current_user
  end
end

Activity

  1. caseyprovost commented on Oct 2, 2019

    @caseyprovost
    Contributor

    @ribordy I believe the way you would go about this is by using a library like Pundit to perform your authorizations at the object or object attribute level.

  2. caseyprovost commented on Oct 2, 2019

    @caseyprovost
    Contributor

    This will also keep your authorization logic out of your resources, which will make testing and re-use far easier.

  3. ribordy commented on Oct 2, 2019

    @ribordy
    Author

    Thanks @caseyprovost! I had never heard of Pundit, but this seems like exactly what we're looking for.

    Maybe it's worth adding a link to Pundit in the documentation, @richmolj?

  4. onerinas commented on Oct 5, 2024

    @onerinas

    Here is another way without using pundit: #134 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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