Commit 2ea5ef0b authored by Ahmad Sherif's avatar Ahmad Sherif

Update ProjectTeam#fetch_members to use project authorizations

parent 747959a8
...@@ -127,6 +127,7 @@ class Project < ActiveRecord::Base ...@@ -127,6 +127,7 @@ class Project < ActiveRecord::Base
has_many :protected_branches, dependent: :destroy has_many :protected_branches, dependent: :destroy
has_many :project_authorizations, dependent: :destroy has_many :project_authorizations, dependent: :destroy
has_many :authorized_users, through: :project_authorizations, source: :user, class_name: 'User'
has_many :project_members, -> { where(requested_at: nil) }, dependent: :destroy, as: :source has_many :project_members, -> { where(requested_at: nil) }, dependent: :destroy, as: :source
alias_method :members, :project_members alias_method :members, :project_members
has_many :users, through: :project_members has_many :users, through: :project_members
......
...@@ -80,19 +80,19 @@ class ProjectTeam ...@@ -80,19 +80,19 @@ class ProjectTeam
alias_method :users, :members alias_method :users, :members
def guests def guests
@guests ||= fetch_members(:guests) @guests ||= fetch_members(Gitlab::Access::GUEST)
end end
def reporters def reporters
@reporters ||= fetch_members(:reporters) @reporters ||= fetch_members(Gitlab::Access::REPORTER)
end end
def developers def developers
@developers ||= fetch_members(:developers) @developers ||= fetch_members(Gitlab::Access::DEVELOPER)
end end
def masters def masters
@masters ||= fetch_members(:masters) @masters ||= fetch_members(Gitlab::Access::MASTER)
end end
def import(source_project, current_user = nil) def import(source_project, current_user = nil)
...@@ -185,59 +185,13 @@ class ProjectTeam ...@@ -185,59 +185,13 @@ class ProjectTeam
private private
def fetch_members(level = nil) def fetch_members(level = nil)
project_members = project.members members = project.authorized_users
group_members = group ? group.members : [] members = members.where(project_authorizations: { access_level: level }) if level
if level members
project_members = project_members.public_send(level)
group_members = group_members.public_send(level) if group
end
user_ids = project_members.pluck(:user_id)
invited_members = fetch_invited_members(level)
user_ids.push(*invited_members.map(&:user_id)) if invited_members.any?
user_ids.push(*group_members.pluck(:user_id)) if group
User.where(id: user_ids)
end end
def group def group
project.group project.group
end end
def project_shared_with_group?
project.invited_groups.any? && project.allowed_to_share_with_group?
end
def fetch_invited_members(level = nil)
invited_members = []
return invited_members unless project_shared_with_group?
project.project_group_links.includes(group: [:group_members]).each do |link|
invited_group_members = link.group.members
if level
numeric_level = GroupMember.access_level_roles[level.to_s.singularize.titleize]
# If we're asked for a level that's higher than the group's access,
# there's nothing left to do
next if numeric_level > link.group_access
# Make sure we include everyone _above_ the requested level as well
invited_group_members =
if numeric_level == link.group_access
invited_group_members.where("access_level >= ?", link.group_access)
else
invited_group_members.public_send(level)
end
end
invited_members << invited_group_members
end
invited_members.flatten.compact
end
end end
---
title: Use authorized projects in ProjectTeam
merge_request:
author:
Gitlab::Seeder.quiet do require 'sidekiq/testing'
Group.all.each do |group| require './db/fixtures/support/serialized_transaction'
User.all.sample(4).each do |user|
if group.add_user(user, Gitlab::Access.values.sample).persisted? Sidekiq::Testing.inline! do
print '.' Gitlab::Seeder.quiet do
else Group.all.each do |group|
print 'F' User.all.sample(4).each do |user|
if group.add_user(user, Gitlab::Access.values.sample).persisted?
print '.'
else
print 'F'
end
end end
end end
end
Project.all.each do |project| Project.all.each do |project|
User.all.sample(4).each do |user| User.all.sample(4).each do |user|
if project.team << [user, Gitlab::Access.values.sample] if project.team << [user, Gitlab::Access.values.sample]
print '.' print '.'
else else
print 'F' print 'F'
end
end end
end end
end end
......
...@@ -118,7 +118,7 @@ describe ProjectTeam, models: true do ...@@ -118,7 +118,7 @@ describe ProjectTeam, models: true do
context 'group project' do context 'group project' do
let(:group) { create(:group) } let(:group) { create(:group) }
let(:project) { create(:empty_project, group: group) } let!(:project) { create(:empty_project, group: group) }
it 'returns project members' do it 'returns project members' do
group_member = create(:group_member, group: group) group_member = create(:group_member, group: group)
......
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