Created
March 30, 2012 17:44
-
-
Save brandon-beacher/2253303 to your computer and use it in GitHub Desktop.
This file contains 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
# drop this in lib/forbidden_error.rb | |
class ForbiddenError < StandardError | |
end |
This file contains 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 | |
rescue_from ForbiddenError, :with => :forbidden | |
private | |
def forbid | |
raise ForbiddenError | |
end | |
def forbidden | |
respond_to do |format| | |
format.html { render :text => "Permission denied", :status => :forbidden } | |
format.all { head :forbidden } | |
end | |
end | |
end |
This file contains 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 UsersController < ApplicationController | |
before_filter :require_user | |
def show | |
@user = User.find(params[:id]) | |
forbid unless current_user.can_show_user?(@user) | |
end | |
end |
This file contains 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 | |
def can_show_user?(user) | |
system_admin? || self?(user) | |
end | |
def self?(user) | |
self == user | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment