summaryrefslogtreecommitdiff
path: root/app/models/project.rb
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2017-06-15 15:19:25 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2017-06-19 19:11:35 +0200
commitc9e277ee01b05da7e359459a0a25bdd9bc7dbca8 (patch)
tree4edc9db2155f8dff7f17bc769784d17f966e196c /app/models/project.rb
parent73bf9413b95d20860c09b3b37737c37add2d1342 (diff)
downloadgitlab-ce-c9e277ee01b05da7e359459a0a25bdd9bc7dbca8.tar.gz
Refactor GroupProjectsFinder#init_collection
This optimises how GroupProjectsFinder builds it collection, producing simpler and faster queries in the process. It also cleans up the code a bit to make it easier to understand.
Diffstat (limited to 'app/models/project.rb')
-rw-r--r--app/models/project.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index bb183e535d9..36ec4f398ca 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -266,6 +266,23 @@ class Project < ActiveRecord::Base
enum auto_cancel_pending_pipelines: { disabled: 0, enabled: 1 }
+ # Returns a collection of projects that is either public or visible to the
+ # logged in user.
+ def self.public_or_visible_to_user(user = nil)
+ if user
+ authorized = user.
+ project_authorizations.
+ select(1).
+ where('project_authorizations.project_id = projects.id')
+
+ levels = Gitlab::VisibilityLevel.levels_for_user(user)
+
+ where('EXISTS (?) OR projects.visibility_level IN (?)', authorized, levels)
+ else
+ public_to_user
+ end
+ end
+
# project features may be "disabled", "internal" or "enabled". If "internal",
# they are only available to team members. This scope returns projects where
# the feature is either enabled, or internal with permission for the user.