Created
January 24, 2012 15:52
-
-
Save egrouse/1670798 to your computer and use it in GitHub Desktop.
Rails redirection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdminController < ApplicationController | |
# Execute the filter to authenticate users (::authenticate) | |
before_filter :authenticate | |
def user | |
end | |
def unit | |
end | |
def index | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
# Execute the filter to select logged in user | |
before_filter :fetch_logged_user | |
# Find the logged in user in DB to verify | |
def fetch_logged_user | |
unless session[:user_id].blank? | |
# Find the user by stored session ID | |
@logged_user = User.find( session[:user_id] ) | |
end | |
# rescue ActiveRecord::RecordNotFound | |
end | |
# Filter employed to perform actual authentication | |
def authenticate | |
# Check if the login user is set | |
unless @logged_user | |
redirect_to login_path, :status => 401 # Redirect to the login form | |
flash[:msg] = 'You must be logged in to access this page'; # Set a flash message | |
return false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment