Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: admin-only maintenance mode

I'd like to put my app into maintenance mode but still have admins / moderators be able to log in and use the site.

Two reasons:

  1. I'm making some changes within the app that are best applied using the in-app interface rather than the console.

  2. Moderators don't have access to the console, but can definitely help with the maintenance tasks we have ahead of us.

How would you recommend this be done? I have been experimenting with setting an environment variable ADMIN_MODE and changing all the permissions when it is true, but that seems pretty cludgy and slow.

I'm using CanCan and Devise, for what it's worth, but I'm definitely open to any suggestions.

like image 283
Andrew Avatar asked Jul 13 '11 05:07

Andrew


2 Answers

This would probably be a fairly simple solution:

class ApplicationController < ActionController::Base
  before_filter :check_admin_mode

  protected

  def check_admin_mode
    if ENV['ADMIN_MODE'] && controller_name != 'sessions' && !current_user.admin?
      redirect_to '/maintenance.html'
    end
  end
end
like image 159
aNoble Avatar answered Nov 06 '22 07:11

aNoble


I think aNoble's solution is fine, another way might be to get your webserver to do this, I use Capistrano's cap deploy:web:disable task and then mod_rewrite to conditionally redirect either to the maintenance page or allow specific users through by IP address, but you may be able to write your own conditions.

# Redirect to system maintenance if exists - used by cap deploy:web:disable
RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif)$
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.php -f 
# Allow me through
RewriteCond %{REMOTE_ADDR} "!^XXX\.XXX\.XXX\.XXX"
RewriteCond %{SCRIPT_FILENAME} !maintenance.php [NC]
RewriteRule ^.*$ /system/maintenance.php [L]
like image 23
Paul Groves Avatar answered Nov 06 '22 05:11

Paul Groves