]> git.openstreetmap.org Git - rails.git/blob - app/controllers/node_controller.rb
prevent update of any object without providing the correct/current
[rails.git] / app / controllers / node_controller.rb
1 # The NodeController is the RESTful interface to Node objects
2
3 class NodeController < ApplicationController
4   require 'xml/libxml'
5
6   session :off
7   before_filter :authorize, :only => [:create, :update, :delete]
8   before_filter :check_write_availability, :only => [:create, :update, :delete]
9   before_filter :check_read_availability, :except => [:create, :update, :delete]
10   after_filter :compress_output
11
12   # Create a node from XML.
13   def create
14     if request.put?
15       node = Node.from_xml(request.raw_post, true)
16
17       if node
18         node.version = 0
19         node.user_id = @user.id
20         node.visible = true
21         node.save_with_history!
22
23         render :text => node.id.to_s, :content_type => "text/plain"
24       else
25         render :nothing => true, :status => :bad_request
26       end
27     else
28       render :nothing => true, :status => :method_not_allowed
29     end
30   end
31
32   # Dump the details on a node given in params[:id]
33   def read
34     begin
35       node = Node.find(params[:id])
36       if node.visible
37         response.headers['Last-Modified'] = node.timestamp.rfc822
38         render :text => node.to_xml.to_s, :content_type => "text/xml"
39        else
40         render :text => "", :status => :gone
41       end
42     rescue ActiveRecord::RecordNotFound
43       render :nothing => true, :status => :not_found
44     end
45   end
46
47   # Update a node from given XML
48   def update
49     begin
50       node = Node.find(params[:id])
51       new_node = Node.from_xml(request.raw_post)
52       if new_node.version != node.version
53         render :text => "Version mismatch: Provided " + new_node.version.to_s + ", server had: " + node.version.to_s, :status => :bad_request
54         return
55       end  
56
57       if new_node and new_node.id == node.id
58         node.update_from(new_node, @user)
59         render :text => node.version.to_s, :content_type => "text/plain"
60       else
61         render :nothing => true, :status => :bad_request
62       end
63     rescue ActiveRecord::RecordNotFound
64       render :nothing => true, :status => :not_found
65     end
66   end
67
68   # Delete a node. Doesn't actually delete it, but retains its history in a wiki-like way.
69   # FIXME remove all the fricking SQL
70   def delete
71     begin
72       node = Node.find(params[:id])
73       node.delete_with_history(@user)
74     rescue ActiveRecord::RecordNotFound
75       render :nothing => true, :status => :not_found
76     rescue OSM::APIAlreadyDeletedError
77       render :text => "", :status => :gone
78     rescue OSM::APIPreconditionFailedError
79       render :text => "", :status => :precondition_failed
80     end
81   end
82
83   # WTF does this do?
84   def nodes
85     ids = params['nodes'].split(',').collect { |n| n.to_i }
86
87     if ids.length > 0
88       doc = OSM::API.new.get_xml_doc
89
90       Node.find(ids).each do |node|
91         doc.root << node.to_xml_node
92       end 
93
94       render :text => doc.to_s, :content_type => "text/xml"
95     else
96       render :nothing => true, :status => :bad_request
97     end
98   end
99 end