Sha256: da63fc9a15319a06f7f88d16b66dbba809c75c4eab97f331cb3cf932e5397da3

Contents?: true

Size: 1.35 KB

Versions: 61

Compression:

Stored size: 1.35 KB

Contents

# this code will be moved/renamed to Hydra::AccessControl::RoleMapperBehavior (with the appropriate namespace changes) in Hydra 5.0
require 'yaml'
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 ||= YAML.load(File.open(File.join(Rails.root, "config/role_map_#{Rails.env}.yml")))
    end


    def byname
      return @byname if @byname
      m = Hash.new{|h,k| h[k]=[]}
      @byname = map.inject(m) do|memo, (role,usernames)| 
        ((usernames if usernames.respond_to?(:each)) || [usernames]).each { |x| memo[x]<<role}
        memo
      end
    end
    
  end
end

Version data entries

61 entries across 61 versions & 1 rubygems

Version Path
hydra-access-controls-5.4.1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.5.2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.5.1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.4.2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-7.0.0.rc1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-7.0.0.pre3 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.5.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.5.0.rc2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-7.0.0.pre2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-7.0.0.pre1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.4.1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.4.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.4.0.rc5 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.4.0.rc4 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.4.0.rc3 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.4.0.rc2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.4.0.rc1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.4.0.pre2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.4.0.pre1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.3.4 lib/hydra/role_mapper_behavior.rb