]> git.openstreetmap.org Git - rails.git/blob - app/abilities/capability.rb
More resourceful routing for nodes, ways, relations and changesets controllers
[rails.git] / app / abilities / capability.rb
1 # frozen_string_literal: true
2
3 class Capability
4   include CanCan::Ability
5
6   def initialize(token)
7     can [:create, :comment, :close, :reopen], Note if capability?(token, :allow_write_notes)
8     can [:api_read, :api_data], Trace if capability?(token, :allow_read_gpx)
9     can [:api_create, :api_update, :api_delete], Trace if capability?(token, :allow_write_gpx)
10     can [:api_details], User if capability?(token, :allow_read_prefs)
11     can [:api_gpx_files], User if capability?(token, :allow_read_gpx)
12     can [:read, :read_one], UserPreference if capability?(token, :allow_read_prefs)
13     can [:update, :update_one, :delete_one], UserPreference if capability?(token, :allow_write_prefs)
14
15     if token&.user&.terms_agreed? || !REQUIRE_TERMS_AGREED
16       can [:create, :update, :upload, :close, :subscribe, :unsubscribe, :expand_bbox], Changeset if capability?(token, :allow_write_api)
17       can :create, ChangesetComment if capability?(token, :allow_write_api)
18       can [:create, :update, :delete], Node if capability?(token, :allow_write_api)
19       can [:create, :update, :delete], Way if capability?(token, :allow_write_api)
20       can [:create, :update, :delete], Relation if capability?(token, :allow_write_api)
21     end
22
23     if token&.user&.moderator?
24       can [:destroy, :restore], ChangesetComment if capability?(token, :allow_write_api)
25       can :destroy, Note if capability?(token, :allow_write_notes)
26       if token&.user&.terms_agreed? || !REQUIRE_TERMS_AGREED
27         can :redact, OldNode if capability?(token, :allow_write_api)
28         can :redact, OldWay if capability?(token, :allow_write_api)
29         can :redact, OldRelation if capability?(token, :allow_write_api)
30       end
31     end
32   end
33
34   private
35
36   def capability?(token, cap)
37     token&.read_attribute(cap)
38   end
39 end