Sha256: 152a3374e074177719ec5782e451ff4853898b37fe7ae9237e2d317ffd12188c

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Scram
  using SymbolExtensions

  # Base class to represent a Holder of permissions through policies.
  # @note Implementing classes must implement #policies and #scram_compare_value
  module Holder
    extend ActiveSupport::Concern

    # @return [Array] list of policies
    def policies
      raise NotImplementedError
    end

    # @return [Object] a value to compare {Holder} in the database. For example, an ObjectID would be suitable.
    def scram_compare_value
      raise NotImplementedError
    end

    # Checks if this holder can perform some action on an object by checking the Holder's policies
    # @param action [String] What the user is trying to do to obj
    # @param obj [Object] The receiver of the action
    # @return [Boolean] Whether or not holder can action to object. We define a full abstainment as a failure to perform the action.
    def can? action, target
      target = target.to_s if target.is_a? Symbol
      action = action.to_s

      # Checks policies in priority order for explicit allow or deny.
      policies.sort_by(&:priority).reverse.each do |policy|
        opinion = policy.can?(self, action, target)
        return opinion.to_bool if %i[allow deny].include? opinion
      end

      return false
    end

    # Helper method to enhance readability of permission checks
    def cannot? *args
      !can?(*args)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scram-0.1.4 lib/scram/concerns/holder.rb