Sha256: 2d939c50a518e535192a219e43b56ea869e9411f02829bcab69beeacb9aff1a5

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

  before_action :authenticate_normal!, unless: :devise_controller?

  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

1 entries across 1 versions & 1 rubygems

Version Path
crosstie-0.0.7 lib/crosstie/templates/authorization/application_controller.rb