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

135 lines
2.4 KiB
Ruby
Raw Normal View History

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
# Shortcut to add users
#
# Use:
# @team << [@user, :master]
# @team << [@users, :master]
#
def << args
users = args.first
if users.respond_to?(:each)
add_users(users, args.second)
else
add_user(users, args.second)
end
2013-01-03 14:09:18 -05:00
end
def find(user_id)
2013-06-17 07:55:41 -04:00
user = project.users.find_by_id(user_id)
if group
user ||= group.users.find_by_id(user_id)
end
user
2013-06-17 07:55:41 -04:00
end
def find_tm(user_id)
tm = project.users_projects.find_by_user_id(user_id)
# If user is not in project members
# we should check for group membership
if group && !tm
tm = group.users_groups.find_by_user_id(user_id)
end
tm
2013-01-22 12:45:13 -05:00
end
2013-01-03 14:09:18 -05: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
def add_users_ids(user_ids, access)
2013-01-03 14:09:18 -05:00
UsersProject.add_users_into_projects(
[project.id],
user_ids,
access
)
end
# Remove all users from project team
def truncate
UsersProject.truncate_team(project)
end
def members
2013-06-22 05:57:05 -04:00
@members ||= fetch_members
2013-01-03 14:09:18 -05:00
end
def guests
@guests ||= fetch_members(:guests)
2013-01-03 14:09:18 -05:00
end
def reporters
@reporters ||= fetch_members(:reporters)
2013-01-03 14:09:18 -05:00
end
def developers
@developers ||= fetch_members(:developers)
2013-01-03 14:09:18 -05:00
end
def masters
@masters ||= fetch_members(:masters)
2013-01-03 14:09:18 -05:00
end
def import(source_project)
target_project = project
2013-12-14 08:43:48 -05:00
source_team = source_project.users_projects.to_a
target_team = target_project.users_projects.to_a
target_user_ids = target_team.map(&:user_id)
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
new_tm.project_id = target_project.id
new_tm
end
UsersProject.transaction do
source_team.each do |tm|
tm.save
end
end
true
rescue
false
end
private
def fetch_members(level = nil)
project_members = project.users_projects
group_members = group ? group.users_groups : []
if level
project_members = project_members.send(level)
group_members = group_members.send(level) if group
end
(project_members + group_members).map(&:user).uniq
end
def group
project.group
end
2013-01-03 14:09:18 -05:00
end