Commit 925183ed7a8eb392e008764483f59c319e22a59c

Authored by Robert Speicher
1 parent 83f24de3

Add an AdminController base class for Admin controllers

Handles stuff that's shared across admin controllers.
app/controllers/admin/dashboard_controller.rb
1   -class Admin::DashboardController < ApplicationController
2   - layout "admin"
3   - before_filter :authenticate_user!
4   - before_filter :authenticate_admin!
5   -
  1 +class Admin::DashboardController < AdminController
6 2 def index
7 3 @workers = Resque.workers
8 4 @pending_jobs = Resque.size(:post_receive)
... ...
app/controllers/admin/hooks_controller.rb
1   -class Admin::HooksController < ApplicationController
2   - layout "admin"
3   - before_filter :authenticate_user!
4   - before_filter :authenticate_admin!
5   -
  1 +class Admin::HooksController < AdminController
6 2 def index
7 3 @hooks = SystemHook.all
8 4 @hook = SystemHook.new
... ... @@ -15,7 +11,7 @@ class Admin::HooksController &lt; ApplicationController
15 11 redirect_to admin_hooks_path, notice: 'Hook was successfully created.'
16 12 else
17 13 @hooks = SystemHook.all
18   - render :index
  14 + render :index
19 15 end
20 16 end
21 17  
... ...
app/controllers/admin/logs_controller.rb
1   -class Admin::LogsController < ApplicationController
2   - layout "admin"
3   - before_filter :authenticate_user!
4   - before_filter :authenticate_admin!
  1 +class Admin::LogsController < AdminController
5 2 end
6   -
... ...
app/controllers/admin/projects_controller.rb
1   -class Admin::ProjectsController < ApplicationController
2   - layout "admin"
3   - before_filter :authenticate_user!
4   - before_filter :authenticate_admin!
  1 +class Admin::ProjectsController < AdminController
5 2 before_filter :admin_project, only: [:edit, :show, :update, :destroy, :team_update]
6 3  
7 4 def index
... ... @@ -43,7 +40,7 @@ class Admin::ProjectsController &lt; ApplicationController
43 40 def update
44 41 owner_id = params[:project].delete(:owner_id)
45 42  
46   - if owner_id
  43 + if owner_id
47 44 @admin_project.owner = User.find(owner_id)
48 45 end
49 46  
... ... @@ -60,7 +57,7 @@ class Admin::ProjectsController &lt; ApplicationController
60 57 redirect_to admin_projects_url, notice: 'Project was successfully deleted.'
61 58 end
62 59  
63   - private
  60 + private
64 61  
65 62 def admin_project
66 63 @admin_project = Project.find_by_code(params[:id])
... ...
app/controllers/admin/resque_controller.rb
1   -class Admin::ResqueController < ApplicationController
2   - layout 'admin'
  1 +class Admin::ResqueController < AdminController
3 2 def show
4 3 end
5   -end
6 4 \ No newline at end of file
  5 +end
... ...
app/controllers/admin/team_members_controller.rb
1   -class Admin::TeamMembersController < ApplicationController
2   - layout "admin"
3   - before_filter :authenticate_user!
4   - before_filter :authenticate_admin!
5   -
  1 +class Admin::TeamMembersController < AdminController
6 2 def edit
7 3 @admin_team_member = UsersProject.find(params[:id])
8 4 end
... ...
app/controllers/admin/users_controller.rb
1   -class Admin::UsersController < ApplicationController
2   - layout "admin"
3   - before_filter :authenticate_user!
4   - before_filter :authenticate_admin!
5   -
  1 +class Admin::UsersController < AdminController
6 2 def index
7 3 @admin_users = User.scoped
8 4 @admin_users = @admin_users.filter(params[:filter])
... ... @@ -24,7 +20,7 @@ class Admin::UsersController &lt; ApplicationController
24 20 @admin_user = User.find(params[:id])
25 21  
26 22 UsersProject.user_bulk_import(
27   - @admin_user,
  23 + @admin_user,
28 24 params[:project_ids],
29 25 params[:project_access]
30 26 )
... ... @@ -41,22 +37,22 @@ class Admin::UsersController &lt; ApplicationController
41 37 @admin_user = User.find(params[:id])
42 38 end
43 39  
44   - def block
  40 + def block
45 41 @admin_user = User.find(params[:id])
46 42  
47 43 if @admin_user.block
48 44 redirect_to :back, alert: "Successfully blocked"
49   - else
  45 + else
50 46 redirect_to :back, alert: "Error occured. User was not blocked"
51 47 end
52 48 end
53 49  
54   - def unblock
  50 + def unblock
55 51 @admin_user = User.find(params[:id])
56 52  
57 53 if @admin_user.update_attribute(:blocked, false)
58 54 redirect_to :back, alert: "Successfully unblocked"
59   - else
  55 + else
60 56 redirect_to :back, alert: "Error occured. User was not unblocked"
61 57 end
62 58 end
... ...
app/controllers/admin_controller.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +# Provides a base class for Admin controllers to subclass
  2 +#
  3 +# Automatically sets the layout and ensures an administrator is logged in
  4 +class AdminController < ApplicationController
  5 + layout 'admin'
  6 + before_filter :authenticate_admin!
  7 +
  8 + def authenticate_admin!
  9 + return render_404 unless current_user.is_admin?
  10 + end
  11 +end
... ...
app/controllers/application_controller.rb
... ... @@ -84,10 +84,6 @@ class ApplicationController &lt; ActionController::Base
84 84 abilities << Ability
85 85 end
86 86  
87   - def authenticate_admin!
88   - return render_404 unless current_user.is_admin?
89   - end
90   -
91 87 def authorize_project!(action)
92 88 return access_denied! unless can?(current_user, action, project)
93 89 end
... ...