]> git.openstreetmap.org Git - rails.git/blob - app/models/oauth2_application.rb
Add frozen_string_literal comments to ruby files
[rails.git] / app / models / oauth2_application.rb
1 # frozen_string_literal: true
2
3 # == Schema Information
4 #
5 # Table name: oauth_applications
6 #
7 #  id           :bigint           not null, primary key
8 #  owner_type   :string           not null
9 #  owner_id     :bigint           not null
10 #  name         :string           not null
11 #  uid          :string           not null
12 #  secret       :string           not null
13 #  redirect_uri :text             not null
14 #  scopes       :string           default(""), not null
15 #  confidential :boolean          default(TRUE), not null
16 #  created_at   :datetime         not null
17 #  updated_at   :datetime         not null
18 #
19 # Indexes
20 #
21 #  index_oauth_applications_on_owner_type_and_owner_id  (owner_type,owner_id)
22 #  index_oauth_applications_on_uid                      (uid) UNIQUE
23 #
24 # Foreign Keys
25 #
26 #  fk_rails_...  (owner_id => users.id)
27 #
28 class Oauth2Application < Doorkeeper::Application
29   belongs_to :owner, :polymorphic => true
30
31   validate :allowed_scopes
32
33   def authorized_scopes_for(user)
34     authorized_tokens.where(:resource_owner_id => user).sum(Doorkeeper::OAuth::Scopes.new, &:scopes)
35   end
36
37   private
38
39   def allowed_scopes
40     return if owner.administrator?
41
42     errors.add(:scopes) if scopes.any? { |scope| Oauth::PRIVILEGED_SCOPES.include?(scope) }
43   end
44 end