gitlab-org--gitlab-foss/app/controllers/projects/application_controller.rb

100 lines
2.6 KiB
Ruby
Raw Normal View History

class Projects::ApplicationController < ApplicationController
include RoutableActions
2016-03-20 20:03:53 +00:00
skip_before_action :authenticate_user!
before_action :redirect_git_extension
2016-03-21 23:09:20 +00:00
before_action :project
before_action :repository
2015-04-30 17:06:18 +00:00
layout 'project'
2016-03-20 20:03:53 +00:00
helper_method :repository, :can_collaborate_with_project?
private
def redirect_git_extension
# Redirect from
# localhost/group/project.git
# to
# localhost/group/project
#
2017-05-05 00:06:01 +00:00
redirect_to url_for(params.merge(format: nil)) if params[:format] == 'git'
end
def project
2017-05-05 17:48:01 +00:00
return @project if @project
return nil unless params[:project_id] || params[:id]
2017-05-05 17:48:01 +00:00
path = File.join(params[:namespace_id], params[:project_id] || params[:id])
auth_proc = ->(project) { !project.pending_delete? }
2017-05-05 17:48:01 +00:00
@project = find_routable!(Project, path, extra_authorization_proc: auth_proc)
2016-03-20 20:03:53 +00:00
end
def build_canonical_path(project)
params[:namespace_id] = project.namespace.to_param
params[:project_id] = project.to_param
url_for(params)
end
2016-03-20 20:03:53 +00:00
def repository
@repository ||= project.repository
end
def can_collaborate_with_project?(project = nil)
project ||= @project
can?(current_user, :push_code, project) ||
(current_user && current_user.already_forked?(project))
end
def authorize_action!(action)
unless can?(current_user, action, project)
return access_denied!
end
2016-03-20 20:03:53 +00:00
end
def check_project_feature_available!(feature)
render_404 unless project.feature_available?(feature, current_user)
end
def check_issuables_available!
render_404 unless project.feature_available?(:issues, current_user) ||
project.feature_available?(:merge_requests, current_user)
end
2016-03-20 20:03:53 +00:00
def method_missing(method_sym, *arguments, &block)
case method_sym.to_s
when /\Aauthorize_(.*)!\z/
authorize_action!($1.to_sym)
when /\Acheck_(.*)_available!\z/
check_project_feature_available!($1.to_sym)
2016-03-20 20:03:53 +00:00
else
super
end
2016-03-20 20:03:53 +00:00
end
2016-03-20 20:03:53 +00:00
def require_non_empty_project
# Be sure to return status code 303 to avoid a double DELETE:
# http://api.rubyonrails.org/classes/ActionController/Redirecting.html
redirect_to project_path(@project), status: 303 if @project.empty_repo?
end
def require_branch_head
2016-06-20 13:38:54 +00:00
unless @repository.branch_exists?(@ref)
redirect_to(
project_tree_path(@project, @ref),
2015-12-08 21:30:40 +00:00
notice: "This action is not allowed unless you are on a branch"
)
end
end
def apply_diff_view_cookie!
cookies.permanent[:diff_view] = params.delete(:view) if params[:view].present?
end
def require_pages_enabled!
not_found unless Gitlab.config.pages.enabled
end
end