verifies the letter's code

This commit is contained in:
rgarcia
2015-10-19 22:58:44 +02:00
parent 0d01f431fb
commit 5fe1bb3eee
7 changed files with 37 additions and 21 deletions

View File

@@ -1,9 +1,12 @@
class Verification::LetterController < ApplicationController
before_action :authenticate_user!, except: :edit
before_action :verify_resident!
before_action :verify_phone!
before_action :verify_verified!
before_action :verify_lock
before_action :authenticate_user!, except: [:edit, :update]
before_action :check_credentials, only: :update
before_action :verify_resident!, except: :edit
before_action :verify_phone!, except: :edit
before_action :verify_verified!, except: :edit
before_action :verify_lock, except: :edit
skip_authorization_check
def new
@@ -24,8 +27,8 @@ class Verification::LetterController < ApplicationController
end
def update
@letter = Verification::Letter.new(letter_params.merge(user: current_user))
if @letter.verified?
@letter = Verification::Letter.new(letter_params.merge(user: current_user, verify: true))
if @letter.valid?
current_user.update(verified_at: Time.now)
redirect_to account_path, notice: t('verification.letter.update.flash.success')
else
@@ -37,7 +40,7 @@ class Verification::LetterController < ApplicationController
private
def letter_params
params.require(:letter).permit(:verification_code)
params.require(:verification_letter).permit(:verification_code, :email, :password)
end
def verify_phone!
@@ -46,4 +49,13 @@ class Verification::LetterController < ApplicationController
end
end
def check_credentials
user = User.where(email: letter_params[:email]).first
if user && user.valid_password?(letter_params[:password])
sign_in(user)
else
redirect_to edit_letter_path, alert: t('devise.failure.invalid', authentication_keys: 'email')
end
end
end

View File

@@ -1,9 +1,12 @@
class Verification::Letter
include ActiveModel::Model
attr_accessor :user, :verification_code, :email, :password
attr_accessor :user, :verification_code, :email, :password, :verify
validates :user, presence: true
validates :user, presence: true, on: :create
validate :letter_sent, if: :verify?
validate :correct_code, if: :verify?
def save
valid? &&
@@ -14,22 +17,20 @@ class Verification::Letter
user.update(letter_requested_at: Time.now, letter_verification_code: generate_verification_code)
end
def verified?
validate_letter_sent
validate_correct_code
errors.blank?
end
def validate_letter_sent
def letter_sent
errors.add(:verification_code, I18n.t('verification.letter.errors.letter_not_sent')) unless
user.letter_sent_at.present?
end
def validate_correct_code
def correct_code
errors.add(:verification_code, I18n.t('verification.letter.errors.incorect_code')) unless
user.letter_verification_code == verification_code
end
def verify?
verify.present?
end
def increase_letter_verification_tries
user.update(letter_verification_tries: user.letter_verification_tries += 1)
end

View File

@@ -10,7 +10,8 @@
</div>
<div class="medium-6 small-centered column">
<%= form_for @letter, url: edit_letter_path do |f| %>
<%= form_for @letter, url: letter_path, method: :patch do |f| %>
<%= render "/shared/errors", resource: @letter %>
<%= f.text_field :email, label: t("pages.verify.email") %>
<%= f.password_field :password, label: t("pages.verify.password") %>
<%= f.text_field :verification_code, label: t("pages.verify.code") %>

View File

@@ -71,6 +71,7 @@ en:
debate: debate
proposal: proposal
verification::sms: phone
verification::letter: verification
application:
alert:
only_beta_testers: "Sorry only Beta Testers are allowed access at the moment"

View File

@@ -71,6 +71,7 @@ es:
debate: el debate
proposal: la propuesta
verification::sms: el teléfono
verification::letter: la verificación
application:
alert:
only_beta_testers: "Lo sentimos sólo los usuarios de pruebas tienen acceso de momento"

View File

@@ -88,7 +88,7 @@ en:
see_all: "See proposals"
update:
flash:
success: "Correct code. Your account is verified"
success: "Your account has been verified"
alert:
unconfirmed_code: "You have not yet enter the confirmation code"
errors:

View File

@@ -88,7 +88,7 @@ es:
see_all: "Ver propuestas"
update:
flash:
success: "Código correcto. Tu cuenta ya está verificada"
success: "Tu cuenta ya está verificada"
alert:
unconfirmed_code: "Todavía no has introducido el código de confirmación"
errors: