Sha256: 0b8de0950acc9b457b1a6c5b2ea60fe5f17c3434772402b7df66d5f7f94d6bbb

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

module AccessManager
  module Control
    def self.included(base)
        base.extend(ClassMethods)
    end

    def can_access?(controller, action)
      action_codes.any? { |user_action| self.class.access_granted?(controller, action, user_action) }
    end

    def can?(action)
      action_codes.map(&:to_s).include?(action.to_s)
    end

    module ClassMethods
      attr_accessor :access_tree

      def grant_access_with(user_action, args={})
        if @access_tree.nil?
          @access_tree = { }
        end

        args[:to].each do |controller, actions|
          if @access_tree[controller.to_sym].nil?
            @access_tree[controller.to_sym] = { }
          end

          if actions == :all
            if @access_tree[controller.to_sym]['@all'].nil?
              @access_tree[controller.to_sym]['@all'] = []
            end

            @access_tree[controller.to_sym]['@all'] << user_action
          else
            actions.each do |action|
              if @access_tree[controller.to_sym][action.to_sym].nil?
                @access_tree[controller.to_sym][action.to_sym] = []
              end

              @access_tree[controller.to_sym][action.to_sym] << user_action
            end
          end
        end
      end

      def access_granted?(controller, action, user_action)
        if @access_tree.nil?
          @access_tree = { }
        end

        controller_grants = @access_tree[controller.to_sym]

        if controller_grants.nil?
          false
        elsif controller_grants['@all'] && controller_grants['@all'].include?(user_action.to_sym)
          true
        elsif controller_grants[action.to_sym].nil?
          false
        else
          controller_grants[action.to_sym].include?(user_action.to_sym)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
access_manager-0.1.1 lib/access_manager/control.rb