2013-01-19 18:52:55 +00:00
|
|
|
class ProjectTeam
|
2013-01-03 19:09:18 +00:00
|
|
|
attr_accessor :project
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2013-01-04 06:43:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut to add users
|
|
|
|
#
|
|
|
|
# Use:
|
|
|
|
# @team << [@user, :master]
|
|
|
|
# @team << [@users, :master]
|
|
|
|
#
|
2014-09-25 22:07:40 +00:00
|
|
|
def <<(args)
|
2013-01-04 06:43:25 +00:00
|
|
|
users = args.first
|
|
|
|
|
|
|
|
if users.respond_to?(:each)
|
|
|
|
add_users(users, args.second)
|
|
|
|
else
|
|
|
|
add_user(users, args.second)
|
|
|
|
end
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
2013-06-26 12:57:02 +00:00
|
|
|
def find(user_id)
|
2014-01-19 18:55:59 +00:00
|
|
|
user = project.users.find_by(id: user_id)
|
2013-06-17 12:38:19 +00:00
|
|
|
|
|
|
|
if group
|
2014-01-19 18:55:59 +00:00
|
|
|
user ||= group.users.find_by(id: user_id)
|
2013-06-17 12:38:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
user
|
2013-06-17 11:55:41 +00:00
|
|
|
end
|
|
|
|
|
2013-06-26 12:57:02 +00:00
|
|
|
def find_tm(user_id)
|
2014-09-14 16:32:51 +00:00
|
|
|
tm = project.project_members.find_by(user_id: user_id)
|
2013-08-27 18:35:41 +00:00
|
|
|
|
|
|
|
# If user is not in project members
|
|
|
|
# we should check for group membership
|
|
|
|
if group && !tm
|
2014-09-14 16:32:51 +00:00
|
|
|
tm = group.group_members.find_by(user_id: user_id)
|
2013-08-27 18:35:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
tm
|
2013-01-22 17:45:13 +00:00
|
|
|
end
|
|
|
|
|
2013-01-03 19:09:18 +00:00
|
|
|
def add_user(user, access)
|
|
|
|
add_users_ids([user.id], access)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_users(users, access)
|
|
|
|
add_users_ids(users.map(&:id), access)
|
|
|
|
end
|
|
|
|
|
2013-01-04 06:43:25 +00:00
|
|
|
def add_users_ids(user_ids, access)
|
2014-09-14 16:32:51 +00:00
|
|
|
ProjectMember.add_users_into_projects(
|
2013-01-03 19:09:18 +00:00
|
|
|
[project.id],
|
|
|
|
user_ids,
|
|
|
|
access
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove all users from project team
|
|
|
|
def truncate
|
2014-09-14 16:32:51 +00:00
|
|
|
ProjectMember.truncate_team(project)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
2013-12-25 07:56:39 +00:00
|
|
|
def users
|
|
|
|
members
|
|
|
|
end
|
|
|
|
|
2013-01-03 19:09:18 +00:00
|
|
|
def members
|
2013-06-22 09:57:05 +00:00
|
|
|
@members ||= fetch_members
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def guests
|
2013-06-17 11:17:32 +00:00
|
|
|
@guests ||= fetch_members(:guests)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reporters
|
2013-06-17 11:17:32 +00:00
|
|
|
@reporters ||= fetch_members(:reporters)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def developers
|
2013-06-17 11:17:32 +00:00
|
|
|
@developers ||= fetch_members(:developers)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def masters
|
2013-06-17 11:17:32 +00:00
|
|
|
@masters ||= fetch_members(:masters)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
2013-01-04 06:43:25 +00:00
|
|
|
|
|
|
|
def import(source_project)
|
|
|
|
target_project = project
|
|
|
|
|
2014-09-14 16:32:51 +00:00
|
|
|
source_team = source_project.project_members.to_a
|
|
|
|
target_user_ids = target_project.project_members.pluck(:user_id)
|
2013-01-04 06:43:25 +00:00
|
|
|
|
|
|
|
source_team.reject! do |tm|
|
|
|
|
# Skip if user already present in team
|
|
|
|
target_user_ids.include?(tm.user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
source_team.map! do |tm|
|
|
|
|
new_tm = tm.dup
|
|
|
|
new_tm.id = nil
|
2014-09-15 08:04:25 +00:00
|
|
|
new_tm.source = target_project
|
2013-01-04 06:43:25 +00:00
|
|
|
new_tm
|
|
|
|
end
|
|
|
|
|
2014-09-14 16:32:51 +00:00
|
|
|
ProjectMember.transaction do
|
2013-01-04 06:43:25 +00:00
|
|
|
source_team.each do |tm|
|
|
|
|
tm.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
2013-06-17 11:17:32 +00:00
|
|
|
|
2014-06-04 08:52:17 +00:00
|
|
|
def guest?(user)
|
2014-06-20 09:54:03 +00:00
|
|
|
max_tm_access(user.id) == Gitlab::Access::GUEST
|
2014-06-04 08:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reporter?(user)
|
2014-06-20 09:54:03 +00:00
|
|
|
max_tm_access(user.id) == Gitlab::Access::REPORTER
|
2014-06-04 08:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def developer?(user)
|
2014-06-20 09:54:03 +00:00
|
|
|
max_tm_access(user.id) == Gitlab::Access::DEVELOPER
|
2014-06-04 08:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def master?(user)
|
2014-06-20 09:54:03 +00:00
|
|
|
max_tm_access(user.id) == Gitlab::Access::MASTER
|
|
|
|
end
|
|
|
|
|
2014-10-08 13:44:25 +00:00
|
|
|
def member?(user_id)
|
|
|
|
!!find_tm(user_id)
|
|
|
|
end
|
|
|
|
|
2014-06-20 09:54:03 +00:00
|
|
|
def max_tm_access(user_id)
|
|
|
|
access = []
|
2014-09-14 16:32:51 +00:00
|
|
|
access << project.project_members.find_by(user_id: user_id).try(:access_field)
|
2014-06-20 09:54:03 +00:00
|
|
|
|
|
|
|
if group
|
2014-09-14 16:32:51 +00:00
|
|
|
access << group.group_members.find_by(user_id: user_id).try(:access_field)
|
2014-06-20 09:54:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
access.compact.max
|
2014-06-04 08:52:17 +00:00
|
|
|
end
|
|
|
|
|
2013-06-17 11:17:32 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_members(level = nil)
|
2014-09-14 16:32:51 +00:00
|
|
|
project_members = project.project_members
|
|
|
|
group_members = group ? group.group_members : []
|
2013-06-17 11:17:32 +00:00
|
|
|
|
|
|
|
if level
|
|
|
|
project_members = project_members.send(level)
|
2013-06-17 12:38:19 +00:00
|
|
|
group_members = group_members.send(level) if group
|
2013-06-17 11:17:32 +00:00
|
|
|
end
|
|
|
|
|
2014-06-04 16:20:25 +00:00
|
|
|
user_ids = project_members.pluck(:user_id)
|
2014-10-09 07:47:47 +00:00
|
|
|
user_ids.push(*group_members.pluck(:user_id)) if group
|
2014-06-04 16:20:25 +00:00
|
|
|
|
|
|
|
User.where(id: user_ids)
|
2013-06-17 11:17:32 +00:00
|
|
|
end
|
2013-06-17 12:38:19 +00:00
|
|
|
|
|
|
|
def group
|
|
|
|
project.group
|
|
|
|
end
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|