]> git.openstreetmap.org Git - rails.git/blob - app/abilities/api_capability.rb
Test failures on issues pagination
[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       if user&.active?
15         can [:create, :comment, :close, :reopen], Note if scope?(token, :write_notes)
16         can [:show, :data], Trace if scope?(token, :read_gpx)
17         can [:create, :update, :destroy], Trace if scope?(token, :write_gpx)
18         can [:details], User if scope?(token, :read_prefs)
19         can [:gpx_files], User if scope?(token, :read_gpx)
20         can [:index, :show], UserPreference if scope?(token, :read_prefs)
21         can [:update, :update_all, :destroy], UserPreference if scope?(token, :write_prefs)
22         can [:inbox, :outbox, :show, :update, :destroy], Message if scope?(token, :consume_messages)
23         can [:create], Message if scope?(token, :send_messages)
24
25         if user.terms_agreed?
26           can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset if scope?(token, :write_api)
27           can :create, ChangesetComment if scope?(token, :write_api)
28           can [:create, :update, :delete], [Node, Way, Relation] if scope?(token, :write_api)
29         end
30
31         if user.moderator?
32           can [:destroy, :restore], ChangesetComment if scope?(token, :write_api)
33           can :destroy, Note if scope?(token, :write_notes)
34           can :redact, [OldNode, OldWay, OldRelation] if user&.terms_agreed? && (scope?(token, :write_api) || scope?(token, :write_redactions))
35         end
36       end
37     end
38   end
39
40   private
41
42   def scope?(token, scope)
43     token&.includes_scope?(scope)
44   end
45 end