2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
class ProjectMember < Member
|
2021-08-09 11:09:13 -04:00
|
|
|
extend ::Gitlab::Utils::Override
|
2019-08-31 15:57:00 -04:00
|
|
|
SOURCE_TYPE = 'Project'
|
2021-12-08 07:13:04 -05:00
|
|
|
SOURCE_TYPE_FORMAT = /\AProject\z/.freeze
|
2014-09-14 12:32:51 -04:00
|
|
|
|
2016-10-18 17:20:36 -04:00
|
|
|
belongs_to :project, foreign_key: 'source_id'
|
2014-09-14 13:38:57 -04:00
|
|
|
|
2021-03-31 14:09:19 -04:00
|
|
|
delegate :namespace_id, to: :project
|
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
# Make sure project member points only to project 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-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
|
|
|
|
|
|
|
scope :in_project, ->(project) { where(source_id: project.id) }
|
2018-11-16 10:09:32 -05:00
|
|
|
scope :in_namespaces, ->(groups) do
|
|
|
|
joins('INNER JOIN projects ON projects.id = members.source_id')
|
2021-04-26 08:09:44 -04:00
|
|
|
.where(projects: { namespace_id: groups.select(:id) })
|
2018-11-16 10:09:32 -05:00
|
|
|
end
|
2014-09-14 12:32:51 -04:00
|
|
|
|
|
|
|
class << self
|
2022-07-05 14:08:43 -04:00
|
|
|
# Add members to projects with passed access option
|
2014-09-14 12:32:51 -04:00
|
|
|
#
|
|
|
|
# access can be an integer representing a access code
|
2018-07-11 10:36:08 -04:00
|
|
|
# or symbol like :maintainer representing role
|
2014-09-14 12:32:51 -04:00
|
|
|
#
|
|
|
|
# Ex.
|
2022-07-05 14:08:43 -04:00
|
|
|
# add_members_to_projects(
|
2014-09-14 12:32:51 -04:00
|
|
|
# project_ids,
|
|
|
|
# user_ids,
|
2018-07-11 10:36:08 -04:00
|
|
|
# ProjectMember::MAINTAINER
|
2014-09-14 12:32:51 -04:00
|
|
|
# )
|
|
|
|
#
|
2022-07-05 14:08:43 -04:00
|
|
|
# add_members_to_projects(
|
2014-09-14 12:32:51 -04:00
|
|
|
# project_ids,
|
|
|
|
# user_ids,
|
2018-07-11 10:36:08 -04:00
|
|
|
# :maintainer
|
2014-09-14 12:32:51 -04:00
|
|
|
# )
|
|
|
|
#
|
2022-07-05 14:08:43 -04:00
|
|
|
def add_members_to_projects(project_ids, users, access_level, current_user: nil, expires_at: nil)
|
2016-09-16 11:54:21 -04:00
|
|
|
self.transaction do
|
2014-09-14 12:32:51 -04:00
|
|
|
project_ids.each do |project_id|
|
2015-04-10 09:25:32 -04:00
|
|
|
project = Project.find(project_id)
|
|
|
|
|
2022-07-05 14:08:43 -04:00
|
|
|
Members::Projects::CreatorService.add_members( # rubocop:disable CodeReuse/ServiceClass
|
2016-09-16 11:54:21 -04:00
|
|
|
project,
|
|
|
|
users,
|
|
|
|
access_level,
|
|
|
|
current_user: current_user,
|
|
|
|
expires_at: expires_at
|
|
|
|
)
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def truncate_teams(project_ids)
|
|
|
|
ProjectMember.transaction do
|
|
|
|
members = ProjectMember.where(source_id: project_ids)
|
2016-01-21 08:23:53 -05:00
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
members.each do |member|
|
|
|
|
member.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError
|
2014-09-14 12:32:51 -04:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2014-09-25 18:07:40 -04:00
|
|
|
def truncate_team(project)
|
2014-09-14 12:32:51 -04:00
|
|
|
truncate_teams [project.id]
|
|
|
|
end
|
|
|
|
|
2022-06-16 05:09:15 -04:00
|
|
|
# For those who get to see a modal with a role dropdown, here are the options presented
|
|
|
|
def permissible_access_level_roles(current_user, project)
|
|
|
|
# This method is a stopgap in preparation for https://gitlab.com/gitlab-org/gitlab/-/issues/364087
|
|
|
|
if Ability.allowed?(current_user, :manage_owners, project)
|
|
|
|
Gitlab::Access.options_with_owner
|
|
|
|
else
|
|
|
|
ProjectMember.access_level_roles
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
def access_level_roles
|
2014-09-14 12:32:51 -04:00
|
|
|
Gitlab::Access.options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
def project
|
|
|
|
source
|
|
|
|
end
|
|
|
|
|
2017-08-04 16:56:33 -04:00
|
|
|
def notifiable_options
|
|
|
|
{ project: project }
|
2017-08-04 14:53:36 -04:00
|
|
|
end
|
|
|
|
|
2021-09-28 11:11:30 -04:00
|
|
|
private
|
|
|
|
|
2021-12-08 07:13:04 -05:00
|
|
|
override :access_level_inclusion
|
|
|
|
def access_level_inclusion
|
2022-03-06 22:18:52 -05:00
|
|
|
unless access_level.in?(Gitlab::Access.all_values)
|
2022-02-23 01:18:49 -05:00
|
|
|
errors.add(:access_level, "is not included in the list")
|
|
|
|
end
|
2021-12-08 07:13:04 -05:00
|
|
|
end
|
|
|
|
|
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
|
|
|
return unless user
|
|
|
|
|
|
|
|
# rubocop:disable CodeReuse/ServiceClass
|
2021-09-28 11:11:30 -04:00
|
|
|
if blocking
|
2022-08-01 08:12:10 -04:00
|
|
|
blocking_project_authorizations_refresh
|
2021-09-28 11:11:30 -04:00
|
|
|
else
|
|
|
|
AuthorizedProjectUpdate::ProjectRecalculatePerUserWorker.perform_async(project.id, user.id)
|
|
|
|
end
|
2021-08-09 11:09:13 -04:00
|
|
|
|
|
|
|
# Until we compare the inconsistency rates of the new, specialized service and
|
|
|
|
# the old approach, we still run AuthorizedProjectsWorker
|
|
|
|
# but with some delay and lower urgency as a safety net.
|
|
|
|
UserProjectAccessChangedService.new(user_id)
|
2021-09-28 11:11:30 -04:00
|
|
|
.execute(blocking: false, priority: UserProjectAccessChangedService::LOW_PRIORITY)
|
2021-08-09 11:09:13 -04:00
|
|
|
# rubocop:enable CodeReuse/ServiceClass
|
|
|
|
end
|
|
|
|
|
2022-08-01 08:12:10 -04:00
|
|
|
# This method is overridden in the test environment, see stubbed_member.rb
|
|
|
|
def blocking_project_authorizations_refresh
|
|
|
|
AuthorizedProjectUpdate::ProjectRecalculatePerUserWorker.bulk_perform_and_wait([[project.id, user.id]])
|
|
|
|
end
|
|
|
|
|
2022-01-24 10:11:33 -05:00
|
|
|
# TODO: https://gitlab.com/groups/gitlab-org/-/epics/7054
|
|
|
|
# temporary until we can we properly remove the source columns
|
|
|
|
override :set_member_namespace_id
|
|
|
|
def set_member_namespace_id
|
|
|
|
self.member_namespace_id = project&.project_namespace_id
|
|
|
|
end
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
def send_invite
|
2018-04-23 10:38:37 -04:00
|
|
|
run_after_commit_or_now { notification_service.invite_project_member(self, @raw_invite_token) }
|
2016-03-14 09:13:35 -04:00
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
def post_create_hook
|
2022-03-28 20:09:12 -04:00
|
|
|
# The creator of a personal project gets added as a `ProjectMember`
|
|
|
|
# with `OWNER` access during creation of a personal project,
|
|
|
|
# but we do not want to trigger notifications to the same person who created the personal project.
|
|
|
|
unless project.personal_namespace_holder?(user)
|
2015-02-13 06:01:28 -05:00
|
|
|
event_service.join_project(self.project, self.user)
|
2018-04-23 10:38:37 -04:00
|
|
|
run_after_commit_or_now { notification_service.new_project_member(self) }
|
2015-02-13 06:01:28 -05:00
|
|
|
end
|
2016-01-21 08:23:53 -05:00
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
super
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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_project_member(self) }
|
2015-04-10 09:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
super
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_destroy_hook
|
2016-10-06 11:19:27 -04:00
|
|
|
if expired?
|
|
|
|
event_service.expired_leave_project(self.project, self.user)
|
|
|
|
else
|
|
|
|
event_service.leave_project(self.project, self.user)
|
|
|
|
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
|
2022-05-31 05:08:17 -04:00
|
|
|
run_after_commit_or_now do
|
|
|
|
notification_service.accept_project_invite(self)
|
|
|
|
end
|
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_project_invite(self)
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2015-02-13 05:57:56 -05:00
|
|
|
def event_service
|
|
|
|
EventCreateService.new
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
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
|
|
|
ProjectMember.prepend_mod_with('ProjectMember')
|