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

121 lines
3.1 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
class GroupMember < Member
2021-10-27 15:23:28 +05:30
extend ::Gitlab::Utils::Override
2019-10-12 21:52:04 +05:30
include FromUnion
2020-04-22 19:07:51 +05:30
include CreatedAtFilterable
2019-10-12 21:52:04 +05:30
2019-12-04 20:38:33 +05:30
SOURCE_TYPE = 'Namespace'
2022-01-26 12:08:38 +05:30
SOURCE_TYPE_FORMAT = /\ANamespace\z/.freeze
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
belongs_to :group, foreign_key: 'source_id'
2021-04-29 21:17:54 +05:30
alias_attribute :namespace_id, :source_id
2021-09-04 01:27:46 +05:30
delegate :update_two_factor_requirement, to: :user, allow_nil: true
2015-04-26 12:48:37 +05:30
# Make sure group member points only to group as it source
default_value_for :source_type, SOURCE_TYPE
2022-01-26 12:08:38 +05:30
validates :source_type, format: { with: SOURCE_TYPE_FORMAT }
2020-11-24 15:15:51 +05:30
2020-06-23 00:09:42 +05:30
default_scope { where(source_type: SOURCE_TYPE) } # rubocop:disable Cop/DefaultScope
2015-04-26 12:48:37 +05:30
2022-03-02 08:16:31 +05:30
scope :of_groups, ->(groups) { where(source_id: groups&.select(:id)) }
2019-12-04 20:38:33 +05:30
scope :of_ldap_type, -> { where(ldap: true) }
2020-07-28 23:09:34 +05:30
scope :count_users_by_group_id, -> { group(:source_id).count }
2021-01-29 00:20:46 +05:30
scope :with_user, -> (user) { where(user: user) }
2020-06-23 00:09:42 +05:30
2017-08-17 22:00:37 +05:30
after_create :update_two_factor_requirement, unless: :invite?
after_destroy :update_two_factor_requirement, unless: :invite?
2021-04-29 21:17:54 +05:30
attr_accessor :last_owner, :last_blocked_owner
2022-07-23 23:45:48 +05:30
# For those who get to see a modal with a role dropdown, here are the options presented
def self.permissible_access_level_roles(_, _)
# This method is a stopgap in preparation for https://gitlab.com/gitlab-org/gitlab/-/issues/364087
access_level_roles
end
2015-04-26 12:48:37 +05:30
def self.access_level_roles
Gitlab::Access.options_with_owner
end
2021-09-04 01:27:46 +05:30
def self.pluck_user_ids
pluck(:user_id)
end
2015-04-26 12:48:37 +05:30
def group
source
end
# Because source_type is `Namespace`...
def real_source_type
2021-11-18 22:05:49 +05:30
Group.sti_name
end
2018-03-17 18:26:18 +05:30
def notifiable_options
{ group: group }
end
2021-11-18 22:05:49 +05:30
private
2021-10-27 15:23:28 +05:30
override :refresh_member_authorized_projects
2021-11-18 22:05:49 +05:30
def refresh_member_authorized_projects(blocking:)
2021-10-27 15:23:28 +05:30
# Here, `destroyed_by_association` will be present if the
# GroupMember is being destroyed due to the `dependent: :destroy`
# callback on Group. In this case, there is no need to refresh the
# authorizations, because whenever a Group is being destroyed,
# its projects are also destroyed, so the removal of project_authorizations
# will happen behind the scenes via DB foreign keys anyway.
return if destroyed_by_association.present?
2022-08-27 11:52:29 +05:30
super
2021-10-27 15:23:28 +05:30
end
2015-04-26 12:48:37 +05:30
def send_invite
2018-10-15 14:42:47 +05:30
run_after_commit_or_now { notification_service.invite_group_member(self, @raw_invite_token) }
2015-04-26 12:48:37 +05:30
super
end
def post_create_hook
2021-03-08 18:12:59 +05:30
if send_welcome_email?
run_after_commit_or_now { notification_service.new_group_member(self) }
end
2015-04-26 12:48:37 +05:30
super
end
def post_update_hook
2019-07-31 22:56:46 +05:30
if saved_change_to_access_level?
2018-10-15 14:42:47 +05:30
run_after_commit { notification_service.update_group_member(self) }
2015-04-26 12:48:37 +05:30
end
2021-03-08 18:12:59 +05:30
if saved_change_to_expires_at?
run_after_commit { notification_service.updated_group_member_expiration(self) }
end
2015-04-26 12:48:37 +05:30
super
end
def after_accept_invite
2022-07-23 23:45:48 +05:30
run_after_commit_or_now do
notification_service.accept_group_invite(self)
end
2020-03-07 23:17:34 +05:30
update_two_factor_requirement
2015-04-26 12:48:37 +05:30
super
end
def after_decline_invite
notification_service.decline_group_invite(self)
super
end
2021-03-08 18:12:59 +05:30
def send_welcome_email?
true
end
2015-04-26 12:48:37 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
GroupMember.prepend_mod_with('GroupMember')