]> git.openstreetmap.org Git - rails.git/commitdiff
Moved changeset consistency checks to library code.
authorMatt Amos <zerebubuth@gmail.com>
Wed, 26 Nov 2008 12:56:42 +0000 (12:56 +0000)
committerMatt Amos <zerebubuth@gmail.com>
Wed, 26 Nov 2008 12:56:42 +0000 (12:56 +0000)
app/controllers/changeset_controller.rb
lib/consistency_validations.rb

index fad797cdc4a14ff604fc2710560c2d3efb685adb..58bcd1020f15598d21b73ac4c55c7a312a9798ec 100644 (file)
@@ -11,6 +11,9 @@ class ChangesetController < ApplicationController
   # Help methods for checking boundary sanity and area size
   include MapBoundary
 
   # Help methods for checking boundary sanity and area size
   include MapBoundary
 
+  # Helper methods for checking consistency
+  include ConsistencyValidations
+
   # Create a changeset from XML.
   def create
     if request.put?
   # Create a changeset from XML.
   def create
     if request.put?
@@ -46,12 +49,9 @@ class ChangesetController < ApplicationController
       return
     end
     
       return
     end
     
-    changeset = Changeset.find(params[:id])
-    
-    unless @user.id == changeset.user_id 
-      raise OSM::APIUserChangesetMismatchError 
-    end
-    
+    changeset = Changeset.find(params[:id])    
+    check_changeset_consistency(changeset, @user)
+
     # to close the changeset, we'll just set its closed_at time to
     # now. this might not be enough if there are concurrency issues, 
     # but we'll have to wait and see.
     # to close the changeset, we'll just set its closed_at time to
     # now. this might not be enough if there are concurrency issues, 
     # but we'll have to wait and see.
@@ -75,12 +75,7 @@ class ChangesetController < ApplicationController
     # idempotent, there is no "document" to PUT really...
     if request.post?
       cs = Changeset.find(params[:id])
     # idempotent, there is no "document" to PUT really...
     if request.post?
       cs = Changeset.find(params[:id])
-
-      # check user credentials - only the user who opened a changeset
-      # may alter it.
-      unless @user.id == cs.user_id 
-        raise OSM::APIUserChangesetMismatchError 
-      end
+      check_changeset_consistency(cs, @user)
 
       # keep an array of lons and lats
       lon = Array.new
 
       # keep an array of lons and lats
       lon = Array.new
@@ -142,12 +137,7 @@ class ChangesetController < ApplicationController
     end
 
     changeset = Changeset.find(params[:id])
     end
 
     changeset = Changeset.find(params[:id])
-
-    # access control - only the user who created a changeset may
-    # upload to it.
-    unless @user.id == changeset.user_id 
-      raise OSM::APIUserChangesetMismatchError 
-    end
+    check_changeset_consistency(changeset, @user)
     
     diff_reader = DiffReader.new(request.raw_post, changeset)
     Changeset.transaction do
     
     diff_reader = DiffReader.new(request.raw_post, changeset)
     Changeset.transaction do
@@ -281,6 +271,7 @@ class ChangesetController < ApplicationController
     new_changeset = Changeset.from_xml(request.raw_post)
 
     unless new_changeset.nil?
     new_changeset = Changeset.from_xml(request.raw_post)
 
     unless new_changeset.nil?
+      check_changeset_consistency(changeset, @user)
       changeset.update_from(new_changeset, @user)
       render :text => changeset.to_xml, :mime_type => "text/xml"
     else
       changeset.update_from(new_changeset, @user)
       render :text => changeset.to_xml, :mime_type => "text/xml"
     else
index 46fb3c06e2ad771a9fd00db2ab618d4e0c453b80..4f3881542e39afcadf490c089669a2369f692b57 100644 (file)
@@ -27,4 +27,19 @@ module ConsistencyValidations
       raise OSM::APIChangesetAlreadyClosedError.new(new.changeset)
     end
   end
       raise OSM::APIChangesetAlreadyClosedError.new(new.changeset)
     end
   end
+
+  ##
+  # subset of consistency checks which should be applied to almost
+  # all the changeset controller's writable methods.
+  def check_changeset_consistency(changeset, user)
+    # check user credentials - only the user who opened a changeset
+    # may alter it.
+    if changeset.nil?
+      raise OSM::APIChangesetMissingError.new
+    elsif user.id != changeset.user_id 
+      raise OSM::APIUserChangesetMismatchError.new
+    elsif not changeset.is_open?
+      raise OSM::APIChangesetAlreadyClosedError.new(changeset)
+    end
+  end
 end
 end