Giter Club home page Giter Club logo

Comments (5)

kellysutton avatar kellysutton commented on July 29, 2024

I've been running into this question of authorization a bit with some of my own applications as well.

However, I think authorization squarely lies in the responsibilities of the controller and not at the model/resource level. Thus, I subclass the JSONAPI::ResourceController and make sure it does the short-circuiting.

This duplicates the efforts a little bit when policies are model-dependent, i.e. "This model can only be seen if the current user was the author." This results in issuing a find request twice, but I find that's acceptable tradeoff when it comes to code cleanliness.

Here's a little example:

class ModelController < JSONAPI::ResourceController
  def show
    keys = parse_key_array(params[resource_klass._primary_key])

    resources = if keys.length > 1
                  resource_klass.find_by_keys(keys, context: context)
                else
                  resource_klass.find_by_key(keys[0], context: context)
                end

    Array(resources).each do |resource|
      unless resource.user == current_user
        # Raise your 403
      end
    end

    super
  rescue => e
    handle_exceptions(e)
  end 
end

from jsonapi-resources.

barelyknown avatar barelyknown commented on July 29, 2024

I don't see the resource being as coupled to the model as you do, and I also don't think your proposed solution works for the other actions (create, update...).

Am I missing something? How do you handle the authorization of create and update actions using your approach?

from jsonapi-resources.

lgebhardt avatar lgebhardt commented on July 29, 2024

@barelyknown, I don't fully understand your business case. However I think it sounds like an issue if the after action callbacks aren't being called in the event of an error. So I'd be interested in a change that allows them to be called and have access to the errors collection. I haven't looked into what that would take, and what else will be affected.

from jsonapi-resources.

barelyknown avatar barelyknown commented on July 29, 2024

What about adding an error to the errors collection when ArgumentError is raised because of an invalid enum instead of raising JSONAPI::Exceptions::InvalidFieldValue?

from jsonapi-resources.

lgebhardt avatar lgebhardt commented on July 29, 2024

@barelyknown, I think that sounds good. It should probably be treated more like a validation error.

from jsonapi-resources.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.