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-6.0.0.pre4 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.0.0.pre3 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.0.0.pre2 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-6.0.0.pre1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.4.0.pre1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.3.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.2.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.1.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0.rc1 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0.pre15 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0.pre14 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0.pre13 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0.pre12 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0.pre11 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0.pre10 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0.pre9 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0.pre8 lib/hydra/role_mapper_behavior.rb
hydra-access-controls-5.0.0.pre7 lib/hydra/role_mapper_behavior.rb