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

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

192 lines
5.2 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 ProjectMember < Member
2021-10-27 15:23:28 +05:30
extend ::Gitlab::Utils::Override
2019-12-04 20:38:33 +05:30
SOURCE_TYPE = 'Project'
2022-01-26 12:08:38 +05:30
SOURCE_TYPE_FORMAT = /\AProject\z/.freeze
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
belongs_to :project, foreign_key: 'source_id'
2015-04-26 12:48:37 +05:30
2021-04-29 21:17:54 +05:30
delegate :namespace_id, to: :project
2015-04-26 12:48:37 +05:30
# Make sure project member points only to project 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-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
scope :in_project, ->(project) { where(source_id: project.id) }
2019-01-03 12:48:30 +05:30
scope :in_namespaces, ->(groups) do
joins('INNER JOIN projects ON projects.id = members.source_id')
2021-06-08 01:23:25 +05:30
.where(projects: { namespace_id: groups.select(:id) })
2019-01-03 12:48:30 +05:30
end
2015-04-26 12:48:37 +05:30
class << self
2022-08-13 15:12:31 +05:30
# Add members to projects with passed access option
2015-04-26 12:48:37 +05:30
#
# access can be an integer representing a access code
2018-11-18 11:00:15 +05:30
# or symbol like :maintainer representing role
2015-04-26 12:48:37 +05:30
#
# Ex.
2022-08-13 15:12:31 +05:30
# add_members_to_projects(
2015-04-26 12:48:37 +05:30
# project_ids,
# user_ids,
2018-11-18 11:00:15 +05:30
# ProjectMember::MAINTAINER
2015-04-26 12:48:37 +05:30
# )
#
2022-08-13 15:12:31 +05:30
# add_members_to_projects(
2015-04-26 12:48:37 +05:30
# project_ids,
# user_ids,
2018-11-18 11:00:15 +05:30
# :maintainer
2015-04-26 12:48:37 +05:30
# )
#
2022-08-13 15:12:31 +05:30
def add_members_to_projects(project_ids, users, access_level, current_user: nil, expires_at: nil)
2016-11-03 12:29:30 +05:30
self.transaction do
2015-04-26 12:48:37 +05:30
project_ids.each do |project_id|
project = Project.find(project_id)
2022-08-13 15:12:31 +05:30
Members::Projects::CreatorService.add_members( # rubocop:disable CodeReuse/ServiceClass
2016-11-03 12:29:30 +05:30
project,
users,
access_level,
current_user: current_user,
expires_at: expires_at
)
2015-04-26 12:48:37 +05:30
end
end
end
def truncate_teams(project_ids)
ProjectMember.transaction do
members = ProjectMember.where(source_id: project_ids)
2016-02-05 20:25:01 +05:30
2015-04-26 12:48:37 +05:30
members.each do |member|
member.destroy
end
end
true
2021-06-08 01:23:25 +05:30
rescue StandardError
2015-04-26 12:48:37 +05:30
false
end
def truncate_team(project)
truncate_teams [project.id]
end
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 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
def access_level_roles
2015-04-26 12:48:37 +05:30
Gitlab::Access.options
end
end
def project
source
end
2018-03-17 18:26:18 +05:30
def notifiable_options
{ project: project }
end
2021-11-18 22:05:49 +05:30
private
2022-01-26 12:08:38 +05:30
override :access_level_inclusion
def access_level_inclusion
2022-05-07 20:08:51 +05:30
unless access_level.in?(Gitlab::Access.all_values)
errors.add(:access_level, "is not included in the list")
end
2022-01-26 12:08:38 +05:30
end
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
return unless user
# rubocop:disable CodeReuse/ServiceClass
2021-11-18 22:05:49 +05:30
if blocking
2022-08-13 15:12:31 +05:30
AuthorizedProjectUpdate::ProjectRecalculatePerUserWorker.bulk_perform_and_wait([[project.id, user.id]])
2021-11-18 22:05:49 +05:30
else
AuthorizedProjectUpdate::ProjectRecalculatePerUserWorker.perform_async(project.id, user.id)
end
2021-10-27 15:23:28 +05:30
# 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-11-18 22:05:49 +05:30
.execute(blocking: false, priority: UserProjectAccessChangedService::LOW_PRIORITY)
2021-10-27 15:23:28 +05:30
# rubocop:enable CodeReuse/ServiceClass
end
2022-04-04 11:22:00 +05:30
# 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
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_project_member(self, @raw_invite_token) }
2015-04-26 12:48:37 +05:30
super
end
def post_create_hook
2022-06-21 17:19:12 +05:30
# 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-04-26 12:48:37 +05:30
event_service.join_project(self.project, self.user)
2018-10-15 14:42:47 +05:30
run_after_commit_or_now { notification_service.new_project_member(self) }
2015-04-26 12:48:37 +05:30
end
2016-02-05 20:25:01 +05:30
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_project_member(self) }
2015-04-26 12:48:37 +05:30
end
super
end
def post_destroy_hook
2017-08-17 22:00:37 +05:30
if expired?
event_service.expired_leave_project(self.project, self.user)
else
event_service.leave_project(self.project, self.user)
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_project_invite(self)
end
2015-04-26 12:48:37 +05:30
super
end
def after_decline_invite
notification_service.decline_project_invite(self)
super
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ServiceClass
2015-04-26 12:48:37 +05:30
def event_service
EventCreateService.new
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ServiceClass
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
ProjectMember.prepend_mod_with('ProjectMember')