diff --git a/app/controllers/verification/letter_controller.rb b/app/controllers/verification/letter_controller.rb index 5bb58d759..5f0513556 100644 --- a/app/controllers/verification/letter_controller.rb +++ b/app/controllers/verification/letter_controller.rb @@ -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 \ No newline at end of file diff --git a/app/models/verification/letter.rb b/app/models/verification/letter.rb index 96321a419..c1ace6063 100644 --- a/app/models/verification/letter.rb +++ b/app/models/verification/letter.rb @@ -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 diff --git a/app/views/verification/letter/edit.html.erb b/app/views/verification/letter/edit.html.erb index 73c9b9a48..3f2084d26 100644 --- a/app/views/verification/letter/edit.html.erb +++ b/app/views/verification/letter/edit.html.erb @@ -10,7 +10,8 @@
- <%= 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") %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 8e56cd69e..7365d2703 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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" diff --git a/config/locales/es.yml b/config/locales/es.yml index 18763a895..6c94c2ef5 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -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" diff --git a/config/locales/verification.en.yml b/config/locales/verification.en.yml index 50e0d425b..68aba1603 100644 --- a/config/locales/verification.en.yml +++ b/config/locales/verification.en.yml @@ -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: diff --git a/config/locales/verification.es.yml b/config/locales/verification.es.yml index fbd7c9499..859eadf3a 100644 --- a/config/locales/verification.es.yml +++ b/config/locales/verification.es.yml @@ -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: