summaryrefslogtreecommitdiff
path: root/app/controllers/clusters/base_controller.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers/clusters/base_controller.rb')
-rw-r--r--app/controllers/clusters/base_controller.rb42
1 files changed, 42 insertions, 0 deletions
diff --git a/app/controllers/clusters/base_controller.rb b/app/controllers/clusters/base_controller.rb
new file mode 100644
index 00000000000..2804b236d17
--- /dev/null
+++ b/app/controllers/clusters/base_controller.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+class Clusters::BaseController < ApplicationController
+ include RoutableActions
+
+ skip_before_action :authenticate_user!
+ before_action :require_project_id
+ before_action :project, if: :project_type?
+ before_action :repository, if: :project_type?
+ before_action :authorize_read_cluster!
+
+ private
+
+ # We can extend to `#group_type?` in the future
+ def require_project_id
+ not_found unless project_type?
+ end
+
+ def project
+ @project ||= find_routable!(Project, File.join(params[:namespace_id], params[:project_id]))
+ end
+
+ def repository
+ @repository ||= project.repository
+ end
+
+ def authorize_read_cluster!
+ access_denied! unless can?(current_user, :read_cluster, clusterable)
+ end
+
+ def authorize_create_cluster!
+ access_denied! unless can?(current_user, :create_cluster, clusterable)
+ end
+
+ def clusterable
+ project if project_type?
+ end
+
+ def project_type?
+ params[:project_id].present?
+ end
+end