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