]> git.openstreetmap.org Git - rails.git/blob - app/models/capability.rb
Rework capabilities to avoid assumptions about missing tokens
[rails.git] / app / models / capability.rb
1 # frozen_string_literal: true
2
3 class Capability
4   include CanCan::Ability
5
6   def initialize(user, token)
7     if user
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     end
11   end
12
13   private
14
15   def capability?(token, cap)
16     token&.read_attribute(cap)
17   end
18 end