]> git.openstreetmap.org Git - rails.git/blob - app/controllers/traces_controller.rb
Show deleted diary entries to administrators, if the user isn't also deleted
[rails.git] / app / controllers / traces_controller.rb
1 class TracesController < ApplicationController
2   layout "site", :except => :georss
3
4   before_action :authorize_web
5   before_action :set_locale
6   before_action :check_database_readable
7
8   authorize_resource
9
10   before_action :check_database_writable, :only => [:new, :create, :edit, :delete]
11   before_action :offline_warning, :only => [:mine, :show]
12   before_action :offline_redirect, :only => [:new, :create, :edit, :delete, :data]
13
14   # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
15   #  target_user - if set, specifies the user to fetch traces for.  if not set will fetch all traces
16   def index
17     # from display name, pick up user id if one user's traces only
18     display_name = params[:display_name]
19     if display_name.present?
20       target_user = User.active.where(:display_name => display_name).first
21       if target_user.nil?
22         render_unknown_user display_name
23         return
24       end
25     end
26
27     # set title
28     @title = if target_user.nil?
29                t ".public_traces"
30              elsif current_user && current_user == target_user
31                t ".my_traces"
32              else
33                t ".public_traces_from", :user => target_user.display_name
34              end
35
36     @title += t ".tagged_with", :tags => params[:tag] if params[:tag]
37
38     # four main cases:
39     # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
40     # 2 - all traces, not logged in = all public traces
41     # 3 - user's traces, logged in as same user = all user's traces
42     # 4 - user's traces, not logged in as that user = all user's public traces
43     @traces = if target_user.nil? # all traces
44                 if current_user
45                   Trace.visible_to(current_user) # 1
46                 else
47                   Trace.visible_to_all # 2
48                 end
49               elsif current_user && current_user == target_user
50                 current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
51               else
52                 target_user.traces.visible_to_all # 4
53               end
54
55     @traces = @traces.tagged(params[:tag]) if params[:tag]
56
57     @params = params.permit(:display_name, :tag)
58
59     @page = (params[:page] || 1).to_i
60     @page_size = 20
61
62     @traces = @traces.visible
63     @traces = @traces.order(:id => :desc)
64     @traces = @traces.offset((@page - 1) * @page_size)
65     @traces = @traces.limit(@page_size)
66     @traces = @traces.includes(:user, :tags)
67
68     # put together SET of tags across traces, for related links
69     tagset = {}
70     @traces.each do |trace|
71       trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
72       trace.tags.each do |tag|
73         tagset[tag.tag] = tag.tag
74       end
75     end
76
77     # final helper vars for view
78     @target_user = target_user
79     @display_name = target_user.display_name if target_user
80     @all_tags = tagset.values
81   end
82
83   def mine
84     redirect_to :action => :index, :display_name => current_user.display_name
85   end
86
87   def show
88     @trace = Trace.find(params[:id])
89
90     if @trace&.visible? &&
91        (@trace&.public? || @trace&.user == current_user)
92       @title = t ".title", :name => @trace.name
93     else
94       flash[:error] = t ".trace_not_found"
95       redirect_to :action => "index"
96     end
97   rescue ActiveRecord::RecordNotFound
98     flash[:error] = t ".trace_not_found"
99     redirect_to :action => "index"
100   end
101
102   def new
103     @title = t ".upload_trace"
104     @trace = Trace.new(:visibility => default_visibility)
105   end
106
107   def create
108     @title = t ".upload_trace"
109
110     logger.info(params[:trace][:gpx_file].class.name)
111
112     if params[:trace][:gpx_file].respond_to?(:read)
113       begin
114         @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
115                            params[:trace][:description], params[:trace][:visibility])
116       rescue StandardError => e
117         logger.debug e
118       end
119
120       if @trace.id
121         flash[:notice] = t ".trace_uploaded"
122         flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
123
124         TraceImporterJob.perform_later(@trace) if Settings.trace_use_job_queue
125         redirect_to :action => :index, :display_name => current_user.display_name
126       else
127         flash[:error] = t("traces.create.upload_failed") if @trace.valid?
128
129         render :action => "new"
130       end
131     else
132       @trace = Trace.new(:name => "Dummy",
133                          :tagstring => params[:trace][:tagstring],
134                          :description => params[:trace][:description],
135                          :visibility => params[:trace][:visibility],
136                          :inserted => false, :user => current_user,
137                          :timestamp => Time.now.getutc)
138       @trace.valid?
139       @trace.errors.add(:gpx_file, "can't be blank")
140
141       render :action => "new"
142     end
143   end
144
145   def data
146     trace = Trace.find(params[:id])
147
148     if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
149       if Acl.no_trace_download(request.remote_ip)
150         head :forbidden
151       elsif request.format == Mime[:xml]
152         send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
153       elsif request.format == Mime[:gpx]
154         send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
155       else
156         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
157       end
158     else
159       head :not_found
160     end
161   rescue ActiveRecord::RecordNotFound
162     head :not_found
163   end
164
165   def edit
166     @trace = Trace.find(params[:id])
167
168     if !@trace.visible?
169       head :not_found
170     elsif current_user.nil? || @trace.user != current_user
171       head :forbidden
172     else
173       @title = t ".title", :name => @trace.name
174     end
175   rescue ActiveRecord::RecordNotFound
176     head :not_found
177   end
178
179   def update
180     @trace = Trace.find(params[:id])
181
182     if !@trace.visible?
183       head :not_found
184     elsif current_user.nil? || @trace.user != current_user
185       head :forbidden
186     elsif @trace.update(trace_params)
187       flash[:notice] = t ".updated"
188       redirect_to :action => "show", :display_name => current_user.display_name
189     else
190       @title = t ".title", :name => @trace.name
191       render :action => "edit"
192     end
193   rescue ActiveRecord::RecordNotFound
194     head :not_found
195   end
196
197   def delete
198     trace = Trace.find(params[:id])
199
200     if !trace.visible?
201       head :not_found
202     elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
203       head :forbidden
204     else
205       trace.visible = false
206       trace.save
207       flash[:notice] = t ".scheduled_for_deletion"
208       TraceDestroyerJob.perform_later(trace) if Settings.trace_use_job_queue
209       redirect_to :action => :index, :display_name => trace.user.display_name
210     end
211   rescue ActiveRecord::RecordNotFound
212     head :not_found
213   end
214
215   def georss
216     @traces = Trace.visible_to_all.visible
217
218     @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
219
220     @traces = @traces.tagged(params[:tag]) if params[:tag]
221     @traces = @traces.order("timestamp DESC")
222     @traces = @traces.limit(20)
223     @traces = @traces.includes(:user)
224   end
225
226   def picture
227     trace = Trace.find(params[:id])
228
229     if trace.visible? && trace.inserted?
230       if trace.public? || (current_user && current_user == trace.user)
231         expires_in 7.days, :private => !trace.public?, :public => trace.public?
232         send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
233       else
234         head :forbidden
235       end
236     else
237       head :not_found
238     end
239   rescue ActiveRecord::RecordNotFound
240     head :not_found
241   end
242
243   def icon
244     trace = Trace.find(params[:id])
245
246     if trace.visible? && trace.inserted?
247       if trace.public? || (current_user && current_user == trace.user)
248         expires_in 7.days, :private => !trace.public?, :public => trace.public?
249         send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
250       else
251         head :forbidden
252       end
253     else
254       head :not_found
255     end
256   rescue ActiveRecord::RecordNotFound
257     head :not_found
258   end
259
260   private
261
262   def do_create(file, tags, description, visibility)
263     # Sanitise the user's filename
264     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
265
266     # Get a temporary filename...
267     filename = "/tmp/#{rand}"
268
269     # ...and save the uploaded file to that location
270     File.open(filename, "wb") { |f| f.write(file.read) }
271
272     # Create the trace object, falsely marked as already
273     # inserted to stop the import daemon trying to load it
274     trace = Trace.new(
275       :name => name,
276       :tagstring => tags,
277       :description => description,
278       :visibility => visibility,
279       :inserted => true,
280       :user => current_user,
281       :timestamp => Time.now.getutc
282     )
283
284     if trace.valid?
285       Trace.transaction do
286         begin
287           # Save the trace object
288           trace.save!
289
290           # Rename the temporary file to the final name
291           FileUtils.mv(filename, trace.trace_name)
292         rescue StandardError
293           # Remove the file as we have failed to update the database
294           FileUtils.rm_f(filename)
295
296           # Pass the exception on
297           raise
298         end
299
300         begin
301           # Clear the inserted flag to make the import daemon load the trace
302           trace.inserted = false
303           trace.save!
304         rescue StandardError
305           # Remove the file as we have failed to update the database
306           FileUtils.rm_f(trace.trace_name)
307
308           # Pass the exception on
309           raise
310         end
311       end
312     end
313
314     # Finally save the user's preferred privacy level
315     if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
316       pref.v = visibility
317       pref.save
318     else
319       current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
320     end
321
322     trace
323   end
324
325   def offline_warning
326     flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
327   end
328
329   def offline_redirect
330     redirect_to :action => :offline if Settings.status == "gpx_offline"
331   end
332
333   def default_visibility
334     visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
335
336     if visibility
337       visibility.v
338     elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
339       "private"
340     else
341       "public"
342     end
343   end
344
345   def trace_params
346     params.require(:trace).permit(:description, :tagstring, :visibility)
347   end
348 end