Class: RubyRest::SecureApplication

Inherits:
SimpleApplication show all
Defined in:
lib/rubyrest/application.rb

Overview

Specialization of a Simple application, that introduces the notion of security services and principals. It overrides the retrieve, create, update and delete methods and adds some extras like automatic binding.

Direct Known Subclasses

SequelApplication

Instance Attribute Summary

Attributes inherited from AbstractApplication

#config

Instance Method Summary collapse

Methods inherited from AbstractApplication

#formatter, #init_database, #is_a_collection, #is_a_service_doc, #register_domain, #register_formatters, #register_resource, #render_model, #resource_by_domain, #resource_by_name, #resource_by_path, #setup, #to_domain_class, #to_resource_class, #to_s

Constructor Details

#initialize(config) ⇒ SecureApplication

Builds a new secured application and register its security service



179
180
181
182
# File 'lib/rubyrest/application.rb', line 179

def initialize( config )
  super( config )
  register_security
end

Instance Method Details

#auto_bindObject

Defines whether update and create operations should automatically load and create new domain model, and automatically bind values from the request body document. Can be disabled in subclasses.



175
# File 'lib/rubyrest/application.rb', line 175

def auto_bind; true end

#create(params) ⇒ Object

Invoked by the web layer, on a POST request. This method delegates to the resource’s service and provides a fresh new model object populated with the data found in the request body.



227
228
229
230
231
232
233
# File 'lib/rubyrest/application.rb', line 227

def create( params )
  params = resolve_principal( params )
  res = resource_by_path( params[:path] )
  params[:body] = res.bind( res.domain.create( params[:principal] ), params[:body] ) if auto_bind == true
  object = res.domain.save_new( params[:body], params[:principal] )
  render_model( params, object )
end

#delete(params) ⇒ Object

Invoked by the web layer, on a DELETE request



247
248
249
250
251
# File 'lib/rubyrest/application.rb', line 247

def delete( params )
  params = resolve_principal( params )
  res = resource_by_path( params[:path] )
  res.domain.delete( res.domain.single( params[:target], params[:principal] ), params[:principal] )
end

#register_securityObject

Register the security service, if specified in the application configuration hash



186
187
188
189
190
# File 'lib/rubyrest/application.rb', line 186

def register_security
  raise "no security service was defined in application #{self}" if !@config[:security_service]
  client_class = Class.by_name( (@config[:security_module]||@config[:module]).to_s.capitalize + "::" + @config[:security_service].to_s.capitalize + "::Client" )
  @security = client_class.new( @config[:security_host]||"localhost", @config[:security_port] )
end

#resolve_principal(params) ⇒ Object

Resolves the principal, by inoking the security service



198
199
200
201
202
203
# File 'lib/rubyrest/application.rb', line 198

def resolve_principal( params )
  principal = @security.principal( params[:authkey] ) 
  raise "No principal was found for authentication key: #{params[:authkey]}" if !principal
  params[:principal]=principal
  return params
end

#retrieve(params) ⇒ Object

Invoked by the web layer, on a GET request. Retrieves the collection or resource, and formats the result as a feed, entry or service document



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rubyrest/application.rb', line 208

def retrieve( params )
  params = resolve_principal( params )
  res = resource_by_path( params[:path] )
  if params[:target] == nil
    objects = res.domain.list( params[:principal] )
  else
    if params[:property] == nil
      objects = res.domain.single( params[:target], params[:principal] )
    else
      objects = res.domain.list_related( params[:target], params[:property], params[:principal] )
    end
  end
  render_model( params, objects )
end

#securityObject

Returns the security service, of nil if not configured



193
194
195
# File 'lib/rubyrest/application.rb', line 193

def security
  @security
end

#update(params) ⇒ Object

Invoked by the web layer, on a PUT request This method delegates to the resource’s service and provides an existing model object, loaded and populated with the data found in the request body.



238
239
240
241
242
243
244
# File 'lib/rubyrest/application.rb', line 238

def update( params )
  params = resolve_principal( params )
  res = resource_by_path( params[:path] )
  params[:body] = res.bind( res.domain.single( params[:target], params[:principal] ), params[:body] ) if auto_bind == true
  object = res.domain.save_existing( params[:target], params[:body], params[:principal] )
  render_model( params, object )
end