Sha256: a3dba5a93d64c854de7f929152cb1e670d68b687c2caf6a4902d9298b6325ff3

Contents?: true

Size: 1.85 KB

Versions: 24

Compression:

Stored size: 1.85 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
        unless yml.is_a? Hash
          raise("#{filename} was found, but was blank or malformed.\n")
        end

        yml.fetch(Rails.env)

      end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
hydra-access-controls-10.3.4 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.3.3 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.3.2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.3.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.2.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.1.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.0.2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.0.1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.0.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.0.0.beta4 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.0.0.beta3 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.0.0.beta2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-10.0.0.beta1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.10.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.9.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.8.1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.8.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.7.1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.7.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-9.6.0 lib/hydra/role_mapper_behavior.rb