Skip to content

Commit 39011be

Browse files
author
Andreas Brandl
committed
Extract method User#authorizations_for_projects.
1 parent 0a3fc7e commit 39011be

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

app/finders/snippets_finder.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,7 @@ def projects_for_user
7272
# we can shortcut and just return.
7373
return yield(Project.all) if current_user.full_private_access?
7474

75-
authorized = current_user
76-
.project_authorizations
77-
.select(1)
78-
.where('project_authorizations.project_id = projects.id')
79-
authorized_projects = yield(Project.where('EXISTS (?)', authorized))
75+
authorized_projects = yield(Project.where('EXISTS (?)', current_user.authorizations_for_projects))
8076

8177
levels = Gitlab::VisibilityLevel.levels_for_user(current_user)
8278
visible_projects = yield(Project.where(visibility_level: levels))

app/models/project.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,9 @@ class Project < ActiveRecord::Base
319319
# logged in user.
320320
def self.public_or_visible_to_user(user = nil)
321321
if user
322-
authorized = user
323-
.project_authorizations
324-
.select(1)
325-
.where('project_authorizations.project_id = projects.id')
326-
327-
levels = Gitlab::VisibilityLevel.levels_for_user(user)
328-
329-
where('EXISTS (?) OR projects.visibility_level IN (?)', authorized, levels)
322+
where('EXISTS (?) OR projects.visibility_level IN (?)',
323+
user.authorizations_for_projects,
324+
Gitlab::VisibilityLevel.levels_for_user(user))
330325
else
331326
public_to_user
332327
end
@@ -347,14 +342,11 @@ def self.with_feature_available_for_user(feature, user)
347342
elsif user
348343
column = ProjectFeature.quoted_access_level_column(feature)
349344

350-
authorized = user.project_authorizations.select(1)
351-
.where('project_authorizations.project_id = projects.id')
352-
353345
with_project_feature
354346
.where("#{column} IN (?) OR (#{column} = ? AND EXISTS (?))",
355347
visible,
356348
ProjectFeature::PRIVATE,
357-
authorized)
349+
user.authorizations_for_projects)
358350
else
359351
with_feature_access_level(feature, visible)
360352
end

app/models/user.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,15 @@ def authorized_project?(project, min_access_level = nil)
601601
authorized_projects(min_access_level).exists?({ id: project.id })
602602
end
603603

604+
# Typically used in conjunction with projects table to get projects
605+
# a user has been given access to.
606+
#
607+
# Example use:
608+
# `Project.where('EXISTS(?)', user.authorizations_for_projects)`
609+
def authorizations_for_projects
610+
project_authorizations.select(1).where('project_authorizations.project_id = projects.id')
611+
end
612+
604613
# Returns the projects this user has reporter (or greater) access to, limited
605614
# to at most the given projects.
606615
#

spec/models/user_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,32 @@
16351635
end
16361636
end
16371637

1638+
describe '#authorizations_for_projects' do
1639+
let!(:user) { create(:user) }
1640+
subject { Project.where("EXISTS (?)", user.authorizations_for_projects) }
1641+
1642+
it 'includes projects that belong to a user, but no other projects' do
1643+
owned = create(:project, :private, namespace: user.namespace)
1644+
member = create(:project, :private).tap { |p| p.add_master(user) }
1645+
other = create(:project)
1646+
1647+
expect(subject).to include(owned)
1648+
expect(subject).to include(member)
1649+
expect(subject).not_to include(other)
1650+
end
1651+
1652+
it 'includes projects a user has access to, but no other projects' do
1653+
other_user = create(:user)
1654+
accessible = create(:project, :private, namespace: other_user.namespace) do |project|
1655+
project.add_developer(user)
1656+
end
1657+
other = create(:project)
1658+
1659+
expect(subject).to include(accessible)
1660+
expect(subject).not_to include(other)
1661+
end
1662+
end
1663+
16381664
describe '#authorized_projects', :delete do
16391665
context 'with a minimum access level' do
16401666
it 'includes projects for which the user is an owner' do

0 commit comments

Comments
 (0)