module Hydra::RoleMapperBehavior::ClassMethods

Public Instance Methods

byname() click to toggle source
# File lib/hydra/role_mapper_behavior.rb, line 37
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
fetch_groups(user:) click to toggle source
# File lib/hydra/role_mapper_behavior.rb, line 10
def fetch_groups(user:)
  _groups(user.user_key)
end
map() click to toggle source
# File lib/hydra/role_mapper_behavior.rb, line 32
def map
  @map ||= load_role_map
end
role_names() click to toggle source
# File lib/hydra/role_mapper_behavior.rb, line 6
def role_names
  map.keys
end
roles(user_or_uid) click to toggle source

@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

# File lib/hydra/role_mapper_behavior.rb, line 17
def roles(user_or_uid)
  Deprecation.warn(self, "roles is deprecated and will be removed in Hydra-Head 11.  Use fetch_groups instead")
  user_id = case user_or_uid
              when String
                user_or_uid
              else
                user_or_uid.user_key
            end
  _groups(user_id)
end
whois(r) click to toggle source
# File lib/hydra/role_mapper_behavior.rb, line 28
def whois(r)
  map[r] || []
end

Private Instance Methods

_groups(user_id) click to toggle source

@param user_id [String] the identfying user key @return [Array<String>] a list of group names. If a nil user id, or a user id that doesn’t exist is passed in, it will return an empty array

# File lib/hydra/role_mapper_behavior.rb, line 48
def _groups(user_id)
  byname[user_id].dup || []
end
load_role_map() click to toggle source
# File lib/hydra/role_mapper_behavior.rb, line 52
def load_role_map
  require 'erb'
  require 'yaml'

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

  unless File.exist?(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 = if Psych::VERSION > '4.0'
      YAML.safe_load(erb, aliases: true)
    else
      YAML.safe_load(erb, [], [], true)
    end
  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

  roles = yml.fetch(Rails.env)
  raise "No roles were found for the #{Rails.env} environment in #{file}" unless roles
  roles
end