Commit dfa77e40 authored by Yorick Peterse's avatar Yorick Peterse

Move Identity.uniqueness_scope to a module

Moving this method to a separate module looks a bit odd, but it allows
for EE to extend the method without also having to redefine a variety of
validation rules.
parent ac0103a4
# frozen_string_literal: true
class Identity < ActiveRecord::Base
def self.uniqueness_scope
:provider
end
prepend EE::Identity
include Sortable
......@@ -13,8 +9,8 @@ class Identity < ActiveRecord::Base
belongs_to :user
validates :provider, presence: true
validates :extern_uid, allow_blank: true, uniqueness: { scope: uniqueness_scope, case_sensitive: false }
validates :user_id, uniqueness: { scope: uniqueness_scope }
validates :extern_uid, allow_blank: true, uniqueness: { scope: UniquenessScopes.scopes, case_sensitive: false }
validates :user_id, uniqueness: { scope: UniquenessScopes.scopes }
before_save :ensure_normalized_extern_uid, if: :extern_uid_changed?
after_destroy :clear_user_synced_attributes, if: :user_synced_attributes_metadata_from_provider?
......
# frozen_string_literal: true
class Identity < ActiveRecord::Base
# This module and method are defined in a separate file to allow EE to
# redefine the `scopes` method before it is used in the `Identity` model.
module UniquenessScopes
def self.scopes
[:provider]
end
end
end
Identity::UniquenessScopes.prepend(EE::Identity::UniquenessScopes)
......@@ -5,17 +5,16 @@ module EE
prepended do
belongs_to :saml_provider
validates :secondary_extern_uid, allow_blank: true, uniqueness: { scope: uniqueness_scope, case_sensitive: false }
validates :secondary_extern_uid,
allow_blank: true,
uniqueness: {
scope: ::Identity::UniquenessScopes.scopes,
case_sensitive: false
}
scope :with_secondary_extern_uid, ->(provider, secondary_extern_uid) do
iwhere(secondary_extern_uid: normalize_uid(provider, secondary_extern_uid)).with_provider(provider)
end
end
class_methods do
def uniqueness_scope
[*super, :saml_provider_id]
end
end
end
end
# frozen_string_literal: true
module EE
module Identity
module UniquenessScopes
extend ActiveSupport::Concern
class_methods do
def scopes
[*super, :saml_provider_id]
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment