From c27b7ef90cd3e4a7fd00c3f90b26333eb7372233 Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Thu, 12 Jan 2023 17:53:17 +0300 Subject: [PATCH] Move polymorphic resource owner and application owner associations in a lazy ORM hooks --- CHANGELOG.md | 1 + lib/doorkeeper.rb | 1 + .../concerns/polymorphic_resource_owner.rb | 30 +++++++++++++++++++ lib/doorkeeper/orm/active_record.rb | 11 ++++++- .../orm/active_record/mixins/access_grant.rb | 6 ---- .../orm/active_record/mixins/access_token.rb | 4 --- .../orm/active_record/mixins/application.rb | 4 --- spec/lib/config_spec.rb | 6 ++++ spec/models/doorkeeper/application_spec.rb | 6 ++++ 9 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 lib/doorkeeper/models/concerns/polymorphic_resource_owner.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index c85c52004..02d5e9736 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ User-visible changes worth mentioning. - [#1605] Fix URI validation for Ruby 3.2+. - [#1625] Exclude endless access tokens from `StaleRecordsCleaner`. - [#1626] Remove deprecated `active_record_options` config option. +- [#1627] Move polymorphic resource owner and application owner associations in a lazy ORM hooks. ## 5.6.2 diff --git a/lib/doorkeeper.rb b/lib/doorkeeper.rb index c92a74038..cd63e453f 100644 --- a/lib/doorkeeper.rb +++ b/lib/doorkeeper.rb @@ -90,6 +90,7 @@ module Models autoload :Expirable, "doorkeeper/models/concerns/expirable" autoload :ExpirationTimeSqlMath, "doorkeeper/models/concerns/expiration_time_sql_math" autoload :Orderable, "doorkeeper/models/concerns/orderable" + autoload :PolymorphicResourceOwner, "doorkeeper/models/concerns/polymorphic_resource_owner" autoload :Scopes, "doorkeeper/models/concerns/scopes" autoload :Reusable, "doorkeeper/models/concerns/reusable" autoload :ResourceOwnerable, "doorkeeper/models/concerns/resource_ownerable" diff --git a/lib/doorkeeper/models/concerns/polymorphic_resource_owner.rb b/lib/doorkeeper/models/concerns/polymorphic_resource_owner.rb new file mode 100644 index 000000000..e89ee02c3 --- /dev/null +++ b/lib/doorkeeper/models/concerns/polymorphic_resource_owner.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Doorkeeper + module Models + module PolymorphicResourceOwner + module ForAccessGrant + extend ActiveSupport::Concern + + included do + if Doorkeeper.config.polymorphic_resource_owner? + belongs_to :resource_owner, polymorphic: true, optional: false + else + validates :resource_owner_id, presence: true + end + end + end + + module ForAccessToken + extend ActiveSupport::Concern + + included do + if Doorkeeper.config.polymorphic_resource_owner? + belongs_to :resource_owner, polymorphic: true, optional: true + end + end + end + end + end +end + diff --git a/lib/doorkeeper/orm/active_record.rb b/lib/doorkeeper/orm/active_record.rb index deef164c3..8cfa49e34 100644 --- a/lib/doorkeeper/orm/active_record.rb +++ b/lib/doorkeeper/orm/active_record.rb @@ -29,7 +29,16 @@ module Mixins end def self.run_hooks - # nop + initialize_configured_associations + end + + def self.initialize_configured_associations + if Doorkeeper.config.enable_application_owner? + Doorkeeper.config.application_model.include ::Doorkeeper::Models::Ownership + end + + Doorkeeper.config.access_grant_model.include ::Doorkeeper::Models::PolymorphicResourceOwner::ForAccessGrant + Doorkeeper.config.access_token_model.include ::Doorkeeper::Models::PolymorphicResourceOwner::ForAccessToken end end end diff --git a/lib/doorkeeper/orm/active_record/mixins/access_grant.rb b/lib/doorkeeper/orm/active_record/mixins/access_grant.rb index b9ba92b6b..85c52beed 100644 --- a/lib/doorkeeper/orm/active_record/mixins/access_grant.rb +++ b/lib/doorkeeper/orm/active_record/mixins/access_grant.rb @@ -14,12 +14,6 @@ module AccessGrant optional: true, inverse_of: :access_grants - if Doorkeeper.config.polymorphic_resource_owner? - belongs_to :resource_owner, polymorphic: true, optional: false - else - validates :resource_owner_id, presence: true - end - validates :application_id, :token, :expires_in, diff --git a/lib/doorkeeper/orm/active_record/mixins/access_token.rb b/lib/doorkeeper/orm/active_record/mixins/access_token.rb index ebf98743d..300f80f9b 100644 --- a/lib/doorkeeper/orm/active_record/mixins/access_token.rb +++ b/lib/doorkeeper/orm/active_record/mixins/access_token.rb @@ -14,10 +14,6 @@ module AccessToken inverse_of: :access_tokens, optional: true - if Doorkeeper.config.polymorphic_resource_owner? - belongs_to :resource_owner, polymorphic: true, optional: true - end - validates :token, presence: true, uniqueness: { case_sensitive: true } validates :refresh_token, uniqueness: { case_sensitive: true }, if: :use_refresh_token? diff --git a/lib/doorkeeper/orm/active_record/mixins/application.rb b/lib/doorkeeper/orm/active_record/mixins/application.rb index 3c4d45fea..c2fab3594 100644 --- a/lib/doorkeeper/orm/active_record/mixins/application.rb +++ b/lib/doorkeeper/orm/active_record/mixins/application.rb @@ -10,10 +10,6 @@ module Application include ::Doorkeeper::ApplicationMixin - if Doorkeeper.config.enable_application_owner? - include ::Doorkeeper::Models::Ownership - end - has_many :access_grants, foreign_key: :application_id, dependent: :delete_all, diff --git a/spec/lib/config_spec.rb b/spec/lib/config_spec.rb index 6b5e6d8f1..1d95076fd 100644 --- a/spec/lib/config_spec.rb +++ b/spec/lib/config_spec.rb @@ -376,10 +376,13 @@ end Object.const_set("ApplicationWithOwner", application_with_owner_class) + # Manually reload Doorkeeper ORM setup because we're patching classes + Doorkeeper.run_orm_hooks end after do Object.send(:remove_const, :ApplicationWithOwner) + Doorkeeper.run_orm_hooks end it "adds support for application owner" do @@ -404,10 +407,13 @@ end Object.const_set("ApplicationWithOwner", application_with_owner_class) + # Manually reload Doorkeeper ORM setup because we're patching classes + Doorkeeper.run_orm_hooks end after do Object.send(:remove_const, :ApplicationWithOwner) + Doorkeeper.run_orm_hooks end it "adds support for application owner" do diff --git a/spec/models/doorkeeper/application_spec.rb b/spec/models/doorkeeper/application_spec.rb index 54a80b552..5d8e1f27f 100644 --- a/spec/models/doorkeeper/application_spec.rb +++ b/spec/models/doorkeeper/application_spec.rb @@ -114,10 +114,13 @@ def self.generate end Object.const_set("ApplicationWithOwner", application_with_owner_class) + # Manually reload Doorkeeper ORM setup because we're patching classes + Doorkeeper.run_orm_hooks end after do Object.send(:remove_const, :ApplicationWithOwner) + Doorkeeper.run_orm_hooks end it "is valid given valid attributes" do @@ -135,10 +138,13 @@ def self.generate @owner = FactoryBot.build_stubbed(:doorkeeper_testing_user) Object.const_set("ApplicationWithOwner", application_with_owner_class) + # Manually reload Doorkeeper ORM setup because we're patching classes + Doorkeeper.run_orm_hooks end after do Object.send(:remove_const, :ApplicationWithOwner) + Doorkeeper.run_orm_hooks end it "is invalid without an owner" do