Skip to content

Documentation or Examples for Authorization #134

Closed

Description

@ambethia

Hello!

I found an example of authentication in the user-account-app-cookbook, but I'm looking for some guidance on best practices to achieve authorization with resources.

I could achieve simple authorization in the controller, but I'm mostly worried about sideloading relationships. Accessing the current_user in the context for resources is a start.

Example, say resource A has_many of resource B. Resource A is publically accessible, but B is not. I can easily do this in separate controllers for A and B, but only logged in users should be able to sideload B through A. I hope that makes sense.

Thank you!

Activity

  1. wadetandy commented on May 6, 2019

    @wadetandy
    Contributor

    Hi @ambethia, we typically recommend relying on the base_scope of a resource to scope the lookups to everything a given user should have access to. If your controller has a current_user object and you wanted to only be able to retrieve posts which belonged to that user in a PostResource, you could do the following:

    class PostResource < ApplicationResource
      def base_scope
        Post.where(user: context.current_user)
      end
    end

    Even if you are sideloading posts through a different resource, this scoping will be applied. If you want to scope the lookup differently based on whether the model is being read, updated, or deleted, you can access the Graphiti.context[:namespace] object in within the base_scope method:

    class PostResource < ApplicationResource
      def base_scope
        if Graphiti.context[:namespace].in? [:update, :destroy]
          context.current_user.posts
        else
          Post.where(published: true).or(context.current_user.posts)
        end
      end
    end

    The above would only look up a current user's posts for update or destroy actions but would allow a user to see all published posts as well as their own for reads.

    If instead you need to limit certain fields that can be taken on a given model, you can use read or write guards on specific fields or sideloads. If you wanted to let a user update their profile, but only admins could set other users as admins:

    # Assume the scoping for retrieving an editable user is already implemented
    class UserResource < ApplicationResource
      attribute :first_name, :string, writable: true
      attribute :last_name, :string, writable: true
      attribute :is_admin, :string, readable: :admin?, writable: :admin?
    
      def admin?
        context.current_user.admin?
      end
    end

    The guard methods also receive the instance in question as an argument, so you can run more dynamic logic against the specific instance as well.

    Let me know if this clears everything up. Would love to update the sample app to include more of these examples at some point as well.

  2. wadetandy commented on May 6, 2019

    @wadetandy
    Contributor

    Also, your specific example of not wanting to sideload if a user is not logged in a can most simply be resolved with an empty base scope using ActiveRecord's none query method.

    def base_scope
      if context.current_user
        super
      else
        MyModel.none
      end
    end
  3. ambethia commented on May 6, 2019

    @ambethia
    Author

    @wadetandy Thank you! this probably covers almost everything I need! Thank you!

  4. richmolj commented on May 6, 2019

    @richmolj
    Contributor

    Thanks @ambethia! We'll work on getting an Auth Cookbook up.

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