]> git.openstreetmap.org Git - rails.git/blob - app/models/capability.rb
Change abilities based on upstream renamings
[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
11     end
12   end
13
14   private
15
16   # If a user provides no tokens, they've authenticated via a non-oauth method
17   # and permission to access to all capabilities is assumed.
18   def capability?(token, cap)
19     token.nil? || token.read_attribute(cap)
20   end
21 end