Skip to content

Instantly share code, notes, and snippets.

@davidderus
Last active October 26, 2016 11:47
Show Gist options
  • Save davidderus/94da6286b1046e7b0954134becc76209 to your computer and use it in GitHub Desktop.
Save davidderus/94da6286b1046e7b0954134becc76209 to your computer and use it in GitHub Desktop.

Revisions

  1. davidderus revised this gist May 13, 2016. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions application_policy.rb
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,6 @@
    #
    # A direct adaptation of http://through-voidness.blogspot.fr/2013/10/advanced-rails-4-authorization-with.html
    # but much more performance-wise
    #
    # @note Also handles things like `authorize :dashboard` or `authorize Vineyard`
    class ApplicationPolicy
    attr_reader :user, :record

    @@ -24,7 +22,7 @@ def user_activities

    # @param [Symbol] method Method to check in class
    # @return [String] A combination of model and method
    # @note We allow only Class or Symbol, nothing else, to do things like `authorize :dashboard` or `authorize Vineyard`
    # @note We allow only Class or Symbol, nothing else, to do things like `authorize :dashboard` or `authorize Product`
    def inferred_activity(method)
    record_class = (@record.class == Class || @record.class == Symbol) ? @record.to_s : @record.class.name
    "#{record_class.downcase}:#{method}"
  2. davidderus revised this gist Apr 27, 2016. No changes.
  3. davidderus revised this gist Apr 12, 2016. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion application_policy.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@
    #
    # A direct adaptation of http://through-voidness.blogspot.fr/2013/10/advanced-rails-4-authorization-with.html
    # but much more performance-wise
    #
    # @note Also handles things like `authorize :dashboard` or `authorize Vineyard`
    class ApplicationPolicy
    attr_reader :user, :record

    @@ -22,8 +24,10 @@ def user_activities

    # @param [Symbol] method Method to check in class
    # @return [String] A combination of model and method
    # @note We allow only Class or Symbol, nothing else, to do things like `authorize :dashboard` or `authorize Vineyard`
    def inferred_activity(method)
    "#{@record.class.name.downcase}:#{method}"
    record_class = (@record.class == Class || @record.class == Symbol) ? @record.to_s : @record.class.name
    "#{record_class.downcase}:#{method}"
    end

    # @param [String] name method name
  4. davidderus created this gist Apr 12, 2016.
    74 changes: 74 additions & 0 deletions application_policy.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    # Handling Application rights thanks to roles
    #
    # A direct adaptation of http://through-voidness.blogspot.fr/2013/10/advanced-rails-4-authorization-with.html
    # but much more performance-wise
    class ApplicationPolicy
    attr_reader :user, :record

    def initialize(user, record)
    raise Pundit::NotAuthorizedError, 'Must be signed in.' unless user
    @user = user
    @record = record
    end

    ##############################################################
    # Globalizing policies handling based on the role activities #
    ##############################################################

    # @return [Array] list of all users allowed activities
    def user_activities
    @user.roles.pluck(:activities).flatten.uniq
    end

    # @param [Symbol] method Method to check in class
    # @return [String] A combination of model and method
    def inferred_activity(method)
    "#{@record.class.name.downcase}:#{method}"
    end

    # @param [String] name method name
    # @param [Object] args method args
    def method_missing(name, *args)
    method_name = name.to_s
    if method_name[-1..-1] == '?'
    user_activities.include?(inferred_activity(method_name[0..-2]))
    else
    super
    end
    end

    ##########################
    # Keeping some shortcuts #
    ##########################

    # Just a shortcut for create?
    def new?
    create?
    end

    # Just a shortcut for edit?
    def edit?
    update?
    end

    ##################
    # Scope handling #
    ##################

    def scope
    Pundit.policy_scope!(user, record.class)
    end

    class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
    @user = user
    @scope = scope
    end

    def resolve
    scope
    end
    end
    end