]> git.openstreetmap.org Git - rails.git/blob - app/abilities/capability.rb
Merge remote-tracking branch 'upstream/pull/2073'
[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, ChangesetComment if capability?(token, :allow_write_api)
8     can [:read, :read_one], UserPreference if capability?(token, :allow_read_prefs)
9     can [:update, :update_one, :delete_one], UserPreference if capability?(token, :allow_write_prefs)
10
11     if token&.user&.moderator?
12       can [:destroy, :restore], ChangesetComment if capability?(token, :allow_write_api)
13     end
14   end
15
16   private
17
18   def capability?(token, cap)
19     token&.read_attribute(cap)
20   end
21 end