From 7db683c67029684b39d633da49631f54f58ceeef Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 31 Mar 2021 20:49:49 +0200 Subject: [PATCH] Extract validation helper method and adjust things --- .../direct_verifications/authorize_user.rb | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/decidim/direct_verifications/authorize_user.rb b/lib/decidim/direct_verifications/authorize_user.rb index 12ddabd..622c98e 100644 --- a/lib/decidim/direct_verifications/authorize_user.rb +++ b/lib/decidim/direct_verifications/authorize_user.rb @@ -14,13 +14,13 @@ def initialize(email, data, session, organization, instrumenter) def call user = find_user - if user - auth = authorization(user) - auth.metadata = data - - return unless !auth.granted? || auth.expired? + unless user + instrumenter.add_error :authorized, email + else + @authorization = find_or_create_authorization(user) + return unless valid_authorization? - Verification::ConfirmUserAuthorization.call(auth, authorize_form(user), session) do + Verification::ConfirmUserAuthorization.call(authorization, authorize_form(user), session) do on(:ok) do instrumenter.add_processed :authorized, email end @@ -28,24 +28,28 @@ def call add_error :authorized, email end end - else - instrumenter.add_error :authorized, email end end private - attr_reader :email, :data, :session, :organization, :instrumenter + attr_reader :email, :data, :session, :organization, :instrumenter, :authorization + + def valid_authorization? + !authorization.granted? || authorization.expired? + end def find_user User.find_by(email: email, decidim_organization_id: organization.id) end - def authorization(user) - Authorization.find_or_initialize_by( + def find_or_create_authorization(user) + auth = Authorization.find_or_initialize_by( user: user, name: :direct_verifications ) + auth.metadata = data + auth end def authorize_form(user)