Sha256: 01a9ef89a4d09f678f6b0588d7898823b2aa18b08d47297b7537420410505b52

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 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


  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
scram-0.1.3 lib/scram/concerns/holder.rb
scram-0.1.2 lib/scram/concerns/holder.rb
scram-0.1.1 lib/scram/concerns/holder.rb
scram-0.1.0 lib/scram/concerns/holder.rb