factorize before_filters and layout for projects related controllers

This commit is contained in:
Cyril 2012-09-26 01:21:37 +02:00
parent 0439387be0
commit 078a8f0e66
17 changed files with 33 additions and 101 deletions

View file

@ -76,7 +76,7 @@ class ApplicationController < ActionController::Base
end end
def project def project
@project ||= current_user.projects.find_by_code(params[:project_id]) @project ||= current_user.projects.find_by_code(params[:project_id] || params[:id])
@project || render_404 @project || render_404
end end

View file

@ -1,11 +1,7 @@
require "base64" require "base64"
class CommitsController < ApplicationController class CommitsController < ProjectController
before_filter :project
layout "project"
# Authorize # Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project! before_filter :authorize_read_project!
before_filter :authorize_code_access! before_filter :authorize_code_access!
before_filter :require_non_empty_project before_filter :require_non_empty_project

View file

@ -1,16 +1,9 @@
class DeployKeysController < ApplicationController class DeployKeysController < ProjectController
respond_to :html respond_to :html
layout "project"
before_filter :project
# Authorize # Authorize
before_filter :add_project_abilities
before_filter :authorize_admin_project! before_filter :authorize_admin_project!
def project
@project ||= Project.find_by_code(params[:project_id])
end
def index def index
@keys = @project.deploy_keys.all @keys = @project.deploy_keys.all
end end

View file

@ -1,9 +1,5 @@
class HooksController < ApplicationController class HooksController < ProjectController
before_filter :project
layout "project"
# Authorize # Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project! before_filter :authorize_read_project!
before_filter :authorize_admin_project!, only: [:new, :create, :destroy] before_filter :authorize_admin_project!, only: [:new, :create, :destroy]

View file

@ -1,14 +1,8 @@
class IssuesController < ApplicationController class IssuesController < ProjectController
before_filter :project
before_filter :module_enabled before_filter :module_enabled
before_filter :issue, only: [:edit, :update, :destroy, :show] before_filter :issue, only: [:edit, :update, :destroy, :show]
helper_method :issues_filter helper_method :issues_filter
layout "project"
# Authorize
before_filter :add_project_abilities
# Allow read any issue # Allow read any issue
before_filter :authorize_read_issue! before_filter :authorize_read_issue!

View file

@ -1,12 +1,6 @@
class LabelsController < ApplicationController class LabelsController < ProjectController
before_filter :project
before_filter :module_enabled before_filter :module_enabled
layout "project"
# Authorize
before_filter :add_project_abilities
# Allow read any issue # Allow read any issue
before_filter :authorize_read_issue! before_filter :authorize_read_issue!

View file

@ -1,13 +1,8 @@
class MergeRequestsController < ApplicationController class MergeRequestsController < ProjectController
before_filter :project
before_filter :module_enabled before_filter :module_enabled
before_filter :merge_request, only: [:edit, :update, :destroy, :show, :commits, :diffs, :automerge, :automerge_check, :raw] before_filter :merge_request, only: [:edit, :update, :destroy, :show, :commits, :diffs, :automerge, :automerge_check, :raw]
before_filter :validates_merge_request, only: [:show, :diffs, :raw] before_filter :validates_merge_request, only: [:show, :diffs, :raw]
before_filter :define_show_vars, only: [:show, :diffs] before_filter :define_show_vars, only: [:show, :diffs]
layout "project"
# Authorize
before_filter :add_project_abilities
# Allow read any merge_request # Allow read any merge_request
before_filter :authorize_read_merge_request! before_filter :authorize_read_merge_request!

View file

@ -1,11 +1,6 @@
class MilestonesController < ApplicationController class MilestonesController < ProjectController
before_filter :project
before_filter :module_enabled before_filter :module_enabled
before_filter :milestone, only: [:edit, :update, :destroy, :show] before_filter :milestone, only: [:edit, :update, :destroy, :show]
layout "project"
# Authorize
before_filter :add_project_abilities
# Allow read any milestone # Allow read any milestone
before_filter :authorize_read_milestone! before_filter :authorize_read_milestone!

View file

@ -1,9 +1,5 @@
class NotesController < ApplicationController class NotesController < ProjectController
before_filter :project
# Authorize # Authorize
before_filter :add_project_abilities
before_filter :authorize_read_note! before_filter :authorize_read_note!
before_filter :authorize_write_note!, only: [:create] before_filter :authorize_write_note!, only: [:create]

View file

@ -0,0 +1,16 @@
class ProjectController < ApplicationController
before_filter :project
# Authorize
before_filter :add_project_abilities
layout :determine_layout
protected
def determine_layout
if @project && !@project.new_record?
'project'
else
'application'
end
end
end

