summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Brandl <abrandl@gitlab.com>2018-02-13 14:14:19 +0100
committerAndreas Brandl <abrandl@gitlab.com>2018-02-19 15:26:55 +0100
commit0af3812b0e9867ba2831ed8debd4183217faa244 (patch)
treeca4acc818c987b3154ebc88a15fc8219a328a0ed
parent7956f10e4982c283e2a32162f4f432e6b4cc4321 (diff)
downloadgitlab-ce-0af3812b0e9867ba2831ed8debd4183217faa244.tar.gz
Shortcut when all levels visible.
-rw-r--r--app/models/project.rb6
-rw-r--r--lib/gitlab/visibility_level.rb7
-rw-r--r--spec/lib/gitlab/visibility_level_spec.rb22
3 files changed, 34 insertions, 1 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index 2dc3dd38099..8bf220b2ae4 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -325,6 +325,12 @@ class Project < ActiveRecord::Base
levels = Gitlab::VisibilityLevel.levels_for_user(user)
+ if Gitlab::VisibilityLevel.all_levels?(levels)
+ # If the user is allowed to see all projects,
+ # we can shortcut and just return.
+ return all
+ end
+
authorized_projects = where('EXISTS (?)', authorized).select(:id)
visible_projects = where('visibility_level IN (?)', levels).select(:id)
diff --git a/lib/gitlab/visibility_level.rb b/lib/gitlab/visibility_level.rb
index 2612208a927..d0ec5d57aab 100644
--- a/lib/gitlab/visibility_level.rb
+++ b/lib/gitlab/visibility_level.rb
@@ -20,6 +20,7 @@ module Gitlab
PRIVATE = 0 unless const_defined?(:PRIVATE)
INTERNAL = 10 unless const_defined?(:INTERNAL)
PUBLIC = 20 unless const_defined?(:PUBLIC)
+ ALL_LEVELS = [PRIVATE, INTERNAL, PUBLIC].freeze unless const_defined?(:ALL_LEVELS)
class << self
delegate :values, to: :options
@@ -28,7 +29,7 @@ module Gitlab
return [PUBLIC] unless user
if user.full_private_access?
- [PRIVATE, INTERNAL, PUBLIC]
+ ALL_LEVELS
elsif user.external?
[PUBLIC]
else
@@ -36,6 +37,10 @@ module Gitlab
end
end
+ def all_levels?(levels = [])
+ levels&.sort == ALL_LEVELS
+ end
+
def string_values
string_options.keys
end
diff --git a/spec/lib/gitlab/visibility_level_spec.rb b/spec/lib/gitlab/visibility_level_spec.rb
index 2c1146ceff5..71eff6e81ef 100644
--- a/spec/lib/gitlab/visibility_level_spec.rb
+++ b/spec/lib/gitlab/visibility_level_spec.rb
@@ -50,6 +50,28 @@ describe Gitlab::VisibilityLevel do
end
end
+ describe '.all_levels?' do
+ let(:levels) do
+ [
+ Gitlab::VisibilityLevel::PUBLIC,
+ Gitlab::VisibilityLevel::INTERNAL,
+ Gitlab::VisibilityLevel::PRIVATE
+ ].shuffle
+ end
+
+ it 'returns true only when given all levels defined at once' do
+ expect(described_class.all_levels?(levels)).to be_truthy
+ end
+
+ it 'returns true for ALL_LEVELS' do
+ expect(described_class.all_levels?(Gitlab::VisibilityLevel::ALL_LEVELS)).to be_truthy
+ end
+
+ it 'returns false if any one level is missing' do
+ expect(described_class.all_levels?(levels[0..-2])).to be_falsey
+ end
+ end
+
describe '.allowed_levels' do
it 'only includes the levels that arent restricted' do
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::INTERNAL])