gitlab-org--gitlab-foss/app/models/project_team.rb

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

241 lines
6.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-01-19 13:52:55 -05:00
class ProjectTeam
2013-01-03 14:09:18 -05:00
attr_accessor :project
def initialize(project)
@project = project
end
def add_guest(user, current_user: nil)
add_member(user, :guest, current_user: current_user)
end
def add_reporter(user, current_user: nil)
add_member(user, :reporter, current_user: current_user)
end
def add_developer(user, current_user: nil)
add_member(user, :developer, current_user: current_user)
end
def add_maintainer(user, current_user: nil)
add_member(user, :maintainer, current_user: current_user)
end
def add_owner(user, current_user: nil)
add_member(user, :owner, current_user: current_user)
end
def add_role(user, role, current_user: nil)
2018-06-25 14:58:41 -04:00
public_send(:"add_#{role}", user, current_user: current_user) # rubocop:disable GitlabSecurity/PublicSend
end
2015-03-13 11:23:45 -04:00
def find_member(user_id)
member = project.members.find_by(user_id: user_id)
# If user is not in project members
# we should check for group membership
2015-03-13 11:23:45 -04:00
if group && !member
member = group.members.find_by(user_id: user_id)
end
2015-03-13 11:23:45 -04:00
member
2013-01-22 12:45:13 -05:00
end
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
project,
users,
access_level,
current_user: current_user,
expires_at: expires_at,
tasks_to_be_done: tasks_to_be_done,
tasks_project_id: tasks_project_id
2013-01-03 14:09:18 -05:00
)
end
def add_member(user, access_level, current_user: nil, expires_at: nil)
Members::Projects::CreatorService.add_member( # rubocop:disable CodeReuse/ServiceClass
project,
user,
access_level,
current_user: current_user,
expires_at: expires_at)
end
2013-01-03 14:09:18 -05:00
# Remove all users from project team
def truncate
ProjectMember.truncate_team(project)
2013-01-03 14:09:18 -05:00
end
def members
2013-06-22 05:57:05 -04:00
@members ||= fetch_members
2013-01-03 14:09:18 -05:00
end
alias_method :users, :members
2013-01-03 14:09:18 -05: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
def members_with_access_levels(access_levels = [])
fetch_members(access_levels)
end
2013-01-03 14:09:18 -05:00
def guests
@guests ||= fetch_members(Gitlab::Access::GUEST)
2013-01-03 14:09:18 -05:00
end
def reporters
@reporters ||= fetch_members(Gitlab::Access::REPORTER)
2013-01-03 14:09:18 -05:00
end
def developers
@developers ||= fetch_members(Gitlab::Access::DEVELOPER)
2013-01-03 14:09:18 -05:00
end
def maintainers
@maintainers ||= fetch_members(Gitlab::Access::MAINTAINER)
2013-01-03 14:09:18 -05:00
end
def owners
@owners ||=
if group
group.owners
else
# workaround until we migrate Project#owners to have membership with
# OWNER access level
Array.wrap(fetch_members(Gitlab::Access::OWNER)) | Array.wrap(project.owner)
end
end
def owner?(user)
owners.include?(user)
end
def import(source_project, current_user = nil)
target_project = project
2015-03-13 11:23:45 -04:00
source_members = source_project.project_members.to_a
target_user_ids = target_project.project_members.pluck(:user_id)
2015-03-13 11:23:45 -04:00
source_members.reject! do |member|
# Skip if user already present in team
2015-04-10 09:26:53 -04:00
!member.invite? && target_user_ids.include?(member.user_id)
end
2015-03-13 11:23:45 -04:00
source_members.map! do |member|
new_member = member.dup
new_member.id = nil
new_member.source = target_project
new_member.created_by = current_user
2015-03-13 11:23:45 -04:00
new_member
end
ProjectMember.transaction do
2015-03-13 11:23:45 -04:00
source_members.each do |member|
member.save
end
end
true
rescue StandardError
false
end
def guest?(user)
2015-03-13 11:23:45 -04:00
max_member_access(user.id) == Gitlab::Access::GUEST
end
def reporter?(user)
2015-03-13 11:23:45 -04:00
max_member_access(user.id) == Gitlab::Access::REPORTER
end
def developer?(user)
2015-03-13 11:23:45 -04:00
max_member_access(user.id) == Gitlab::Access::DEVELOPER
end
def maintainer?(user)
max_member_access(user.id) == Gitlab::Access::MAINTAINER
end
# 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
max_member_access(user.id) >= min_access_level
2014-10-08 09:44:25 -04:00
end
def human_max_access(user_id)
Gitlab::Access.human_access(max_member_access(user_id))
end
# Determine the maximum access level for a group of users in bulk.
#
2016-07-26 20:07:51 -04:00
# Returns a Hash mapping user ID -> maximum access level.
def max_member_access_for_user_ids(user_ids)
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|
project.project_authorizations
.where(user: user_ids)
.group(:user_id)
.maximum(:access_level)
end
end
def write_member_access_for_user_id(user_id, project_access_level)
project.merge_value_to_request_store(User, user_id, project_access_level)
end
def purge_member_access_cache_for_user_id(user_id)
project.purge_resource_id_from_request_store(User, user_id)
end
def max_member_access(user_id)
max_member_access_for_user_ids([user_id])[user_id]
end
def contribution_check_for_user_ids(user_ids)
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
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
def fetch_members(level = nil)
members = project.authorized_users
members = members.where(project_authorizations: { access_level: level }) if level
members
end
def group
project.group
end
def member_user_ids
Member.on_project_and_ancestors(project).select(:user_id)
end
2013-01-03 14:09:18 -05:00
end
ProjectTeam.prepend_mod_with('ProjectTeam')