Sha256: 837820ce8cc34d764cae77e317de27c5107c2babeb492cd8dffe349d2e9f08c6

Contents?: true

Size: 1.97 KB

Versions: 6

Compression:

Stored size: 1.97 KB

Contents

class Clearance::ConfirmationsController < ApplicationController
  unloadable

  before_filter :redirect_signed_in_confirmed_user,  :only => [:new, :create]
  before_filter :redirect_signed_out_confirmed_user, :only => [:new, :create]
  before_filter :forbid_missing_token,               :only => [:new, :create]
  before_filter :forbid_non_existent_user,           :only => [:new, :create]

  filter_parameter_logging :token

  def new
    create
  end

  def create
    @user = ::User.find_by_id_and_confirmation_token(
                   params[:user_id], params[:token])
    @user.confirm_email!

    sign_in(@user)
    flash_success_after_create
    redirect_to(url_after_create)
  end

  private

  def redirect_signed_in_confirmed_user
    user = ::User.find_by_id(params[:user_id])
    if user && user.email_confirmed? && current_user == user
      flash_success_after_create
      redirect_to(url_after_create)
    end
  end

  def redirect_signed_out_confirmed_user
    user = ::User.find_by_id(params[:user_id])
    if user && user.email_confirmed? && signed_out?
      flash_already_confirmed
      redirect_to(url_already_confirmed)
    end
  end

  def forbid_missing_token
    if params[:token].blank?
      raise ActionController::Forbidden, "missing token"
    end
  end

  def forbid_non_existent_user
    unless ::User.find_by_id_and_confirmation_token(
                  params[:user_id], params[:token])
      raise ActionController::Forbidden, "non-existent user"
    end
  end

  def flash_success_after_create
    flash[:success] = translate(:confirmed_email,
      :scope   => [:clearance, :controllers, :confirmations],
      :default => "Confirmed email and signed in.")
  end

  def flash_already_confirmed
    flash[:success] = translate(:already_confirmed_email,
      :scope   => [:clearance, :controllers, :confirmations],
      :default => "Already confirmed email. Please sign in.")
  end

  def url_after_create
    root_url
  end

  def url_already_confirmed
    sign_in_url
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
thoughtbot-clearance-0.8.0 app/controllers/clearance/confirmations_controller.rb
thoughtbot-clearance-0.8.1 app/controllers/clearance/confirmations_controller.rb
thoughtbot-clearance-0.8.2 app/controllers/clearance/confirmations_controller.rb
clearance-0.8.4 app/controllers/clearance/confirmations_controller.rb
clearance-0.8.3 app/controllers/clearance/confirmations_controller.rb
clearance-0.8.2 app/controllers/clearance/confirmations_controller.rb