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

209 lines
4.8 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
2018-03-17 18:26:18 +05:30
include BulkMemberAccessLoad
2014-09-02 18:07:02 +05:30
attr_accessor :project
def initialize(project)
@project = project
end
2017-08-17 22:00:37 +05:30
def add_guest(user, current_user: nil)
2018-03-17 18:26:18 +05:30
add_user(user, :guest, current_user: current_user)
2017-08-17 22:00:37 +05:30
end
def add_reporter(user, current_user: nil)
2018-03-17 18:26:18 +05:30
add_user(user, :reporter, current_user: current_user)
2017-08-17 22:00:37 +05:30
end
def add_developer(user, current_user: nil)
2018-03-17 18:26:18 +05:30
add_user(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)
add_user(user, :maintainer, current_user: current_user)
2018-03-17 18:26:18 +05:30
end
2018-11-18 11:00:15 +05:30
# @deprecated
alias_method :add_master, :add_maintainer
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
2016-11-03 12:29:30 +05:30
def add_users(users, access_level, current_user: nil, expires_at: nil)
2017-08-17 22:00:37 +05:30
ProjectMember.add_users(
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,
expires_at: expires_at
2014-09-02 18:07:02 +05:30
)
end
2016-11-03 12:29:30 +05:30
def add_user(user, access_level, current_user: nil, expires_at: nil)
ProjectMember.add_user(
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
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-11-18 11:00:15 +05:30
# @deprecated
alias_method :masters, :maintainers
2018-03-27 19:54:05 +05:30
def owners
@owners ||=
if group
group.owners
else
[project.owner]
end
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
rescue
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
2018-11-18 11:00:15 +05:30
# @deprecated
alias_method :master?, :maintainer?
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)
2018-03-17 18:26:18 +05:30
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)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
end
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
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
ProjectTeam.prepend_if_ee('EE::ProjectTeam')