File tree 4 files changed +40
-17
lines changed
4 files changed +40
-17
lines changed Original file line number Diff line number Diff line change @@ -72,11 +72,7 @@ def projects_for_user
72
72
# we can shortcut and just return.
73
73
return yield ( Project . all ) if current_user . full_private_access?
74
74
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 ) )
80
76
81
77
levels = Gitlab ::VisibilityLevel . levels_for_user ( current_user )
82
78
visible_projects = yield ( Project . where ( visibility_level : levels ) )
Original file line number Diff line number Diff line change @@ -319,14 +319,9 @@ class Project < ActiveRecord::Base
319
319
# logged in user.
320
320
def self . public_or_visible_to_user ( user = nil )
321
321
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 ) )
330
325
else
331
326
public_to_user
332
327
end
@@ -347,14 +342,11 @@ def self.with_feature_available_for_user(feature, user)
347
342
elsif user
348
343
column = ProjectFeature . quoted_access_level_column ( feature )
349
344
350
- authorized = user . project_authorizations . select ( 1 )
351
- . where ( 'project_authorizations.project_id = projects.id' )
352
-
353
345
with_project_feature
354
346
. where ( "#{ column } IN (?) OR (#{ column } = ? AND EXISTS (?))" ,
355
347
visible ,
356
348
ProjectFeature ::PRIVATE ,
357
- authorized )
349
+ user . authorizations_for_projects )
358
350
else
359
351
with_feature_access_level ( feature , visible )
360
352
end
Original file line number Diff line number Diff line change @@ -601,6 +601,15 @@ def authorized_project?(project, min_access_level = nil)
601
601
authorized_projects ( min_access_level ) . exists? ( { id : project . id } )
602
602
end
603
603
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
+
604
613
# Returns the projects this user has reporter (or greater) access to, limited
605
614
# to at most the given projects.
606
615
#
Original file line number Diff line number Diff line change 1635
1635
end
1636
1636
end
1637
1637
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
+
1638
1664
describe '#authorized_projects' , :delete do
1639
1665
context 'with a minimum access level' do
1640
1666
it 'includes projects for which the user is an owner' do
You can’t perform that action at this time.
0 commit comments