Created
August 25, 2011 02:53
-
-
Save mongrelion/1169874 to your computer and use it in GitHub Desktop.
User authorization in controller actions
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 User < ActiveRecord::Base | |
# - Instance Methods - | |
def is_admin? | |
self.role.eql? 'admin' | |
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 UserController < ApplicationController | |
before_filter :authorize_user! | |
def index | |
end | |
def edit | |
end | |
def show | |
end | |
# ... | |
private | |
def authorize_user! | |
redirect = true | |
if current_user | |
if current_user.is_admin? | |
redirect = false | |
else | |
flash[ :error ] = 'Unauthorized access!' | |
redirect = true | |
end | |
end | |
redirect_to root_path if redirect | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment