Commit 1fba0710 authored by Stan Hu's avatar Stan Hu

Optimize the invited group link access level check

parent 871723da
...@@ -155,10 +155,13 @@ class ProjectTeam ...@@ -155,10 +155,13 @@ class ProjectTeam
merge_max!(access, group_access) merge_max!(access, group_access)
end end
# Each group produces a list of maximum access level per user. We take the
# max of the values produced by each group.
if project.invited_groups.any? && project.allowed_to_share_with_group? if project.invited_groups.any? && project.allowed_to_share_with_group?
# Not optimized project.project_group_links.each do |group_link|
invited_levels = user_ids.map { |id| [id, max_invited_level(id)] }.to_h invited_access = max_invited_level_for_users(group_link, user_ids)
merge_max!(access, invited_levels) merge_max!(access, invited_access)
end
end end
end end
...@@ -171,20 +174,21 @@ class ProjectTeam ...@@ -171,20 +174,21 @@ class ProjectTeam
private private
def max_invited_level(user_id) # For a given group, return the maximum access level for the user. This is the min of
project.project_group_links.map do |group_link| # the invited access level of the group and the access level of the user within the group.
invited_group = group_link.group # For example, if the group has been given DEVELOPER access but the member has MASTER access,
access = invited_group.group_members.find_by(user_id: user_id).try(:access_field) # the user should receive only DEVELOPER access.
access = Gitlab::Access::NO_ACCESS unless access.present? def max_invited_level_for_users(group_link, user_ids)
invited_group = group_link.group
# If group member has higher access level we should restrict it capped_access_level = group_link.group_access
# to max allowed access level access = invited_group.group_members.access_for_user_ids(user_ids)
if access && access > group_link.group_access
access = group_link.group_access # If the user is not in the list, assume he/she does not have access
end missing_users = user_ids - access.keys
missing_users.each { |id| access[id] = Gitlab::Access::NO_ACCESS }
access
end.compact.max # Cap the maximum access by the invited level access
access.each { |key, value| access[key] = [value, capped_access_level].min }
end end
def fetch_members(level = nil) def fetch_members(level = nil)
......
...@@ -214,20 +214,30 @@ describe ProjectTeam, models: true do ...@@ -214,20 +214,30 @@ describe ProjectTeam, models: true do
group = create(:group) group = create(:group)
group_developer = create(:user) group_developer = create(:user)
second_developer = create(:user)
project.project_group_links.create( project.project_group_links.create(
group: group, group: group,
group_access: Gitlab::Access::DEVELOPER) group_access: Gitlab::Access::DEVELOPER)
group.add_master(promoted_guest) group.add_master(promoted_guest)
group.add_developer(group_developer) group.add_developer(group_developer)
users = [master, reporter, promoted_guest, guest, group_developer].map(&:id) group.add_developer(second_developer)
second_group = create(:group)
project.project_group_links.create(
group: second_group,
group_access: Gitlab::Access::MASTER)
second_group.add_master(second_developer)
users = [master, reporter, promoted_guest, guest, group_developer, second_developer].map(&:id)
expected = { expected = {
master.id => Gitlab::Access::MASTER, master.id => Gitlab::Access::MASTER,
reporter.id => Gitlab::Access::REPORTER, reporter.id => Gitlab::Access::REPORTER,
promoted_guest.id => Gitlab::Access::DEVELOPER, promoted_guest.id => Gitlab::Access::DEVELOPER,
guest.id => Gitlab::Access::GUEST, guest.id => Gitlab::Access::GUEST,
group_developer.id => Gitlab::Access::DEVELOPER group_developer.id => Gitlab::Access::DEVELOPER,
second_developer.id => Gitlab::Access::MASTER
} }
expect(project.team.max_member_access_for_user_ids(users)).to eq(expected) expect(project.team.max_member_access_for_user_ids(users)).to eq(expected)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment