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

219 lines
5.1 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class ProjectTeam
attr_accessor :project
def initialize(project)
@project = project
end
# Shortcut to add users
#
# Use:
# @team << [@user, :master]
# @team << [@users, :master]
#
2015-04-26 12:48:37 +05:30
def <<(args)
users, access, current_user = *args
2014-09-02 18:07:02 +05:30
if users.respond_to?(:each)
2015-04-26 12:48:37 +05:30
add_users(users, access, current_user)
2014-09-02 18:07:02 +05:30
else
2015-04-26 12:48:37 +05:30
add_user(users, access, current_user)
2014-09-02 18:07:02 +05:30
end
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
2015-04-26 12:48:37 +05:30
def add_users(users, access, current_user = nil)
ProjectMember.add_users_into_projects(
2014-09-02 18:07:02 +05:30
[project.id],
2015-04-26 12:48:37 +05:30
users,
access,
current_user
2014-09-02 18:07:02 +05:30
)
end
2015-04-26 12:48:37 +05:30
def add_user(user, access, current_user = nil)
add_users([user], access, current_user)
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
def guests
@guests ||= fetch_members(:guests)
end
def reporters
@reporters ||= fetch_members(:reporters)
end
def developers
@developers ||= fetch_members(:developers)
end
def masters
@masters ||= fetch_members(:masters)
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
def master?(user)
2015-04-26 12:48:37 +05:30
max_member_access(user.id) == Gitlab::Access::MASTER
end
def member?(user, min_member_access = nil)
member = !!find_member(user.id)
if min_member_access
member && max_member_access(user.id) >= min_member_access
else
member
end
2014-09-02 18:07:02 +05:30
end
2015-10-24 18:46:33 +05:30
def human_max_access(user_id)
2016-04-02 18:10:28 +05:30
Gitlab::Access.options_with_owner.key(max_member_access(user_id))
2015-10-24 18:46:33 +05:30
end
# This method assumes project and group members are eager loaded for optimal
# performance.
2015-04-26 12:48:37 +05:30
def max_member_access(user_id)
2014-09-02 18:07:02 +05:30
access = []
2015-10-24 18:46:33 +05:30
2016-08-24 12:49:21 +05:30
access += project.members.where(user_id: user_id).has_access.pluck(:access_level)
2014-09-02 18:07:02 +05:30
if group
2016-08-24 12:49:21 +05:30
access += group.members.where(user_id: user_id).has_access.pluck(:access_level)
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
if project.invited_groups.any? && project.allowed_to_share_with_group?
access << max_invited_level(user_id)
end
access.compact.max
end
private
2016-06-02 11:05:42 +05:30
def max_invited_level(user_id)
project.project_group_links.map do |group_link|
invited_group = group_link.group
access = invited_group.group_members.find_by(user_id: user_id).try(:access_field)
# If group member has higher access level we should restrict it
# to max allowed access level
if access && access > group_link.group_access
access = group_link.group_access
end
access
end.compact.max
2014-09-02 18:07:02 +05:30
end
def fetch_members(level = nil)
2016-08-24 12:49:21 +05:30
project_members = project.members
group_members = group ? group.members : []
2016-06-02 11:05:42 +05:30
invited_members = []
if project.invited_groups.any? && project.allowed_to_share_with_group?
project.project_group_links.each do |group_link|
invited_group = group_link.group
2016-08-24 12:49:21 +05:30
im = invited_group.members
2016-06-02 11:05:42 +05:30
if level
int_level = GroupMember.access_level_roles[level.to_s.singularize.titleize]
# Skip group members if we ask for masters
# but max group access is developers
next if int_level > group_link.group_access
# If we ask for developers and max
# group access is developers we need to provide
# both group master, developers as devs
if int_level == group_link.group_access
im.where("access_level >= ?)", group_link.group_access)
else
im.send(level)
end
end
invited_members << im
end
invited_members = invited_members.flatten.compact
end
2014-09-02 18:07:02 +05:30
if level
project_members = project_members.send(level)
group_members = group_members.send(level) if group
end
user_ids = project_members.pluck(:user_id)
2016-06-02 11:05:42 +05:30
user_ids.push(*invited_members.map(&:user_id)) if invited_members.any?
2015-04-26 12:48:37 +05:30
user_ids.push(*group_members.pluck(:user_id)) if group
2014-09-02 18:07:02 +05:30
User.where(id: user_ids)
end
def group
project.group
end
end