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