]> git.openstreetmap.org Git - rails.git/blob - app/controllers/old_way_controller.rb
Added redactions controller test and fixed a bug in the controller
[rails.git] / app / controllers / old_way_controller.rb
1 class OldWayController < ApplicationController
2   require 'xml/libxml'
3
4   skip_before_filter :verify_authenticity_token
5   before_filter :setup_user_auth, :only => [ :history, :version ]
6   before_filter :authorize, :only => [ :redact ]
7   before_filter :authorize_moderator, :only => [ :redact ]
8   before_filter :require_allow_write_api, :only => [ :redact ]
9   before_filter :check_api_readable
10   before_filter :check_api_writable, :only => [ :redact ]
11   before_filter :lookup_old_way, :except => [ :history ]
12   after_filter :compress_output
13   around_filter :api_call_handle_error, :api_call_timeout
14
15   def history
16     way = Way.find(params[:id].to_i)
17     
18     doc = OSM::API.new.get_xml_doc
19     
20     visible_ways = if @user and @user.moderator? and params[:show_redactions] == "true"
21                      way.old_ways
22                    else
23                      way.old_ways.unredacted
24                    end
25     
26     visible_ways.each do |old_way|
27       doc.root << old_way.to_xml_node
28     end
29     
30     render :text => doc.to_s, :content_type => "text/xml"
31   end
32   
33   def version
34     if @old_way.redacted? and not (@user and @user.moderator? and params[:show_redactions] == "true")
35       render :nothing => true, :status => :forbidden
36     else
37
38       response.last_modified = @old_way.timestamp
39       
40       doc = OSM::API.new.get_xml_doc
41       doc.root << @old_way.to_xml_node
42         
43       render :text => doc.to_s, :content_type => "text/xml"
44     end
45   end
46
47   def redact
48     redaction_id = params['redaction']
49     unless redaction_id.nil?
50       # if a redaction ID was specified, then set this way to
51       # be redacted in that redaction. (TODO: check that the
52       # user doing the redaction owns the redaction object too)
53       redaction = Redaction.find(redaction_id.to_i)
54       @old_way.redact!(redaction)
55       
56     else
57       # if no redaction ID was provided, then this is an unredact
58       # operation.
59       @old_way.redact!(nil)
60     end
61     
62     # just return an empty 200 OK for success
63     render :nothing => true
64   end
65
66   private
67   
68   def lookup_old_way
69     @old_way = OldWay.where(:way_id => params[:id], :version => params[:version]).first
70     if @old_way.nil?
71       render :nothing => true, :status => :not_found
72       return false
73     end
74   end
75 end