Modified: trunk/app/controllers/api_controller.rb (2113 => 2114)
--- trunk/app/controllers/api_controller.rb 2009-03-05 12:15:46 UTC (rev 2113)
+++ trunk/app/controllers/api_controller.rb 2009-03-06 15:29:04 UTC (rev 2114)
@@ -13,13 +13,15 @@
def process_request
+ user = current_user
+
auth = request.env["HTTP_AUTHORIZATION"]
if auth and auth.starts_with?("Basic ")
credentials = Base64.decode64(auth.sub(/^Basic /, '')).split(':')
- current_user = User.authenticate(credentials[0], credentials[1])
+ user = User.authenticate(credentials[0], credentials[1])
- if current_user.nil?
+ if user.nil?
render :xml => rest_error_response(401, 'Not authorized').to_s
return
end
@@ -31,7 +33,7 @@
uri = params[:uri]
# logger.info "current token: #{current_token.inspect}"
- # logger.info "current user: #{current_user.id}"
+ # logger.info "current user: #{user.id}"
# logger.info "query: #{query}"
# logger.info "method: #{method}"
# logger.info "uri: #{uri}"
@@ -59,13 +61,12 @@
end
case rules['Type']
- when 'index'; doc = rest_index_request(rules, current_user, query)
- when 'crud'; doc = rest_crud_request(rules, current_user)
- when 'call'; doc = rest_call_request(rules, current_user, query)
+ when 'index'; doc = rest_index_request(rules, user, query)
+ when 'crud'; doc = rest_crud_request(rules, user)
+ when 'call'; doc = rest_call_request(rules, user, query)
else; bad_rest_request
end
- current_user = nil
current_token = nil
render :xml => doc.to_s
end
Modified: trunk/lib/rest.rb (2113 => 2114)
--- trunk/lib/rest.rb 2009-03-05 12:15:46 UTC (rev 2113)
+++ trunk/lib/rest.rb 2009-03-06 15:29:04 UTC (rev 2114)
@@ -210,7 +210,7 @@
doc
end
-def rest_error_response(code, message)
+def rest_error_response(code, message, error_ob = nil)
error = XML::Node.new('error')
error["code" ] = code.to_s
@@ -219,6 +219,14 @@
doc = XML::Document.new
doc.root = error
+ if error_ob
+ error_ob.errors.full_messages.each do |message|
+ reason = XML::Node.new('reason')
+ reason << message
+ doc.root << reason
+ end
+ end
+
doc
end
@@ -503,45 +511,27 @@
end
def create_default_policy(user)
- Policy.new(:name => 'auto', :update_mode => 6, :share_mode => 0,
- :view_public => true, :view_protected => false,
- :download_public => true, :download_protected => false,
- :edit_public => false, :edit_protected => false,
- :contributor => user)
+ Policy.new(:contributor => user, :name => 'auto', :update_mode => 6, :share_mode => 0)
end
def post_workflow(rules, user, query)
return rest_error_response(400, 'Bad Request') if user.nil?
+ return rest_error_response(400, 'Bad Request') if params["workflow"].nil?
- title = params["workflow"]["title"]
- description = params["workflow"]["description"]
- license_type = params["workflow"]["license_type"]
- content_type = params["workflow"]["content_type"]
- content = params["workflow"]["content"]
+ elements = params["workflow"]
- return rest_error_response(400, 'Bad Request') if title.nil?
- return rest_error_response(400, 'Bad Request') if description.nil?
- return rest_error_response(400, 'Bad Request') if license_type.nil?
- return rest_error_response(400, 'Bad Request') if content_type.nil?
- return rest_error_response(400, 'Bad Request') if content.nil?
-
- content = Base64.decode64(content)
+ # build the contributable
- contribution = Contribution.new(
- :contributor_type => 'User',
- :contributor_id => user.id)
+ workflow = Workflow.new(:contributor => user)
- workflow = Workflow.new(
- :title => title,
- :body => description,
- :license => license_type,
- :content_type => content_type,
- :content_blob => ContentBlob.new(:data ="" content),
- :contributor_type => 'User',
- :contributor_id => user.id,
- :contribution => contribution)
+ workflow.title = elements["title"] if elements["title"]
+ workflow.body = elements["description"] if elements["description"]
+ workflow.license = elements["license_type"] if elements["license_type"]
+ workflow.content_type = elements["content_type"] if elements["content_type"]
+ workflow.content_blob = ContentBlob.new(:data ="" Base64.decode64(elements["content"])) if elements["content"]
+
# Handle the preview and svg images. If there's a preview supplied, use it.
# Otherwise auto-generate one if we can.
@@ -558,7 +548,7 @@
image.close
- elsif workflow.processor_class.can_generate_preview?
+ elsif workflow.processor_class and workflow.processor_class.can_generate_preview?
processor = workflow.processor_class.new(content)
workflow.image, workflow.svg = processor.get_preview_images
@@ -568,7 +558,7 @@
workflow.set_unique_name
if not workflow.save
- return rest_error_response(400, 'Bad Request')
+ return rest_error_response(400, 'Bad Request', workflow)
end
workflow.contribution.policy = create_default_policy(user)