]> git.openstreetmap.org Git - rails.git/blob - lib/consistency_validations.rb
pass common API error text through to Potlatch so it can be shown to the user
[rails.git] / lib / consistency_validations.rb
1 module ConsistencyValidations
2   # Generic checks that are run for the updates and deletes of
3   # node, ways and relations. This code is here to avoid duplication, 
4   # and allow the extention of the checks without having to modify the
5   # code in 6 places for all the updates and deletes. Some of these tests are 
6   # needed for creates, but are currently not run :-( 
7   # This will throw an exception if there is an inconsistency
8   def check_consistency(old, new, user)
9     if new.version != old.version
10       raise OSM::APIVersionMismatchError.new(new.id, new.class.to_s, new.version, old.version)
11     elsif new.changeset.nil?
12       raise OSM::APIChangesetMissingError.new
13     elsif new.changeset.user_id != user.id
14       raise OSM::APIUserChangesetMismatchError.new
15     elsif not new.changeset.is_open?
16       raise OSM::APIChangesetAlreadyClosedError.new(new.changeset)
17     end
18   end
19   
20   # This is similar to above, just some validations don't apply
21   def check_create_consistency(new, user)
22     if new.changeset.nil?
23       raise OSM::APIChangesetMissingError.new
24     elsif new.changeset.user_id != user.id
25       raise OSM::APIUserChangesetMismatchError.new
26     elsif not new.changeset.is_open?
27       raise OSM::APIChangesetAlreadyClosedError.new(new.changeset)
28     end
29   end
30
31   ##
32   # subset of consistency checks which should be applied to almost
33   # all the changeset controller's writable methods.
34   def check_changeset_consistency(changeset, user)
35     # check user credentials - only the user who opened a changeset
36     # may alter it.
37     if changeset.nil?
38       raise OSM::APIChangesetMissingError.new
39     elsif user.id != changeset.user_id 
40       raise OSM::APIUserChangesetMismatchError.new
41     elsif not changeset.is_open?
42       raise OSM::APIChangesetAlreadyClosedError.new(changeset)
43     end
44   end
45 end