]> git.openstreetmap.org Git - rails.git/blob - app/models/capability.rb
17468750331de66846a696472c8436f3dd740bad
[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 has_capability?(token, :allow_read_prefs)
9       can [:update, :update_one, :delete_one], UserPreference if has_capability?(token, :allow_write_prefs)
10
11     end
12   end
13
14   # If a user provides no tokens, they've authenticated via a non-oauth method
15   # and permission to access to all capabilities is assumed.
16   def has_capability?(token, cap)
17     token.nil? || token.read_attribute(cap)
18   end
19 end