Sha256: 9bd5d7b8cbf39e365f802f72b2b6e91ee1c1e800478fab7b1cd7066435c629cd

Contents?: true

Size: 1.75 KB

Versions: 19

Compression:

Stored size: 1.75 KB

Contents

module RoleAuthorization
  class Mapper
    def initialize
      @rules = Hash.new do |h,k|
        h[k] = Array.new
      end
      self
    end

    def to_s
      output = []
      @rules.each_pair do |action, rules|
        output << "Action :#{action}"
        rules.map {|rule| output << "    #{rule.to_s}"}
        output << ""
        output << ""
      end

      output.join("\n")
    end

    def with_options(options, &block)
      unless block.nil?
        @with_options = options
        instance_eval(&block)
        @with_options = nil
      end
    end

    def add_to_rules(rule_name, *options, &block)
      extracted_options, extracted_role = if options.is_a?(Hash)
                                            [options, nil]
                                          elsif options.last.is_a?(Hash)
                                            [options.pop, options.first]
                                          else
                                            [{}, options.first]
                                          end

      merged_options = if @with_options.nil?
                         extracted_options
                       else
                         @with_options.merge(extracted_options)
                       end

      rule = RoleAuthorization::Rules::Rule.new(extracted_role, merged_options, &block)

      actions = ([rule.options[:only] || [:all]]).flatten.map(&:to_sym)

      actions.map do |action|
        @rules[action] << rule
      end
    end

    def authorized?(controller_instance, controller, action, id = nil)
      rules = @rules[action]

      return false if rules.empty?

      rules.map do |rule|
        return true if rule.authorized?(controller_instance, controller, action, id)
      end

      return false
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
role_authorization-0.9.6 lib/role_authorization/controller/mapper.rb
role_authorization-0.9.5 lib/role_authorization/controller/mapper.rb
role_authorization-0.9.4 lib/role_authorization/controller/mapper.rb
role_authorization-0.9.3 lib/role_authorization/controller/mapper.rb
role_authorization-0.9.2 lib/role_authorization/controller/mapper.rb
role_authorization-0.9.1 lib/role_authorization/controller/mapper.rb
role_authorization-0.9.0 lib/role_authorization/controller/mapper.rb
role_authorization-0.6.2 lib/role_authorization/controller/mapper.rb
role_authorization-0.6.1 lib/role_authorization/controller/mapper.rb
role_authorization-0.6.0 lib/role_authorization/controller/mapper.rb
role_authorization-0.5.9 lib/role_authorization/controller/mapper.rb
role_authorization-0.5.8 lib/role_authorization/controller/mapper.rb
role_authorization-0.5.7 lib/role_authorization/controller/mapper.rb
role_authorization-0.5.6 lib/role_authorization/controller/mapper.rb
role_authorization-0.5.5 lib/role_authorization/controller/mapper.rb
role_authorization-0.5.4 lib/role_authorization/controller/mapper.rb
role_authorization-0.5.3 lib/role_authorization/controller/mapper.rb
role_authorization-0.5.2 lib/role_authorization/controller/mapper.rb
role_authorization-0.5.1 lib/role_authorization/controller/mapper.rb