2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
class GroupMember < Member
|
2021-08-09 11:09:13 -04:00
|
|
|
extend ::Gitlab::Utils::Override
|
2019-07-29 18:33:57 -04:00
|
|
|
include FromUnion
|
2020-03-31 20:08:09 -04:00
|
|
|
include CreatedAtFilterable
|
2019-07-29 18:33:57 -04:00
|
|
|
|
2019-08-31 15:57:00 -04:00
|
|
|
SOURCE_TYPE = 'Namespace'
|
2021-12-08 07:13:04 -05:00
|
|
|
SOURCE_TYPE_FORMAT = /\ANamespace\z/.freeze
|
2014-09-14 12:32:51 -04:00
|
|
|
|
2016-10-18 17:20:36 -04:00
|
|
|
belongs_to :group, foreign_key: 'source_id'
|
2021-03-31 14:09:19 -04:00
|
|
|
alias_attribute :namespace_id, :source_id
|
2021-05-26 17:10:49 -04:00
|
|
|
delegate :update_two_factor_requirement, to: :user, allow_nil: true
|
2017-01-24 16:09:58 -05:00
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
# Make sure group member points only to group as it source
|
|
|
|
default_value_for :source_type, SOURCE_TYPE
|
2021-12-08 07:13:04 -05:00
|
|
|
validates :source_type, format: { with: SOURCE_TYPE_FORMAT }
|
2020-09-10 14:08:54 -04:00
|
|
|
|
2020-06-10 08:08:58 -04:00
|
|
|
default_scope { where(source_type: SOURCE_TYPE) } # rubocop:disable Cop/DefaultScope
|
2014-09-14 12:32:51 -04:00
|
|
|
|
2021-12-20 10:12:25 -05:00
|
|
|
scope :of_groups, ->(groups) { where(source_id: groups&.select(:id)) }
|
2019-07-17 22:09:30 -04:00
|
|
|
scope :of_ldap_type, -> { where(ldap: true) }
|
2020-07-16 11:09:38 -04:00
|
|
|
scope :count_users_by_group_id, -> { group(:source_id).count }
|
2020-10-22 08:08:41 -04:00
|
|
|
scope :with_user, -> (user) { where(user: user) }
|
2020-06-10 17:09:29 -04:00
|
|
|
|
2017-01-24 16:09:58 -05:00
|
|
|
after_create :update_two_factor_requirement, unless: :invite?
|
|
|
|
after_destroy :update_two_factor_requirement, unless: :invite?
|
|
|
|
|
2021-04-15 05:09:03 -04:00
|
|
|
attr_accessor :last_owner, :last_blocked_owner
|
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
def self.access_level_roles
|
|
|
|
Gitlab::Access.options_with_owner
|
|
|
|
end
|
|
|
|
|
2021-06-01 17:10:06 -04:00
|
|
|
def self.pluck_user_ids
|
|
|
|
pluck(:user_id)
|
|
|
|
end
|
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
def group
|
|
|
|
source
|
|
|
|
end
|
|
|
|
|
2016-06-02 12:05:06 -04:00
|
|
|
# Because source_type is `Namespace`...
|
|
|
|
def real_source_type
|
2021-09-22 08:12:04 -04:00
|
|
|
Group.sti_name
|
2016-06-02 12:05:06 -04:00
|
|
|
end
|
|
|
|
|
2017-08-04 16:56:33 -04:00
|
|
|
def notifiable_options
|
|
|
|
{ group: group }
|
2017-08-04 14:53:36 -04:00
|
|
|
end
|
|
|
|
|
2021-09-28 11:11:30 -04:00
|
|
|
private
|
|
|
|
|
2021-08-09 11:09:13 -04:00
|
|
|
override :refresh_member_authorized_projects
|
2021-09-28 11:11:30 -04:00
|
|
|
def refresh_member_authorized_projects(blocking:)
|
2021-08-09 11:09:13 -04:00
|
|
|
# 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?
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:23:38 -04:00
|
|
|
def send_invite
|
2018-04-23 10:38:37 -04:00
|
|
|
run_after_commit_or_now { notification_service.invite_group_member(self, @raw_invite_token) }
|
2015-04-10 09:23:38 -04:00
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2014-08-21 06:53:32 -04:00
|
|
|
def post_create_hook
|
2021-01-13 13:10:55 -05:00
|
|
|
if send_welcome_email?
|
|
|
|
run_after_commit_or_now { notification_service.new_group_member(self) }
|
|
|
|
end
|
2015-04-10 09:09:37 -04:00
|
|
|
|
|
|
|
super
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
def post_update_hook
|
2019-01-15 16:05:36 -05:00
|
|
|
if saved_change_to_access_level?
|
2018-04-23 10:38:37 -04:00
|
|
|
run_after_commit { notification_service.update_group_member(self) }
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
2014-08-21 06:53:32 -04:00
|
|
|
|
2021-01-14 13:10:59 -05:00
|
|
|
if saved_change_to_expires_at?
|
|
|
|
run_after_commit { notification_service.updated_group_member_expiration(self) }
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
super
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
2015-04-10 09:23:38 -04:00
|
|
|
|
|
|
|
def after_accept_invite
|
|
|
|
notification_service.accept_group_invite(self)
|
2020-03-04 16:07:54 -05:00
|
|
|
update_two_factor_requirement
|
2015-04-10 09:23:38 -04:00
|
|
|
|
|
|
|
super
|
|
|
|
end
|
2015-04-10 10:37:02 -04:00
|
|
|
|
|
|
|
def after_decline_invite
|
|
|
|
notification_service.decline_group_invite(self)
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
2021-01-13 13:10:55 -05:00
|
|
|
|
|
|
|
def send_welcome_email?
|
|
|
|
true
|
|
|
|
end
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
GroupMember.prepend_mod_with('GroupMember')
|