]> git.openstreetmap.org Git - rails.git/blob - app/controllers/old_controller.rb
Merge pull request #1151 from polarbearing/patch-1
[rails.git] / app / controllers / old_controller.rb
1 # this class pulls together the logic for all the old_* controllers
2 # into one place. as it turns out, the API methods for historical
3 # nodes, ways and relations are basically identical.
4 class OldController < ApplicationController
5   require "xml/libxml"
6
7   skip_before_action :verify_authenticity_token
8   before_action :setup_user_auth, :only => [:history, :version]
9   before_action :api_deny_access_handler
10   before_action :authorize, :only => [:redact]
11
12   authorize_resource
13
14   before_action :check_api_readable
15   before_action :check_api_writable, :only => [:redact]
16   around_action :api_call_handle_error, :api_call_timeout
17   before_action :lookup_old_element, :except => [:history]
18   before_action :lookup_old_element_versions, :only => [:history]
19
20   def history
21     # the .where() method used in the lookup_old_element_versions
22     # call won't throw an error if no records are found, so we have
23     # to do that ourselves.
24     raise OSM::APINotFoundError if @elements.empty?
25
26     doc = OSM::API.new.get_xml_doc
27
28     visible_elements = if show_redactions?
29                          @elements
30                        else
31                          @elements.unredacted
32                        end
33
34     visible_elements.each do |element|
35       doc.root << element.to_xml_node
36     end
37
38     render :xml => doc.to_s
39   end
40
41   def version
42     if @old_element.redacted? && !show_redactions?
43       head :forbidden
44
45     else
46       response.last_modified = @old_element.timestamp
47
48       doc = OSM::API.new.get_xml_doc
49       doc.root << @old_element.to_xml_node
50
51       render :xml => doc.to_s
52     end
53   end
54
55   def redact
56     redaction_id = params["redaction"]
57     if redaction_id.nil?
58       # if no redaction ID was provided, then this is an unredact
59       # operation.
60       @old_element.redact!(nil)
61     else
62       # if a redaction ID was specified, then set this element to
63       # be redacted in that redaction.
64       redaction = Redaction.find(redaction_id.to_i)
65       @old_element.redact!(redaction)
66     end
67
68     # just return an empty 200 OK for success
69     head :ok
70   end
71
72   private
73
74   def show_redactions?
75     current_user&.moderator? && params[:show_redactions] == "true"
76   end
77 end