debian-mirror-gitlab/app/models/members/member_role.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.4 KiB
Ruby
Raw Normal View History

2022-08-27 11:52:29 +05:30
# frozen_string_literal: true
2023-04-23 21:23:45 +05:30
class MemberRole < ApplicationRecord # rubocop:disable Gitlab/NamespacedClass
2023-03-04 22:38:38 +05:30
include IgnorableColumns
ignore_column :download_code, remove_with: '15.9', remove_after: '2023-01-22'
2022-08-27 11:52:29 +05:30
has_many :members
belongs_to :namespace
2022-11-25 23:54:43 +05:30
validates :namespace, presence: true
2022-08-27 11:52:29 +05:30
validates :base_access_level, presence: true
2022-11-25 23:54:43 +05:30
validate :belongs_to_top_level_namespace
2023-03-04 22:38:38 +05:30
validate :validate_namespace_locked, on: :update
2023-03-17 16:20:25 +05:30
validate :attributes_locked_after_member_associated, on: :update
2023-03-04 22:38:38 +05:30
validates_associated :members
2022-11-25 23:54:43 +05:30
2023-04-23 21:23:45 +05:30
before_destroy :prevent_delete_after_member_associated
2022-11-25 23:54:43 +05:30
private
def belongs_to_top_level_namespace
return if !namespace || namespace.root?
2023-03-04 22:38:38 +05:30
errors.add(:namespace, s_("MemberRole|must be top-level namespace"))
end
def validate_namespace_locked
return unless namespace_id_changed?
errors.add(:namespace, s_("MemberRole|can't be changed"))
2022-11-25 23:54:43 +05:30
end
2023-03-17 16:20:25 +05:30
def attributes_locked_after_member_associated
return unless members.present?
errors.add(:base, s_("MemberRole|cannot be changed because it is already assigned to a user. "\
"Please create a new Member Role instead"))
end
2023-04-23 21:23:45 +05:30
def prevent_delete_after_member_associated
return unless members.present?
errors.add(:base, s_("MemberRole|cannot be deleted because it is already assigned to a user. "\
"Please disassociate the member role from all users before deletion."))
throw :abort # rubocop:disable Cop/BanCatchThrow
end
2022-08-27 11:52:29 +05:30
end