Sha256: 3b51b7f55d5e8fc85f133d1e88c5befff64c33fb2af3989a9217287545b94798

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

  before_action :authenticate_normal!

  class AuthenticationError < SecurityError; end
  class AuthorizationError < SecurityError; end

  rescue_from AuthenticationError do |exception|
    flash[:error] = exception.to_s
    redirect_to :root
  end

  rescue_from AuthorizationError do |exception|
    flash[:error] = exception.to_s
    redirect_to :root
  end

  def authenticate_current_user! user
    raise AuthorizationError unless current_user == user or current_user.system?
  end

  def authenticate_role! role, resource = nil
    return unless user_signed_in?
    unless current_user.has_role? role
      raise AuthenticationError, "#{current_user.name} not authenticated as a #{role} user"
    end
  end

  def authenticate_any_role! *roles
    return unless user_signed_in?
    unless current_user.has_any_role? *roles
      raise AuthenticationError, "#{current_user.name} not authenticated as any of #{roles.join(", ")}"
    end
  end

  Role::USER_ROLES.each do |role|
    define_method "authenticate_#{role.to_s}!" do
      authenticate_role! role
    end
  end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
crosstie-0.0.6 lib/crosstie/templates/authorization/application_controller.rb
crosstie-0.0.5 lib/crosstie/templates/authorization/application_controller.rb
crosstie-0.0.4 lib/crosstie/templates/authorization/application_controller.rb
crosstie-0.0.3 lib/crosstie/templates/authorization/application_controller.rb
crosstie-0.0.2 lib/crosstie/templates/authorization/application_controller.rb