Sha256: 205e7cab70c0528173a58827902fbbb764fc1f8c910898f1e8ab49aa718d0d17

Contents?: true

Size: 1.83 KB

Versions: 29

Compression:

Stored size: 1.83 KB

Contents

module Hydra::RoleMapperBehavior
  extend ActiveSupport::Concern

  module ClassMethods
    def role_names
      map.keys
    end
    
    # 
    # @param user_or_uid either the User object or user id
    # If you pass in a nil User object (ie. user isn't logged in), or a uid that doesn't exist, it will return an empty array
    def roles(user_or_uid)
      if user_or_uid.kind_of?(String)
        user = Hydra::Ability.user_class.find_by_user_key(user_or_uid)
        user_id = user_or_uid
      elsif user_or_uid.kind_of?(Hydra::Ability.user_class) && user_or_uid.user_key   
        user = user_or_uid
        user_id = user.user_key
      end
      array = byname[user_id].dup || []
      array = array << 'registered' unless (user.nil? || user.new_record?) 
      array
    end
    
    def whois(r)
      map[r] || []
    end

    def map
      @map ||= load_role_map
    end


    def byname
      @byname ||= map.each_with_object(Hash.new{ |h,k| h[k] = [] }) do |(role, usernames), memo| 
        Array(usernames).each { |x| memo[x] << role}
      end
    end

    private
      def load_role_map
        require 'erb'
        require 'yaml'

        filename = 'config/role_map.yml'
        file = File.join(Rails.root, filename)

        unless File.exists?(file)
          raise "You are missing a role map configuration file: #{filename}. Have you run \"rails generate hydra:head\"?"
        end

        begin
          erb = ERB.new(IO.read(file)).result(binding)
        rescue
          raise("#{file} was found, but could not be parsed with ERB. \n#{$!.inspect}")
        end

        begin
          yml = YAML::load(erb)
        rescue
          raise("#{filename} was found, but could not be parsed.\n")
        end
        return yml[Rails.env] if yml.is_a? Hash

        raise("#{filename} was found, but was blank or malformed.\n")
      end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
hydra-access-controls-8.2.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.2.2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.2.1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.2.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.2.0.rc1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.1.4 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.1.3 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-8.1.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.1.2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.1.1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.1.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-8.0.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.0.1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.0.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.0.0.rc3 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.0.0.rc2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.0.0.rc1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.0.0.beta2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.0.0.beta1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-8.0.0.beta1 lib/hydra/role_mapper_behavior.rb