Sha256: b2f23c4e31ae063ebd8ba4fd15bc53f3f2d08c96010faa84e3f2416a8f571538

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module Surrounded
  module AccessControl
    def self.extended(base)
      base.send(:include, AccessMethods)
      unless defined?(base::AccessError)
        base.const_set(:AccessError, Class.new(::Surrounded::Context::AccessError))
      end
    end
    
    private
    
    def disallow(*names, &block)
      names.map do |name|
        define_access_method(name, &block)
      end
    end
    alias_method :guard, :disallow
    
    def trigger_return_content(name)
      %{

        method_restrictor = "disallow_#{name}?"
        if self.respond_to?(method_restrictor, true) && self.send(method_restrictor)
          raise ::#{self.to_s}::AccessError.new("access to #{self.name}##{name} is not allowed")
        end

      #{super}
      }
    end
    
    def define_access_method(name, &block)
      class_eval {
        define_method "disallow_#{name}?" do
          begin
            apply_roles if __apply_role_policy == :trigger
            instance_exec(&block)
          ensure
            remove_roles if __apply_role_policy == :trigger
          end
        end
      }
    end
    
    module AccessMethods
      # Return a Set of all defined triggers regardless of any disallow blocks
      def all_triggers
        self.class.triggers
      end
    
      # Return a Set of triggers which may be run according to any restrictions defined
      # in disallow blocks.
      def triggers
        all_triggers.select {|name|
          method_restrictor = "disallow_#{name}?"
          !self.respond_to?(method_restrictor, true) || !self.send(method_restrictor)
        }.to_set
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
surrounded-0.8.3 lib/surrounded/access_control.rb