Commit 6ba31f27 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '344814-shift-some-validations' into 'master'

Refactor some validations in Member classes

See merge request gitlab-org/gitlab!76254
parents 3ec6eb7f b0fae780
...@@ -52,6 +52,7 @@ class Member < ApplicationRecord ...@@ -52,6 +52,7 @@ class Member < ApplicationRecord
message: _('project bots cannot be added to other groups / projects') message: _('project bots cannot be added to other groups / projects')
}, },
if: :project_bot? if: :project_bot?
validate :access_level_inclusion
scope :with_invited_user_state, -> do scope :with_invited_user_state, -> do
joins('LEFT JOIN users as invited_user ON invited_user.email = members.invite_email') joins('LEFT JOIN users as invited_user ON invited_user.email = members.invite_email')
...@@ -382,6 +383,12 @@ class Member < ApplicationRecord ...@@ -382,6 +383,12 @@ class Member < ApplicationRecord
private private
def access_level_inclusion
return if access_level.in?(Gitlab::Access.all_values)
errors.add(:access_level, "is not included in the list")
end
def send_invite def send_invite
# override in subclass # override in subclass
end end
......
...@@ -6,6 +6,7 @@ class GroupMember < Member ...@@ -6,6 +6,7 @@ class GroupMember < Member
include CreatedAtFilterable include CreatedAtFilterable
SOURCE_TYPE = 'Namespace' SOURCE_TYPE = 'Namespace'
SOURCE_TYPE_FORMAT = /\ANamespace\z/.freeze
belongs_to :group, foreign_key: 'source_id' belongs_to :group, foreign_key: 'source_id'
alias_attribute :namespace_id, :source_id alias_attribute :namespace_id, :source_id
...@@ -13,9 +14,7 @@ class GroupMember < Member ...@@ -13,9 +14,7 @@ class GroupMember < Member
# Make sure group member points only to group as it source # Make sure group member points only to group as it source
default_value_for :source_type, SOURCE_TYPE default_value_for :source_type, SOURCE_TYPE
validates :source_type, format: { with: /\ANamespace\z/ } validates :source_type, format: { with: SOURCE_TYPE_FORMAT }
validates :access_level, presence: true
validate :access_level_inclusion
default_scope { where(source_type: SOURCE_TYPE) } # rubocop:disable Cop/DefaultScope default_scope { where(source_type: SOURCE_TYPE) } # rubocop:disable Cop/DefaultScope
...@@ -65,12 +64,6 @@ class GroupMember < Member ...@@ -65,12 +64,6 @@ class GroupMember < Member
super super
end end
def access_level_inclusion
return if access_level.in?(Gitlab::Access.all_values)
errors.add(:access_level, "is not included in the list")
end
def send_invite def send_invite
run_after_commit_or_now { notification_service.invite_group_member(self, @raw_invite_token) } run_after_commit_or_now { notification_service.invite_group_member(self, @raw_invite_token) }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
class ProjectMember < Member class ProjectMember < Member
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
SOURCE_TYPE = 'Project' SOURCE_TYPE = 'Project'
SOURCE_TYPE_FORMAT = /\AProject\z/.freeze
belongs_to :project, foreign_key: 'source_id' belongs_to :project, foreign_key: 'source_id'
...@@ -10,8 +11,7 @@ class ProjectMember < Member ...@@ -10,8 +11,7 @@ class ProjectMember < Member
# Make sure project member points only to project as it source # Make sure project member points only to project as it source
default_value_for :source_type, SOURCE_TYPE default_value_for :source_type, SOURCE_TYPE
validates :source_type, format: { with: /\AProject\z/ } validates :source_type, format: { with: SOURCE_TYPE_FORMAT }
validates :access_level, inclusion: { in: Gitlab::Access.values }
default_scope { where(source_type: SOURCE_TYPE) } # rubocop:disable Cop/DefaultScope default_scope { where(source_type: SOURCE_TYPE) } # rubocop:disable Cop/DefaultScope
scope :in_project, ->(project) { where(source_id: project.id) } scope :in_project, ->(project) { where(source_id: project.id) }
...@@ -92,6 +92,13 @@ class ProjectMember < Member ...@@ -92,6 +92,13 @@ class ProjectMember < Member
private private
override :access_level_inclusion
def access_level_inclusion
return if access_level.in?(Gitlab::Access.values)
errors.add(:access_level, "is not included in the list")
end
override :refresh_member_authorized_projects override :refresh_member_authorized_projects
def refresh_member_authorized_projects(blocking:) def refresh_member_authorized_projects(blocking:)
return unless user return unless user
......
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