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
|
2013-01-04 01:43:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut to add users
|
|
|
|
#
|
|
|
|
# Use:
|
|
|
|
# @team << [@user, :master]
|
|
|
|
# @team << [@users, :master]
|
|
|
|
#
|
2014-09-25 18:07:40 -04:00
|
|
|
def <<(args)
|
2015-04-10 08:46:09 -04:00
|
|
|
users, access, current_user = *args
|
2013-01-04 01:43:25 -05:00
|
|
|
|
|
|
|
if users.respond_to?(:each)
|
2015-04-10 08:46:09 -04:00
|
|
|
add_users(users, access, current_user)
|
2013-01-04 01:43:25 -05:00
|
|
|
else
|
2015-04-10 08:46:09 -04:00
|
|
|
add_user(users, access, current_user)
|
2013-01-04 01:43:25 -05:00
|
|
|
end
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
2013-06-26 08:57:02 -04:00
|
|
|
def find(user_id)
|
2014-01-19 13:55:59 -05:00
|
|
|
user = project.users.find_by(id: user_id)
|
2013-06-17 08:38:19 -04:00
|
|
|
|
|
|
|
if group
|
2014-01-19 13:55:59 -05:00
|
|
|
user ||= group.users.find_by(id: user_id)
|
2013-06-17 08:38:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
user
|
2013-06-17 07:55:41 -04:00
|
|
|
end
|
|
|
|
|
2015-03-13 11:23:45 -04:00
|
|
|
def find_member(user_id)
|
|
|
|
member = project.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
|
|
|
|
member = group.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
|
|
|
|
|
2015-04-10 08:46:09 -04:00
|
|
|
def add_users(users, access, current_user = nil)
|
2014-09-14 12:32:51 -04:00
|
|
|
ProjectMember.add_users_into_projects(
|
2013-01-03 14:09:18 -05:00
|
|
|
[project.id],
|
2015-04-10 08:46:09 -04:00
|
|
|
users,
|
|
|
|
access,
|
|
|
|
current_user
|
2013-01-03 14:09:18 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-04-10 08:46:09 -04:00
|
|
|
def add_user(user, access, current_user = nil)
|
|
|
|
add_users([user], access, current_user)
|
|
|
|
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
|
|
|
|
|
2013-12-25 02:56:39 -05:00
|
|
|
def users
|
|
|
|
members
|
|
|
|
end
|
|
|
|
|
2013-01-03 14:09:18 -05:00
|
|
|
def members
|
2013-06-22 05:57:05 -04:00
|
|
|
@members ||= fetch_members
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def guests
|
2013-06-17 07:17:32 -04:00
|
|
|
@guests ||= fetch_members(:guests)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reporters
|
2013-06-17 07:17:32 -04:00
|
|
|
@reporters ||= fetch_members(:reporters)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def developers
|
2013-06-17 07:17:32 -04:00
|
|
|
@developers ||= fetch_members(:developers)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def masters
|
2013-06-17 07:17:32 -04:00
|
|
|
@masters ||= fetch_members(:masters)
|
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
|
|
|
|
|
2014-10-08 09:44:25 -04:00
|
|
|
def member?(user_id)
|
2015-03-13 11:23:45 -04:00
|
|
|
!!find_member(user_id)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
2015-10-02 08:10:33 -04:00
|
|
|
def human_max_access(user_id)
|
|
|
|
Gitlab::Access.options.key max_member_access(user_id)
|
|
|
|
end
|
|
|
|
|
2015-10-14 08:54:04 -04:00
|
|
|
# This method assumes project and group members are eager loaded for optimal
|
|
|
|
# performance.
|
2015-03-13 11:23:45 -04:00
|
|
|
def max_member_access(user_id)
|
2014-06-20 05:54:03 -04:00
|
|
|
access = []
|
2015-10-14 08:54:04 -04:00
|
|
|
|
|
|
|
project.project_members.each do |member|
|
|
|
|
if member.user_id == user_id
|
|
|
|
access << member.access_field if member.access_field
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2014-06-20 05:54:03 -04:00
|
|
|
|
|
|
|
if group
|
2015-10-14 08:54:04 -04:00
|
|
|
group.group_members.each do |member|
|
|
|
|
if member.user_id == user_id
|
|
|
|
access << member.access_field if member.access_field
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2014-06-20 05:54:03 -04:00
|
|
|
end
|
|
|
|
|
2015-10-14 08:54:04 -04:00
|
|
|
access.max
|
2014-06-04 04:52:17 -04:00
|
|
|
end
|
|
|
|
|
2013-06-17 07:17:32 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_members(level = nil)
|
2014-09-14 12:32:51 -04:00
|
|
|
project_members = project.project_members
|
|
|
|
group_members = group ? group.group_members : []
|
2013-06-17 07:17:32 -04:00
|
|
|
|
|
|
|
if level
|
|
|
|
project_members = project_members.send(level)
|
2013-06-17 08:38:19 -04:00
|
|
|
group_members = group_members.send(level) if group
|
2013-06-17 07:17:32 -04:00
|
|
|
end
|
|
|
|
|
2014-06-04 12:20:25 -04:00
|
|
|
user_ids = project_members.pluck(:user_id)
|
2014-10-09 03:47:47 -04:00
|
|
|
user_ids.push(*group_members.pluck(:user_id)) if group
|
2014-06-04 12:20:25 -04:00
|
|
|
|
|
|
|
User.where(id: user_ids)
|
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
|