Skip to content

Instantly share code, notes, and snippets.

@delba
Created May 15, 2013 22:50
Show Gist options
  • Select an option

  • Save delba/5588057 to your computer and use it in GitHub Desktop.

Select an option

Save delba/5588057 to your computer and use it in GitHub Desktop.
Raising an exception if an action isn't explicitly authorized
class ApplicationController < ActionController::Base
# ...
include AuthorizationSystem
after_filter :validate_authorization_checked
end
module AuthorizationSystem
NotAuthorized = Class.new(StandardError)
AuthorizationNotChecked = Class.new(StandardError)
protected
def authorize!
@authorization_checked = true
raise NotAuthorized unless yield
end
def validate_authorization_checked
return if @authorization_checked
raise AuthorizationNotChecked
end
end
class MessagesController < ApplicationController
# ...
def show
@account = Account.find(params[:account_id])
@message = @account.messages.find(params[:id])
authorize! { current_user.can_view?(@message) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment