2013-01-19 13:52:55 -05:00
|
|
|
class ProjectTeam
|
2017-12-07 04:11:41 -05:00
|
|
|
include BulkMemberAccessLoad
|
|
|
|
|
2013-01-03 14:09:18 -05:00
|
|
|
attr_accessor :project
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2013-01-04 01:43:25 -05:00
|
|
|
end
|
|
|
|
|
2016-11-18 07:52:39 -05:00
|
|
|
def add_guest(user, current_user: nil)
|
2017-12-22 03:18:28 -05:00
|
|
|
add_user(user, :guest, current_user: current_user)
|
2016-11-18 07:52:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_reporter(user, current_user: nil)
|
2017-12-22 03:18:28 -05:00
|
|
|
add_user(user, :reporter, current_user: current_user)
|
2016-11-18 07:52:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_developer(user, current_user: nil)
|
2017-12-22 03:18:28 -05:00
|
|
|
add_user(user, :developer, current_user: current_user)
|
2016-11-18 07:52:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_master(user, current_user: nil)
|
2017-12-22 03:18:28 -05:00
|
|
|
add_user(user, :master, current_user: current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_role(user, role, current_user: nil)
|
|
|
|
send(:"add_#{role}", user, current_user: current_user) # rubocop:disable GitlabSecurity/PublicSend
|
2016-11-18 07:52:39 -05:00
|
|
|
end
|
|
|
|
|
2015-03-13 11:23:45 -04:00
|
|
|
def find_member(user_id)
|
2016-06-27 10:20:57 -04:00
|
|
|
member = project.members.find_by(user_id: user_id)
|
2013-08-27 14:35:41 -04:00
|
|
|
|
|
|
|
# If user is not in project members
|
|
|
|
# we should check for group membership
|
2015-03-13 11:23:45 -04:00
|
|
|
if group && !member
|
2016-06-27 10:20:57 -04:00
|
|
|
member = group.members.find_by(user_id: user_id)
|
2013-08-27 14:35:41 -04:00
|
|
|
end
|
|
|
|
|
2015-03-13 11:23:45 -04:00
|
|
|
member
|
2013-01-22 12:45:13 -05:00
|
|
|
end
|
|
|
|
|
2016-09-16 11:54:21 -04:00
|
|
|
def add_users(users, access_level, current_user: nil, expires_at: nil)
|
2017-04-21 10:07:42 -04:00
|
|
|
ProjectMember.add_users(
|
|
|
|
project,
|
2015-04-10 08:46:09 -04:00
|
|
|
users,
|
2016-09-16 11:54:21 -04:00
|
|
|
access_level,
|
2016-08-02 14:37:22 -04:00
|
|
|
current_user: current_user,
|
|
|
|
expires_at: expires_at
|
2013-01-03 14:09:18 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-09-16 11:54:21 -04:00
|
|
|
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-10 08:46:09 -04:00
|
|
|
end
|
|
|
|
|
2013-01-03 14:09:18 -05:00
|
|
|
# Remove all users from project team
|
|
|
|
def truncate
|
2014-09-14 12:32:51 -04:00
|
|
|
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
|
2016-04-18 12:53:32 -04:00
|
|
|
alias_method :users, :members
|
2013-01-03 14:09:18 -05:00
|
|
|
|
|
|
|
def guests
|
2016-11-21 09:26:12 -05:00
|
|
|
@guests ||= fetch_members(Gitlab::Access::GUEST)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reporters
|
2016-11-21 09:26:12 -05:00
|
|
|
@reporters ||= fetch_members(Gitlab::Access::REPORTER)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def developers
|
2016-11-21 09:26:12 -05:00
|
|
|
@developers ||= fetch_members(Gitlab::Access::DEVELOPER)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def masters
|
2016-11-21 09:26:12 -05:00
|
|
|
@masters ||= fetch_members(Gitlab::Access::MASTER)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
2013-01-04 01:43:25 -05:00
|
|
|
|
2015-04-10 08:46:09 -04:00
|
|
|
def import(source_project, current_user = nil)
|
2013-01-04 01:43:25 -05:00
|
|
|
target_project = project
|
|
|
|
|
2015-03-13 11:23:45 -04:00
|
|
|
source_members = source_project.project_members.to_a
|
2014-09-14 12:32:51 -04:00
|
|
|
target_user_ids = target_project.project_members.pluck(:user_id)
|
2013-01-04 01:43:25 -05:00
|
|
|
|
2015-03-13 11:23:45 -04:00
|
|
|
source_members.reject! do |member|
|
2013-01-04 01:43:25 -05:00
|
|
|
# Skip if user already present in team
|
2015-04-10 09:26:53 -04:00
|
|
|
!member.invite? && target_user_ids.include?(member.user_id)
|
2013-01-04 01:43:25 -05:00
|
|
|
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
|
2015-04-10 08:46:09 -04:00
|
|
|
new_member.created_by = current_user
|
2015-03-13 11:23:45 -04:00
|
|
|
new_member
|
2013-01-04 01:43:25 -05:00
|
|
|
end
|
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
ProjectMember.transaction do
|
2015-03-13 11:23:45 -04:00
|
|
|
source_members.each do |member|
|
|
|
|
member.save
|
2013-01-04 01:43:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
2013-06-17 07:17:32 -04:00
|
|
|
|
2014-06-04 04:52:17 -04:00
|
|
|
def guest?(user)
|
2015-03-13 11:23:45 -04:00
|
|
|
max_member_access(user.id) == Gitlab::Access::GUEST
|
2014-06-04 04:52:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reporter?(user)
|
2015-03-13 11:23:45 -04:00
|
|
|
max_member_access(user.id) == Gitlab::Access::REPORTER
|
2014-06-04 04:52:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def developer?(user)
|
2015-03-13 11:23:45 -04:00
|
|
|
max_member_access(user.id) == Gitlab::Access::DEVELOPER
|
2014-06-04 04:52:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def master?(user)
|
2015-03-13 11:23:45 -04:00
|
|
|
max_member_access(user.id) == Gitlab::Access::MASTER
|
2014-06-20 05:54:03 -04:00
|
|
|
end
|
|
|
|
|
2016-11-18 12:15:47 -05: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 02:25:34 -04:00
|
|
|
max_member_access(user.id) >= min_access_level
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
2016-06-02 12:05:06 -04:00
|
|
|
def human_max_access(user_id)
|
2017-07-29 11:04:42 -04:00
|
|
|
Gitlab::Access.human_access(max_member_access(user_id))
|
2016-06-02 12:05:06 -04:00
|
|
|
end
|
|
|
|
|
2016-07-20 00:52:31 -04:00
|
|
|
# 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.
|
2016-07-20 00:52:31 -04:00
|
|
|
def max_member_access_for_user_ids(user_ids)
|
2017-12-07 04:11:41 -05: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 16:11:45 -04:00
|
|
|
end
|
2016-07-20 00:52:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def max_member_access(user_id)
|
2017-12-07 04:11:41 -05:00
|
|
|
max_member_access_for_user_ids([user_id])[user_id]
|
2016-03-11 12:46:01 -05:00
|
|
|
end
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
private
|
2016-03-11 12:46:01 -05:00
|
|
|
|
2013-06-17 07:17:32 -04:00
|
|
|
def fetch_members(level = nil)
|
2016-11-21 09:26:12 -05:00
|
|
|
members = project.authorized_users
|
|
|
|
members = members.where(project_authorizations: { access_level: level }) if level
|
2013-06-17 07:17:32 -04:00
|
|
|
|
2016-11-21 09:26:12 -05:00
|
|
|
members
|
2013-06-17 07:17:32 -04:00
|
|
|
end
|
2013-06-17 08:38:19 -04:00
|
|
|
|
|
|
|
def group
|
|
|
|
project.group
|
|
|
|
end
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|