]> git.openstreetmap.org Git - rails.git/blob - app/abilities/api_capability.rb
Merge remote-tracking branch 'upstream/pull/3297'
[rails.git] / app / abilities / api_capability.rb
1 # frozen_string_literal: true
2
3 class ApiCapability
4   include CanCan::Ability
5
6   def initialize(token)
7     if Settings.status != "database_offline"
8       user = if token.respond_to?(:resource_owner_id)
9                User.find(token.resource_owner_id)
10              elsif token.respond_to?(:user)
11                token.user
12              end
13
14       can [:create, :comment, :close, :reopen], Note if scope?(token, :write_notes)
15       can [:show, :data], Trace if scope?(token, :read_gpx)
16       can [:create, :update, :destroy], Trace if scope?(token, :write_gpx)
17       can [:details], User if scope?(token, :read_prefs)
18       can [:gpx_files], User if scope?(token, :read_gpx)
19       can [:index, :show], UserPreference if scope?(token, :read_prefs)
20       can [:update, :update_all, :destroy], UserPreference if scope?(token, :write_prefs)
21
22       if user&.terms_agreed?
23         can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset if scope?(token, :write_api)
24         can :create, ChangesetComment if scope?(token, :write_api)
25         can [:create, :update, :delete], Node if scope?(token, :write_api)
26         can [:create, :update, :delete], Way if scope?(token, :write_api)
27         can [:create, :update, :delete], Relation if scope?(token, :write_api)
28       end
29
30       if user&.moderator?
31         can [:destroy, :restore], ChangesetComment if scope?(token, :write_api)
32         can :destroy, Note if scope?(token, :write_notes)
33         if user&.terms_agreed?
34           can :redact, OldNode if scope?(token, :write_api)
35           can :redact, OldWay if scope?(token, :write_api)
36           can :redact, OldRelation if scope?(token, :write_api)
37         end
38       end
39     end
40   end
41
42   private
43
44   def scope?(token, scope)
45     token&.includes_scope?(scope)
46   end
47 end