]> git.openstreetmap.org Git - rails.git/blob - app/abilities/capability.rb
Merge remote-tracking branch 'upstream/pull/2105'
[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_details], User if capability?(token, :allow_read_prefs)
9     can [:api_gpx_files], User if capability?(token, :allow_read_gpx)
10     can [:read, :read_one], UserPreference if capability?(token, :allow_read_prefs)
11     can [:update, :update_one, :delete_one], UserPreference if capability?(token, :allow_write_prefs)
12
13     if token&.user&.terms_agreed? || !REQUIRE_TERMS_AGREED
14       can [:create, :update, :upload, :close, :subscribe, :unsubscribe, :expand_bbox], Changeset if capability?(token, :allow_write_api)
15       can :create, ChangesetComment if capability?(token, :allow_write_api)
16     end
17
18     if token&.user&.moderator?
19       can [:destroy, :restore], ChangesetComment if capability?(token, :allow_write_api)
20       can :destroy, Note if capability?(token, :allow_write_notes)
21     end
22   end
23
24   private
25
26   def capability?(token, cap)
27     token&.read_attribute(cap)
28   end
29 end