Class: AnnotationSecurity::UserWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/annotation_security/user_wrapper.rb

Overview

AnnotationSecurity::UserWrapper

This class is not in use!

Needed for evaluating relations, especially if the :as-option is used.

Merges a user and a role. If a role is given,

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, role = nil) ⇒ UserWrapper

Returns a new instance of UserWrapper.



29
30
31
32
# File 'lib/annotation_security/user_wrapper.rb', line 29

def initialize(user,role=nil)
  @user = user
  @role = role
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

Try to send to role, user and policy of args



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/annotation_security/user_wrapper.rb', line 52

def method_missing(symbol,*args,&block)
  if @role && (@role.respond_to? symbol)
    @role.__send__(symbol,*args,&block)
  elsif @user.respond_to? symbol
    @user.__send__(symbol,*args,&block)
  elsif args.first.respond_to? :policy_for
    args.first.policy_for(@user).__send__(symbol,*args[1..-1])
  else
    # This will raise a NoMethodError
    @user.__send__(symbol,*args,&block)
  end
end

Class Method Details

.all_for_role(user, role_name) ⇒ Object

Return user wrappers for the requested role. The role(s) will be determined with sending user.as_’role’. (Normally a user has a role only once, however it will work when he has many roles of the same kind as well)



19
20
21
22
23
24
25
26
27
# File 'lib/annotation_security/user_wrapper.rb', line 19

def self.all_for_role(user,role_name)
  return [] if user.nil?
  user = user.__user__ if user.is_a? AnnotationSecurity::UserWrapper
  return [new(user)] if role_name.nil?
  roles = user.__send__("as_#{role_name}")
  return [] if roles.blank?
  roles = [roles] unless roles.is_a?(Array)
  roles.compact.collect { |role| new(user,role) }
end

Instance Method Details

#==(obj) ⇒ Object



46
47
48
# File 'lib/annotation_security/user_wrapper.rb', line 46

def ==(obj)
  @user == obj or (!@role.nil? and @role == obj)
end

#__role__Object



42
43
44
# File 'lib/annotation_security/user_wrapper.rb', line 42

def __role__
  @role
end

#__user__Object



38
39
40
# File 'lib/annotation_security/user_wrapper.rb', line 38

def __user__
  @user
end

#idObject



34
35
36
# File 'lib/annotation_security/user_wrapper.rb', line 34

def id
  @role? @role.id : @user.id
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# File 'lib/annotation_security/user_wrapper.rb', line 65

def is_a?(klass)
  return true if super(klass)
  if @role
    @role.is_a?(klass)
  else
    @user.is_a?(klass)
  end
end