debian-mirror-gitlab/app/models/project_team.rb

241 lines
6.4 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class ProjectTeam
attr_accessor :project
def initialize(project)
@project = project
end
2017-08-17 22:00:37 +05:30
def add_guest(user, current_user: nil)
2022-08-13 15:12:31 +05:30
add_member(user, :guest, current_user: current_user)
2017-08-17 22:00:37 +05:30
end
def add_reporter(user, current_user: nil)
2022-08-13 15:12:31 +05:30
add_member(user, :reporter, current_user: current_user)
2017-08-17 22:00:37 +05:30
end
def add_developer(user, current_user: nil)
2022-08-13 15:12:31 +05:30
add_member(user, :developer, current_user: current_user)
2017-08-17 22:00:37 +05:30
end
2018-11-18 11:00:15 +05:30
def add_maintainer(user, current_user: nil)
2022-08-13 15:12:31 +05:30
add_member(user, :maintainer, current_user: current_user)
2018-03-17 18:26:18 +05:30
end
2022-05-07 20:08:51 +05:30
def add_owner(user, current_user: nil)
2022-08-13 15:12:31 +05:30
add_member(user, :owner, current_user: current_user)
2022-05-07 20:08:51 +05:30
end
2018-03-17 18:26:18 +05:30
def add_role(user, role, current_user: nil)
2018-11-08 19:23:39 +05:30
public_send(:"add_#{role}", user, current_user: current_user) # rubocop:disable GitlabSecurity/PublicSend
2017-08-17 22:00:37 +05:30
end
2015-04-26 12:48:37 +05:30
def find_member(user_id)
2016-08-24 12:49:21 +05:30
member = project.members.find_by(user_id: user_id)
2014-09-02 18:07:02 +05:30
# If user is not in project members
# we should check for group membership
2015-04-26 12:48:37 +05:30
if group && !member
2016-08-24 12:49:21 +05:30
member = group.members.find_by(user_id: user_id)
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
member
2014-09-02 18:07:02 +05:30
end
2022-08-13 15:12:31 +05:30
def add_members(users, access_level, current_user: nil, expires_at: nil, tasks_to_be_done: [], tasks_project_id: nil)
Members::Projects::CreatorService.add_members( # rubocop:disable CodeReuse/ServiceClass
2017-08-17 22:00:37 +05:30
project,
2015-04-26 12:48:37 +05:30
users,
2016-11-03 12:29:30 +05:30
access_level,
2016-09-13 17:45:13 +05:30
current_user: current_user,
2021-12-11 22:18:48 +05:30
expires_at: expires_at,
tasks_to_be_done: tasks_to_be_done,
tasks_project_id: tasks_project_id
2014-09-02 18:07:02 +05:30
)
end
2022-08-13 15:12:31 +05:30
def add_member(user, access_level, current_user: nil, expires_at: nil)
Members::Projects::CreatorService.add_member( # rubocop:disable CodeReuse/ServiceClass
2022-07-23 23:45:48 +05:30
project,
user,
access_level,
current_user: current_user,
expires_at: expires_at)
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
# Remove all users from project team
def truncate
2015-04-26 12:48:37 +05:30
ProjectMember.truncate_team(project)
2014-09-02 18:07:02 +05:30
end
def members
@members ||= fetch_members
end
alias_method :users, :members
2014-09-02 18:07:02 +05:30
2019-02-02 18:00:53 +05:30
# `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-10-27 15:23:28 +05:30
def members_with_access_levels(access_levels = [])
fetch_members(access_levels)
end
2014-09-02 18:07:02 +05:30
def guests
2017-08-17 22:00:37 +05:30
@guests ||= fetch_members(Gitlab::Access::GUEST)
2014-09-02 18:07:02 +05:30
end
def reporters
2017-08-17 22:00:37 +05:30
@reporters ||= fetch_members(Gitlab::Access::REPORTER)
2014-09-02 18:07:02 +05:30
end
def developers
2017-08-17 22:00:37 +05:30
@developers ||= fetch_members(Gitlab::Access::DEVELOPER)
2014-09-02 18:07:02 +05:30
end
2018-11-18 11:00:15 +05:30
def maintainers
@maintainers ||= fetch_members(Gitlab::Access::MAINTAINER)
2014-09-02 18:07:02 +05:30
end
2018-03-27 19:54:05 +05:30
def owners
@owners ||=
if group
group.owners
else
2022-05-07 20:08:51 +05:30
# workaround until we migrate Project#owners to have membership with
# OWNER access level
Array.wrap(fetch_members(Gitlab::Access::OWNER)) | Array.wrap(project.owner)
2018-03-27 19:54:05 +05:30
end
end
2022-04-04 11:22:00 +05:30
def owner?(user)
owners.include?(user)
end
2015-04-26 12:48:37 +05:30
def import(source_project, current_user = nil)
2014-09-02 18:07:02 +05:30
target_project = project
2015-04-26 12:48:37 +05:30
source_members = source_project.project_members.to_a
target_user_ids = target_project.project_members.pluck(:user_id)
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
source_members.reject! do |member|
2014-09-02 18:07:02 +05:30
# Skip if user already present in team
2015-04-26 12:48:37 +05:30
!member.invite? && target_user_ids.include?(member.user_id)
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
source_members.map! do |member|
new_member = member.dup
new_member.id = nil
new_member.source = target_project
new_member.created_by = current_user
new_member
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
ProjectMember.transaction do
source_members.each do |member|
member.save
2014-09-02 18:07:02 +05:30
end
end
true
2021-06-08 01:23:25 +05:30
rescue StandardError
2014-09-02 18:07:02 +05:30
false
end
def guest?(user)
2015-04-26 12:48:37 +05:30
max_member_access(user.id) == Gitlab::Access::GUEST
2014-09-02 18:07:02 +05:30
end
def reporter?(user)
2015-04-26 12:48:37 +05:30
max_member_access(user.id) == Gitlab::Access::REPORTER
2014-09-02 18:07:02 +05:30
end
def developer?(user)
2015-04-26 12:48:37 +05:30
max_member_access(user.id) == Gitlab::Access::DEVELOPER
2014-09-02 18:07:02 +05:30
end
2018-11-18 11:00:15 +05:30
def maintainer?(user)
max_member_access(user.id) == Gitlab::Access::MAINTAINER
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
# 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
2018-03-17 18:26:18 +05:30
max_member_access(user.id) >= min_access_level
2014-09-02 18:07:02 +05:30
end
2015-10-24 18:46:33 +05:30
def human_max_access(user_id)
2018-03-17 18:26:18 +05:30
Gitlab::Access.human_access(max_member_access(user_id))
2015-10-24 18:46:33 +05:30
end
2016-09-13 17:45:13 +05:30
# Determine the maximum access level for a group of users in bulk.
#
# Returns a Hash mapping user ID -> maximum access level.
def max_member_access_for_user_ids(user_ids)
2022-05-07 20:08:51 +05:30
Gitlab::SafeRequestLoader.execute(resource_key: project.max_member_access_for_resource_key(User),
resource_ids: user_ids,
default_value: Gitlab::Access::NO_ACCESS) do |user_ids|
2018-03-17 18:26:18 +05:30
project.project_authorizations
.where(user: user_ids)
.group(:user_id)
.maximum(:access_level)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
end
2021-04-29 21:17:54 +05:30
def write_member_access_for_user_id(user_id, project_access_level)
2021-12-07 22:27:20 +05:30
project.merge_value_to_request_store(User, user_id, project_access_level)
2021-04-29 21:17:54 +05:30
end
2022-07-16 23:28:13 +05:30
def purge_member_access_cache_for_user_id(user_id)
project.purge_resource_id_from_request_store(User, user_id)
end
2016-09-13 17:45:13 +05:30
def max_member_access(user_id)
2018-03-17 18:26:18 +05:30
max_member_access_for_user_ids([user_id])[user_id]
2016-06-02 11:05:42 +05:30
end
2020-11-24 15:15:51 +05:30
def contribution_check_for_user_ids(user_ids)
2022-05-07 20:08:51 +05:30
Gitlab::SafeRequestLoader.execute(resource_key: "contribution_check_for_users:#{project.id}",
resource_ids: user_ids,
default_value: false) do |user_ids|
project.merge_requests
.merged
.where(author_id: user_ids, target_branch: project.default_branch.to_s)
.pluck(:author_id)
.product([true]).to_h
2020-11-24 15:15:51 +05:30
end
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
private
2016-06-02 11:05:42 +05:30
2014-09-02 18:07:02 +05:30
def fetch_members(level = nil)
2017-08-17 22:00:37 +05:30
members = project.authorized_users
members = members.where(project_authorizations: { access_level: level }) if level
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
members
2014-09-02 18:07:02 +05:30
end
def group
project.group
end
2019-02-02 18:00:53 +05:30
def member_user_ids
Member.on_project_and_ancestors(project).select(:user_id)
end
2014-09-02 18:07:02 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
ProjectTeam.prepend_mod_with('ProjectTeam')