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

201 lines
4.4 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)
2016-09-13 17:45:13 +05:30
add_users(users, access, current_user: current_user)
2014-09-02 18:07:02 +05:30
else
2016-09-13 17:45:13 +05:30
add_user(users, access, current_user: current_user)
2014-09-02 18:07:02 +05:30
end
end
2017-08-17 22:00:37 +05:30
def add_guest(user, current_user: nil)
self << [user, :guest, current_user]
end
def add_reporter(user, current_user: nil)
self << [user, :reporter, current_user]
end
def add_developer(user, current_user: nil)
self << [user, :developer, current_user]
end
def add_master(user, current_user: nil)
self << [user, :master, current_user]
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
2016-11-03 12:29:30 +05:30
def add_users(users, access_level, current_user: nil, expires_at: nil)
2017-08-17 22:00:37 +05:30
ProjectMember.add_users(
project,
2015-04-26 12:48:37 +05:30
users,
2016-11-03 12:29:30 +05:30
access_level,
2016-09-13 17:45:13 +05:30
current_user: current_user,
expires_at: expires_at
2014-09-02 18:07:02 +05:30
)
end
2016-11-03 12:29:30 +05:30
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-26 12:48:37 +05:30
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
2017-08-17 22:00:37 +05:30
@guests ||= fetch_members(Gitlab::Access::GUEST)
2014-09-02 18:07:02 +05:30
end
def reporters
2017-08-17 22:00:37 +05:30
@reporters ||= fetch_members(Gitlab::Access::REPORTER)
2014-09-02 18:07:02 +05:30
end
def developers
2017-08-17 22:00:37 +05:30
@developers ||= fetch_members(Gitlab::Access::DEVELOPER)
2014-09-02 18:07:02 +05:30
end
def masters
2017-08-17 22:00:37 +05:30
@masters ||= fetch_members(Gitlab::Access::MASTER)
2014-09-02 18:07:02 +05:30
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
2017-08-17 22:00:37 +05:30
# 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
user.authorized_project?(project, min_access_level)
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
2016-09-13 17:45:13 +05:30
# Determine the maximum access level for a group of users in bulk.
#
# Returns a Hash mapping user ID -> maximum access level.
def max_member_access_for_user_ids(user_ids)
user_ids = user_ids.uniq
key = "max_member_access:#{project.id}"
2015-10-24 18:46:33 +05:30
2016-09-13 17:45:13 +05:30
access = {}
2014-09-02 18:07:02 +05:30
2016-09-13 17:45:13 +05:30
if RequestStore.active?
RequestStore.store[key] ||= {}
access = RequestStore.store[key]
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
# Lookup only the IDs we need
user_ids = user_ids - access.keys
2017-08-17 22:00:37 +05:30
return access if user_ids.empty?
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
users_access = project.project_authorizations.
where(user: user_ids).
group(:user_id).
maximum(:access_level)
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
access.merge!(users_access)
2016-09-13 17:45:13 +05:30
access
end
def max_member_access(user_id)
2017-08-17 22:00:37 +05:30
max_member_access_for_user_ids([user_id])[user_id] || Gitlab::Access::NO_ACCESS
2016-06-02 11:05:42 +05:30
end
private
2016-06-02 11:05:42 +05:30
2014-09-02 18:07:02 +05:30
def fetch_members(level = nil)
2017-08-17 22:00:37 +05:30
members = project.authorized_users
members = members.where(project_authorizations: { access_level: level }) if level
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
members
2014-09-02 18:07:02 +05:30
end
def group
project.group
end
end