Module: Auth::Concerns::TokenConcern

Instance Method Summary collapse

Instance Method Details

#add_owner_and_signed_in_resource(obj, options = {}) ⇒ Object

@param obj: the object whose owner is to be defined. @param options: possible options include: :owner_is_current_resource => if this option exists, the resource_id and resource_class is set to the current resource



176
177
178
179
180
# File 'app/controllers/auth/concerns/token_concern.rb', line 176

def add_owner_and_signed_in_resource(obj,options={})
  obj = add_owner_resource(obj,options)
  obj = add_signed_in_resource(obj,options)
  obj
end

#add_owner_resource(obj, options = {}) ⇒ Object

only adds the owner resource if its not already present, implying that once the owner resource is set, it should never change.



160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/auth/concerns/token_concern.rb', line 160

def add_owner_resource(obj,options={})
    if (obj.respond_to? :resource_id) && (obj.respond_to? :resource_class)
      if options[:owner_is_current_resource]
        obj.resource_id = current_signed_in_resource.id.to_s if obj.resource_id.nil?
        obj.resource_class = current_signed_in_resource.class.name.to_s if obj.resource_class.nil?
      else
        obj.resource_id = lookup_resource.id.to_s if obj.resource_id.nil?
        obj.resource_class = lookup_resource.class.name.to_s if obj.resource_class.nil?
      end
    end
    return obj
end

#add_signed_in_resource(obj, options = {}) ⇒ Object

convenience method to add the current signed in resource to the model instance. the object instance passed in MUST implement the owner concern @param : instance of any object that implements the OwnerConcern.

Returns:

  • : the object passed in.



152
153
154
155
156
157
# File 'app/controllers/auth/concerns/token_concern.rb', line 152

def add_signed_in_resource(obj,options={})
      if obj.respond_to? :signed_in_resource
        obj.signed_in_resource = current_signed_in_resource
      end
      return obj
end

#current_signed_in_resourceObject

the current signed in resource.



143
144
145
# File 'app/controllers/auth/concerns/token_concern.rb', line 143

def current_signed_in_resource
  @resource
end

#is_admin_userObject

this is used as a before_filter.



183
184
185
# File 'app/controllers/auth/concerns/token_concern.rb', line 183

def is_admin_user
  not_found("You don't have sufficient privileges to complete that action") if !current_signed_in_resource.is_admin?
end

#lookup_resourceObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/auth/concerns/token_concern.rb', line 113

def lookup_resource 
  ## if the current signed in resource si not an admin, just return it, because the concept of proxy arises only if the current_signed in resource is an admin.
  return current_signed_in_resource unless current_signed_in_resource.is_admin?
  
  ## else.
  
  ## first check the session or the params for a proxy resource.
  proxy_resource_id = params[:proxy_resource_id] || session[:proxy_resource_id]
  proxy_resource_class = params[:proxy_resource_class] || session[:proxy_resource_class]
  
  ## if these are not provided or set, and if the resource is an admin, then the admin becomes the proxy_resource
  proxy_resource_id = current_signed_in_resource.id.to_s if (current_signed_in_resource.is_admin? && proxy_resource_id.nil?)

  proxy_resource_class = current_signed_in_resource.class.to_s if (current_signed_in_resource.is_admin? && proxy_resource_class.nil?)

  ## now return nil if the proxy resource is still nil.
  return nil unless (proxy_resource_class && proxy_resource_id)
  return nil unless (Auth.configuration.auth_resources.include? proxy_resource_class.capitalize)

  proxy_resource_class = proxy_resource_class.capitalize.constantize
  begin
    proxy_resource = proxy_resource_class.find(proxy_resource_id)
    proxy_resource
  rescue Mongoid::Errors::DocumentNotFound => error
    nil
  end
  
end

#set_resourceObject

iterates all the authentication resources in the config. tries to see if we have a current_resource for any of them if yes, sets the resource to the first encoutered such key and breaks the iteration basically a convenience method to set @resource variable, since when we have more than one model that is being authenticated with Devise, there is no way to know which one to call.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/auth/concerns/token_concern.rb', line 92

def set_resource
  

  Auth.configuration.auth_resources.keys.each do |resource|
    break if @resource = self.send("current_#{resource.downcase}") 
  end

  #puts "do we have a resource"
  #puts @resource.to_s

  ## devise in registrations_controller#destroy assumes the existence of an 'resource' variable, so we set that here.
  if devise_controller?
    self.resource = @resource
  end

  puts "we have a resource as: #{@resource}"
  
end