View file

@ -1,11 +1,9 @@
require Rails.root.join('lib', 'gitlab', 'graph_commit') require Rails.root.join('lib', 'gitlab', 'graph_commit')
class ProjectsController < ApplicationController class ProjectsController < ProjectController
before_filter :project, except: [:index, :new, :create] skip_before_filter :project, only: [:new, :create]
layout :determine_layout
# Authorize # Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!, except: [:index, :new, :create] before_filter :authorize_read_project!, except: [:index, :new, :create]
before_filter :authorize_admin_project!, only: [:edit, :update, :destroy] before_filter :authorize_admin_project!, only: [:edit, :update, :destroy]
before_filter :require_non_empty_project, only: [:blob, :tree, :graph] before_filter :require_non_empty_project, only: [:blob, :tree, :graph]
@ -93,19 +91,4 @@ class ProjectsController < ApplicationController
format.html { redirect_to root_path } format.html { redirect_to root_path }
end end
end end
protected
def project
@project ||= Project.find_by_code(params[:id])
@project || render_404
end
def determine_layout
if @project && !@project.new_record?
"project"
else
"application"
end
end
end end

View file

@ -1,16 +1,11 @@
class ProtectedBranchesController < ApplicationController class ProtectedBranchesController < ProjectController
before_filter :project
# Authorize # Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project! before_filter :authorize_read_project!
before_filter :require_non_empty_project before_filter :require_non_empty_project
before_filter :authorize_admin_project!, only: [:destroy, :create] before_filter :authorize_admin_project!, only: [:destroy, :create]
before_filter :render_full_content before_filter :render_full_content
layout "project"
def index def index
@branches = @project.protected_branches.all @branches = @project.protected_branches.all
@protected_branch = @project.protected_branches.new @protected_branch = @project.protected_branches.new

View file

@ -1,11 +1,9 @@
require 'github/markup' require 'github/markup'
class RefsController < ApplicationController class RefsController < ProjectController
include Gitlab::Encode include Gitlab::Encode
before_filter :project
# Authorize # Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project! before_filter :authorize_read_project!
before_filter :authorize_code_access! before_filter :authorize_code_access!
before_filter :require_non_empty_project before_filter :require_non_empty_project
@ -14,8 +12,6 @@ class RefsController < ApplicationController
before_filter :define_tree_vars, only: [:tree, :blob, :blame, :logs_tree] before_filter :define_tree_vars, only: [:tree, :blob, :blame, :logs_tree]
before_filter :render_full_content before_filter :render_full_content
layout "project"
def switch def switch
respond_to do |format| respond_to do |format|
format.html do format.html do

View file

@ -1,15 +1,10 @@
class RepositoriesController < ApplicationController class RepositoriesController < ProjectController
before_filter :project
# Authorize # Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project! before_filter :authorize_read_project!
before_filter :authorize_code_access! before_filter :authorize_code_access!
before_filter :require_non_empty_project before_filter :require_non_empty_project
before_filter :render_full_content before_filter :render_full_content
layout "project"
def show def show
@activities = @project.commits_with_refs(20) @activities = @project.commits_with_refs(20)
end end

View file

@ -1,10 +1,5 @@
class SnippetsController < ApplicationController class SnippetsController < ProjectController
before_filter :project
before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw] before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw]
layout "project"
# Authorize
before_filter :add_project_abilities
# Allow read any snippet # Allow read any snippet
before_filter :authorize_read_snippet! before_filter :authorize_read_snippet!

View file

@ -1,9 +1,5 @@
class TeamMembersController < ApplicationController class TeamMembersController < ProjectController
before_filter :project
layout "project"
# Authorize # Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project! before_filter :authorize_read_project!
before_filter :authorize_admin_project!, except: [:index, :show] before_filter :authorize_admin_project!, except: [:index, :show]

View file

@ -1,10 +1,7 @@
class WikisController < ApplicationController class WikisController < ProjectController
before_filter :project
before_filter :add_project_abilities
before_filter :authorize_read_wiki! before_filter :authorize_read_wiki!
before_filter :authorize_write_wiki!, only: [:edit, :create, :history] before_filter :authorize_write_wiki!, only: [:edit, :create, :history]
before_filter :authorize_admin_wiki!, only: :destroy before_filter :authorize_admin_wiki!, only: :destroy
layout "project"
def pages def pages
@wikis = @project.wikis.group(:slug).order("created_at") @wikis = @project.wikis.group(:slug).order("created_at")