2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-01-19 18:52:55 +00:00
|
|
|
class ProjectTeam
|
2017-12-07 09:11:41 +00:00
|
|
|
include BulkMemberAccessLoad
|
|
|
|
|
2013-01-03 19:09:18 +00:00
|
|
|
attr_accessor :project
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2013-01-04 06:43:25 +00:00
|
|
|
end
|
|
|
|
|
2016-11-18 12:52:39 +00:00
|
|
|
def add_guest(user, current_user: nil)
|
2017-12-22 08:18:28 +00:00
|
|
|
add_user(user, :guest, current_user: current_user)
|
2016-11-18 12:52:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_reporter(user, current_user: nil)
|
2017-12-22 08:18:28 +00:00
|
|
|
add_user(user, :reporter, current_user: current_user)
|
2016-11-18 12:52:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_developer(user, current_user: nil)
|
2017-12-22 08:18:28 +00:00
|
|
|
add_user(user, :developer, current_user: current_user)
|
2016-11-18 12:52:39 +00:00
|
|
|
end
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
def add_maintainer(user, current_user: nil)
|
|
|
|
add_user(user, :maintainer, current_user: current_user)
|
2017-12-22 08:18:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_role(user, role, current_user: nil)
|
2018-06-25 18:58:41 +00:00
|
|
|
public_send(:"add_#{role}", user, current_user: current_user) # rubocop:disable GitlabSecurity/PublicSend
|
2016-11-18 12:52:39 +00:00
|
|
|
end
|
|
|
|
|
2015-03-13 15:23:45 +00:00
|
|
|
def find_member(user_id)
|
2016-06-27 14:20:57 +00:00
|
|
|
member = project.members.find_by(user_id: user_id)
|
2013-08-27 18:35:41 +00:00
|
|
|
|
|
|
|
# If user is not in project members
|
|
|
|
# we should check for group membership
|
2015-03-13 15:23:45 +00:00
|
|
|
if group && !member
|
2016-06-27 14:20:57 +00:00
|
|
|
member = group.members.find_by(user_id: user_id)
|
2013-08-27 18:35:41 +00:00
|
|
|
end
|
|
|
|
|
2015-03-13 15:23:45 +00:00
|
|
|
member
|
2013-01-22 17:45:13 +00:00
|
|
|
end
|
|
|
|
|
2021-10-25 12:10:19 +00:00
|
|
|
def add_users(users, access_level, current_user: nil, expires_at: nil, tasks_to_be_done: [], tasks_project_id: nil)
|
2021-09-01 09:10:58 +00:00
|
|
|
Members::Projects::BulkCreatorService.add_users( # rubocop:disable CodeReuse/ServiceClass
|
2017-04-21 14:07:42 +00:00
|
|
|
project,
|
2015-04-10 12:46:09 +00:00
|
|
|
users,
|
2016-09-16 15:54:21 +00:00
|
|
|
access_level,
|
2016-08-02 18:37:22 +00:00
|
|
|
current_user: current_user,
|
2021-10-25 12:10:19 +00:00
|
|
|
expires_at: expires_at,
|
|
|
|
tasks_to_be_done: tasks_to_be_done,
|
|
|
|
tasks_project_id: tasks_project_id
|
2013-01-03 19:09:18 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-09-16 15:54:21 +00:00
|
|
|
def add_user(user, access_level, current_user: nil, expires_at: nil)
|
2021-08-17 12:08:42 +00:00
|
|
|
Members::Projects::CreatorService.new(project, # rubocop:disable CodeReuse/ServiceClass
|
2021-06-28 18:08:39 +00:00
|
|
|
user,
|
|
|
|
access_level,
|
|
|
|
current_user: current_user,
|
|
|
|
expires_at: expires_at)
|
|
|
|
.execute
|
2015-04-10 12:46:09 +00:00
|
|
|
end
|
|
|
|
|
2013-01-03 19:09:18 +00:00
|
|
|
# Remove all users from project team
|
|
|
|
def truncate
|
2014-09-14 16:32:51 +00:00
|
|
|
ProjectMember.truncate_team(project)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def members
|
2013-06-22 09:57:05 +00:00
|
|
|
@members ||= fetch_members
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
2016-04-18 16:53:32 +00:00
|
|
|
alias_method :users, :members
|
2013-01-03 19:09:18 +00:00
|
|
|
|
2019-01-14 21:53:37 +00:00
|
|
|
# `members` method uses project_authorizations table which
|
|
|
|
# is updated asynchronously, on project move it still contains
|
|
|
|
# old members who may not have access to the new location,
|
|
|
|
# so we filter out only members of project or project's group
|
|
|
|
def members_in_project_and_ancestors
|
|
|
|
members.where(id: member_user_ids)
|
|
|
|
end
|
|
|
|
|
2021-08-13 15:11:15 +00:00
|
|
|
def members_with_access_levels(access_levels = [])
|
|
|
|
fetch_members(access_levels)
|
|
|
|
end
|
|
|
|
|
2013-01-03 19:09:18 +00:00
|
|
|
def guests
|
2016-11-21 14:26:12 +00:00
|
|
|
@guests ||= fetch_members(Gitlab::Access::GUEST)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reporters
|
2016-11-21 14:26:12 +00:00
|
|
|
@reporters ||= fetch_members(Gitlab::Access::REPORTER)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def developers
|
2016-11-21 14:26:12 +00:00
|
|
|
@developers ||= fetch_members(Gitlab::Access::DEVELOPER)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
def maintainers
|
|
|
|
@maintainers ||= fetch_members(Gitlab::Access::MAINTAINER)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
2013-01-04 06:43:25 +00:00
|
|
|
|
2018-03-07 17:01:32 +00:00
|
|
|
def owners
|
|
|
|
@owners ||=
|
|
|
|
if group
|
|
|
|
group.owners
|
|
|
|
else
|
|
|
|
[project.owner]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-10 12:46:09 +00:00
|
|
|
def import(source_project, current_user = nil)
|
2013-01-04 06:43:25 +00:00
|
|
|
target_project = project
|
|
|
|
|
2015-03-13 15:23:45 +00:00
|
|
|
source_members = source_project.project_members.to_a
|
2014-09-14 16:32:51 +00:00
|
|
|
target_user_ids = target_project.project_members.pluck(:user_id)
|
2013-01-04 06:43:25 +00:00
|
|
|
|
2015-03-13 15:23:45 +00:00
|
|
|
source_members.reject! do |member|
|
2013-01-04 06:43:25 +00:00
|
|
|
# Skip if user already present in team
|
2015-04-10 13:26:53 +00:00
|
|
|
!member.invite? && target_user_ids.include?(member.user_id)
|
2013-01-04 06:43:25 +00:00
|
|
|
end
|
|
|
|
|
2015-03-13 15:23:45 +00:00
|
|
|
source_members.map! do |member|
|
|
|
|
new_member = member.dup
|
|
|
|
new_member.id = nil
|
|
|
|
new_member.source = target_project
|
2015-04-10 12:46:09 +00:00
|
|
|
new_member.created_by = current_user
|
2015-03-13 15:23:45 +00:00
|
|
|
new_member
|
2013-01-04 06:43:25 +00:00
|
|
|
end
|
|
|
|
|
2014-09-14 16:32:51 +00:00
|
|
|
ProjectMember.transaction do
|
2015-03-13 15:23:45 +00:00
|
|
|
source_members.each do |member|
|
|
|
|
member.save
|
2013-01-04 06:43:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError
|
2013-01-04 06:43:25 +00:00
|
|
|
false
|
|
|
|
end
|
2013-06-17 11:17:32 +00:00
|
|
|
|
2014-06-04 08:52:17 +00:00
|
|
|
def guest?(user)
|
2015-03-13 15:23:45 +00:00
|
|
|
max_member_access(user.id) == Gitlab::Access::GUEST
|
2014-06-04 08:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reporter?(user)
|
2015-03-13 15:23:45 +00:00
|
|
|
max_member_access(user.id) == Gitlab::Access::REPORTER
|
2014-06-04 08:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def developer?(user)
|
2015-03-13 15:23:45 +00:00
|
|
|
max_member_access(user.id) == Gitlab::Access::DEVELOPER
|
2014-06-04 08:52:17 +00:00
|
|
|
end
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
def maintainer?(user)
|
|
|
|
max_member_access(user.id) == Gitlab::Access::MAINTAINER
|
2014-06-20 09:54:03 +00:00
|
|
|
end
|
|
|
|
|
2016-11-18 17:15:47 +00:00
|
|
|
# Checks if `user` is authorized for this project, with at least the
|
|
|
|
# `min_access_level` (if given).
|
|
|
|
def member?(user, min_access_level = Gitlab::Access::GUEST)
|
|
|
|
return false unless user
|
|
|
|
|
2017-09-17 06:25:34 +00:00
|
|
|
max_member_access(user.id) >= min_access_level
|
2014-10-08 13:44:25 +00:00
|
|
|
end
|
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
def human_max_access(user_id)
|
2017-07-29 15:04:42 +00:00
|
|
|
Gitlab::Access.human_access(max_member_access(user_id))
|
2016-06-02 16:05:06 +00:00
|
|
|
end
|
|
|
|
|
2016-07-20 04:52:31 +00:00
|
|
|
# Determine the maximum access level for a group of users in bulk.
|
|
|
|
#
|
2016-07-27 00:07:51 +00:00
|
|
|
# Returns a Hash mapping user ID -> maximum access level.
|
2016-07-20 04:52:31 +00:00
|
|
|
def max_member_access_for_user_ids(user_ids)
|
2017-12-07 09:11:41 +00:00
|
|
|
max_member_access_for_resource_ids(User, user_ids, project.id) do |user_ids|
|
|
|
|
project.project_authorizations
|
|
|
|
.where(user: user_ids)
|
|
|
|
.group(:user_id)
|
|
|
|
.maximum(:access_level)
|
2016-08-01 20:11:45 +00:00
|
|
|
end
|
2016-07-20 04:52:31 +00:00
|
|
|
end
|
|
|
|
|
2021-04-13 18:11:28 +00:00
|
|
|
def write_member_access_for_user_id(user_id, project_access_level)
|
|
|
|
merge_value_to_request_store(User, user_id, project.id, project_access_level)
|
|
|
|
end
|
|
|
|
|
2016-07-20 04:52:31 +00:00
|
|
|
def max_member_access(user_id)
|
2017-12-07 09:11:41 +00:00
|
|
|
max_member_access_for_user_ids([user_id])[user_id]
|
2016-03-11 17:46:01 +00:00
|
|
|
end
|
|
|
|
|
2020-09-16 15:09:32 +00:00
|
|
|
def contribution_check_for_user_ids(user_ids)
|
|
|
|
user_ids = user_ids.uniq
|
|
|
|
key = "contribution_check_for_users:#{project.id}"
|
|
|
|
|
|
|
|
Gitlab::SafeRequestStore[key] ||= {}
|
|
|
|
contributors = Gitlab::SafeRequestStore[key] || {}
|
|
|
|
|
|
|
|
user_ids -= contributors.keys
|
|
|
|
|
|
|
|
return contributors if user_ids.empty?
|
|
|
|
|
|
|
|
resource_contributors = project.merge_requests
|
|
|
|
.merged
|
|
|
|
.where(author_id: user_ids, target_branch: project.default_branch.to_s)
|
|
|
|
.pluck(:author_id)
|
|
|
|
.product([true]).to_h
|
|
|
|
|
|
|
|
contributors.merge!(resource_contributors)
|
|
|
|
|
|
|
|
missing_resource_ids = user_ids - resource_contributors.keys
|
|
|
|
|
|
|
|
missing_resource_ids.each do |resource_id|
|
|
|
|
contributors[resource_id] = false
|
|
|
|
end
|
|
|
|
|
|
|
|
contributors
|
|
|
|
end
|
|
|
|
|
|
|
|
def contributor?(user_id)
|
|
|
|
return false if max_member_access(user_id) >= Gitlab::Access::GUEST
|
|
|
|
|
|
|
|
contribution_check_for_user_ids([user_id])[user_id]
|
|
|
|
end
|
|
|
|
|
2016-04-18 16:53:32 +00:00
|
|
|
private
|
2016-03-11 17:46:01 +00:00
|
|
|
|
2013-06-17 11:17:32 +00:00
|
|
|
def fetch_members(level = nil)
|
2016-11-21 14:26:12 +00:00
|
|
|
members = project.authorized_users
|
|
|
|
members = members.where(project_authorizations: { access_level: level }) if level
|
2013-06-17 11:17:32 +00:00
|
|
|
|
2016-11-21 14:26:12 +00:00
|
|
|
members
|
2013-06-17 11:17:32 +00:00
|
|
|
end
|
2013-06-17 12:38:19 +00:00
|
|
|
|
|
|
|
def group
|
|
|
|
project.group
|
|
|
|
end
|
2019-01-14 21:53:37 +00:00
|
|
|
|
|
|
|
def member_user_ids
|
|
|
|
Member.on_project_and_ancestors(project).select(:user_id)
|
|
|
|
end
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
ProjectTeam.prepend_mod_with('ProjectTeam')
|