Skip to content

Instantly share code, notes, and snippets.

@mongrelion
Created August 25, 2011 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mongrelion/1169874 to your computer and use it in GitHub Desktop.
Save mongrelion/1169874 to your computer and use it in GitHub Desktop.
User authorization in controller actions
class User < ActiveRecord::Base
# - Instance Methods -
def is_admin?
self.role.eql? 'admin'
end
end
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