diff --git a/app/controllers/start_form_controller.rb b/app/controllers/start_form_controller.rb index e8aa1614..d4df3664 100644 --- a/app/controllers/start_form_controller.rb +++ b/app/controllers/start_form_controller.rb @@ -8,13 +8,9 @@ class StartFormController < ApplicationController around_action :with_browser_locale, only: %i[show update completed] before_action :maybe_redirect_com, only: %i[show completed] - before_action :load_resubmit_submitter, only: :update before_action :load_template before_action :authorize_start!, only: :update - COOKIES_TTL = 12.hours - COOKIES_DEFAULTS = { httponly: true, secure: Rails.env.production? }.freeze - def show if @template.preferences['require_phone_2fa'] || @template.preferences['require_email_2fa'] raise ActionController::RoutingError, I18n.t('not_found') @@ -23,7 +19,7 @@ class StartFormController < ApplicationController if @template.shared_link? @submitter = @template.submissions.new(account_id: @template.account_id) .submitters.new(account_id: @template.account_id, - uuid: first_submitter_uuid(@template)) + uuid: Submitters::StartForm.first_submitter_uuid(@template)) render :email_verification if params[:email_verification] else Rollbar.warning("Not shared template: #{@template.id}") if defined?(Rollbar) @@ -40,27 +36,28 @@ class StartFormController < ApplicationController if @submitter.completed_at? redirect_to start_form_completed_path(@template.slug, submitter_params.compact_blank) else - if filter_undefined_submitters(@template).size > 1 && @submitter.new_record? + if Templates.filter_undefined_submitters(@template.submitters).size > 1 && @submitter.new_record? @error_message = multiple_submitters_error_message return render :show, status: :unprocessable_content end if (is_new_record = @submitter.new_record?) - assign_submission_attributes(@submitter, @template) + Submitters::StartForm.assign_submission_attributes( + @submitter, @template, ip: request.remote_ip, user_agent: request.user_agent + ) Submissions::AssignDefinedSubmitters.call(@submitter.submission) else @submitter.assign_attributes(ip: request.remote_ip, ua: request.user_agent) end - if @template.preferences['shared_link_2fa'] == true || - (!is_new_record && @resubmit_submitter.blank? && !own_submitter?(@submitter)) + if require_link_2fa?(@submitter, is_new_record:) Rollbar.info("2FA requested: #{@submitter.id}") if !is_new_record && defined?(Rollbar) handle_require_2fa(@submitter, is_new_record:) elsif @submitter.errors.blank? && @submitter.save - enqueue_new_submitter_jobs(@submitter) if is_new_record + Submitters::StartForm.enqueue_new_submitter_jobs(@submitter) if is_new_record redirect_to submit_form_path(@submitter.slug) else @@ -90,45 +87,30 @@ class StartFormController < ApplicationController private - def enqueue_new_submitter_jobs(submitter) - WebhookUrls.enqueue_events(submitter.submission, 'submission.created') - - SearchEntries.enqueue_reindex(submitter) - - expire_at = submitter.submission.expire_at - - return unless expire_at - - ProcessSubmissionExpiredJob.perform_at(expire_at, 'submission_id' => submitter.submission_id, - 'expire_at' => expire_at.to_i) - end + def find_or_initialize_submitter(template, submitter_params) + submitter = Submitters::StartForm.find_or_initialize_submitter( + template, submitter_params, exclude_completed: params[:resubmit].present?, + current_user:, ability: current_user && current_ability + ) - def load_resubmit_submitter - @resubmit_submitter = - if params[:resubmit].present? && !params[:resubmit].in?([true, 'true']) - submitter = Submitter.find_by(slug: params[:resubmit]) + template.preferences.fetch('link_form_fields', ['email']).each do |key| + submitter.errors.add(key.to_sym, :blank) if submitter_params[key].blank? + end - submitter if submitter && can_resubmit?(submitter) - end + submitter end - def own_submitter?(submitter) - current_user&.email == submitter.email && current_ability.can?(:read, submitter) - end + def require_link_2fa?(submitter, is_new_record:) + return true if @template.preferences['shared_link_2fa'] == true - def can_resubmit?(submitter) - submitter.completed_at? && submitter.completed_at > 14.days.ago && - %w[api embed mcp].exclude?(submitter.submission.source) && - submitter.account.account_configs.find_or_initialize_by(key: AccountConfig::ALLOW_TO_RESUBMIT).value != false + !is_new_record && (current_user&.email != submitter.email || !current_ability.can?(:read, submitter)) end def authorize_start! is_archived = @template.archived_at? || @template.account.archived_at? - return redirect_to submit_form_path(@resubmit_submitter.slug) if @resubmit_submitter && is_archived return redirect_to start_form_path(@template.slug) if is_archived - return if @resubmit_submitter return if @template.shared_link? || (current_user && current_ability.can?(:read, @template)) Rollbar.warning("Not shared template: #{@template.id}") if defined?(Rollbar) @@ -136,104 +118,14 @@ class StartFormController < ApplicationController redirect_to start_form_path(@template.slug) end - def find_or_initialize_submitter(template, submitter_params) - required_fields = template.preferences.fetch('link_form_fields', ['email']) - - required_params = required_fields.index_with { |key| submitter_params[key] } - - find_params = required_params.except('name') - - submitter = Submitter.new if find_params.compact_blank.blank? - - submitter ||= build_submitters_scope(template).find_or_initialize_by(find_params) - - submitter = Submitter.new(find_params) if submitter.persisted? && submitter.email.blank? - - submitter = Submitter.new(find_params) if submitter.submission&.completed_at? && submitter.viewer? - - if !Docuseal.multitenant? && submitter.persisted? && !submitter.completed_at? && - @resubmit_submitter.blank? && !own_submitter?(submitter) && - template.preferences['shared_link_2fa'] != true && !Accounts.can_send_emails?(template.account) - submitter = Submitter.new(find_params) - end - - submitter.name = required_params['name'] if submitter.new_record? - - unless @resubmit_submitter - required_params.each do |key, value| - submitter.errors.add(key.to_sym, :blank) if value.blank? - end - end - - submitter - end - - def build_submitters_scope(template) - Submitter - .where(submission: template.submissions.non_expired.active) - .where(uuid: template.submitters.pluck('uuid')) - .order(id: :desc) - .where(declined_at: nil) - .where(external_id: nil) - .then { |rel| params[:resubmit].present? || params[:selfsign].present? ? rel.where(completed_at: nil) : rel } - end - - def assign_submission_attributes(submitter, template) - submitter.assign_attributes( - uuid: first_submitter_uuid(template), - ip: request.remote_ip, - ua: request.user_agent, - values: @resubmit_submitter&.preferences&.fetch('default_values', nil) || {}, - preferences: @resubmit_submitter&.preferences.presence || { 'send_email' => true }, - metadata: @resubmit_submitter&.metadata.presence || {} - ) - - submitter.assign_attributes(@resubmit_submitter.slice(:name, :email, :phone)) if @resubmit_submitter - - if submitter.values.present? - @resubmit_submitter.attachments.each do |attachment| - submitter.attachments << attachment.dup if submitter.values.value?(attachment.uuid) - end - end - - submitter.submission ||= Submission.new(template:, - account_id: template.account_id, - template_submitters: template.submitters, - expire_at: Templates.build_default_expire_at(template), - submitters: [submitter], - source: :link) - - Submissions::CreateFromSubmitters.maybe_set_dynamic_documents(submitter.submission) - - submitter.account_id = submitter.submission.account_id - - submitter - end - - def filter_undefined_submitters(template) - Templates.filter_undefined_submitters(template.submitters) - end - - def first_submitter_uuid(template) - (filter_undefined_submitters(template).first || template.submitters.first)['uuid'] - end - def submitter_params - return { 'email' => current_user.email, 'name' => current_user.full_name } if params[:selfsign] - return @resubmit_submitter.slice(:name, :phone, :email) if @resubmit_submitter.present? - params.require(:submitter).permit(:email, :phone, :name).tap do |attrs| attrs[:email] = Submissions.normalize_email(attrs[:email]) end end def load_template - @template = - if @resubmit_submitter - @resubmit_submitter.template - else - Template.find_by!(slug: params[:slug] || params[:start_form_slug]) - end + @template = Template.find_by!(slug: params[:slug] || params[:start_form_slug]) end def multiple_submitters_error_message @@ -247,35 +139,20 @@ class StartFormController < ApplicationController def handle_require_2fa(submitter, is_new_record:) return render :show, status: :unprocessable_content if submitter.errors.present? - is_otp_verified = Submitters.verify_link_otp!(params[:one_time_code], submitter) - - if cookies.encrypted[:email_2fa_slug] == submitter.slug || is_otp_verified - if submitter.save - enqueue_new_submitter_jobs(submitter) if is_new_record - - if is_otp_verified - SubmissionEvents.create_with_tracking_data(submitter, 'email_verified', request) - - cookies.encrypted[:email_2fa_slug] = - { value: submitter.slug, expires: COOKIES_TTL.from_now, **COOKIES_DEFAULTS } - end - - redirect_to submit_form_path(submitter.slug) - else - render :show, status: :unprocessable_content - end + if Submitters::StartForm.verify_2fa_and_save_submitter(submitter, request, is_new_record:) + redirect_to submit_form_path(submitter.slug) else Submitters.send_shared_link_email_verification_code(submitter, request:) render :email_verification end + rescue Submitters::StartForm::NotSaved + render :show, status: :unprocessable_content rescue Submitters::UnableToSendCode, Submitters::InvalidOtp => e - redirect_to start_form_path(submitter.submission.template.slug, - params: submitter_params.merge(email_verification: true)), + redirect_to start_form_path(@template.slug, params: submitter_params.merge(email_verification: true)), alert: e.message rescue RateLimit::LimitApproached - redirect_to start_form_path(submitter.submission.template.slug, - params: submitter_params.merge(email_verification: true)), + redirect_to start_form_path(@template.slug, params: submitter_params.merge(email_verification: true)), alert: I18n.t(:too_many_attempts) end end diff --git a/app/controllers/start_form_email_2fa_send_controller.rb b/app/controllers/start_form_email_2fa_send_controller.rb index 8b619f78..754e8428 100644 --- a/app/controllers/start_form_email_2fa_send_controller.rb +++ b/app/controllers/start_form_email_2fa_send_controller.rb @@ -53,13 +53,7 @@ class StartFormEmail2faSendController < ApplicationController return false if email.blank? - Submitter - .where(submission: template.submissions.non_expired.active) - .where(uuid: template.submitters.pluck('uuid')) - .where(declined_at: nil) - .where(external_id: nil) - .where(completed_at: nil) - .exists?(email:) + Submitters::StartForm.build_submitters_scope(template, exclude_completed: true).exists?(email:) end def submitter_params diff --git a/app/controllers/start_form_resubmit_controller.rb b/app/controllers/start_form_resubmit_controller.rb new file mode 100644 index 00000000..ac11370d --- /dev/null +++ b/app/controllers/start_form_resubmit_controller.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +class StartFormResubmitController < ApplicationController + layout 'form' + + skip_before_action :authenticate_user! + skip_authorization_check + + around_action :with_browser_locale + before_action :load_resubmit_submitter + before_action :load_template + before_action :authorize_start! + + def update + @submitter = Submitters::StartForm.find_or_initialize_submitter( + @template, submitter_params, exclude_completed: true + ) + + if Templates.filter_undefined_submitters(@template.submitters).size > 1 && @submitter.new_record? + @error_message = multiple_submitters_error_message + + return render 'start_form/show', status: :unprocessable_content + end + + if (is_new_record = @submitter.new_record?) + Submitters::StartForm.assign_submission_attributes( + @submitter, @template, + ip: request.remote_ip, user_agent: request.user_agent, resubmit_submitter: @resubmit_submitter + ) + + Submissions::AssignDefinedSubmitters.call(@submitter.submission) + else + @submitter.assign_attributes(ip: request.remote_ip, ua: request.user_agent) + end + + if @template.preferences['shared_link_2fa'] == true + handle_require_2fa(@submitter, is_new_record:) + elsif @submitter.save + Submitters::StartForm.enqueue_new_submitter_jobs(@submitter) if is_new_record + + redirect_to submit_form_path(@submitter.slug) + else + render 'start_form/show', status: :unprocessable_content + end + end + + private + + def load_resubmit_submitter + @resubmit_submitter = Submitter.find_by(slug: params[:resubmit]) + + raise ActiveRecord::RecordNotFound if @resubmit_submitter.blank? || + !Submitters::StartForm.can_resubmit?(@resubmit_submitter) + end + + def load_template + @template = @resubmit_submitter.template + end + + def authorize_start! + redirect_to submit_form_path(@resubmit_submitter.slug) if @template.archived_at? || @template.account.archived_at? + end + + def submitter_params + @resubmit_submitter.slice(:name, :phone, :email) + end + + def multiple_submitters_error_message + if current_user&.account_id == @template.account_id + helpers.t('this_submission_has_multiple_signers_which_prevents_the_use_of_a_sharing_link_html') + else + I18n.t('not_found') + end + end + + def handle_require_2fa(submitter, is_new_record:) + if Submitters::StartForm.verify_2fa_and_save_submitter(submitter, request, is_new_record:) + redirect_to submit_form_path(submitter.slug) + else + Submitters.send_shared_link_email_verification_code(submitter, request:) + + render 'start_form/email_verification' + end + rescue Submitters::StartForm::NotSaved + render 'start_form/show', status: :unprocessable_content + rescue Submitters::UnableToSendCode, Submitters::InvalidOtp => e + redirect_to start_form_path(@template.slug, params: submitter_params.merge(email_verification: true)), + alert: e.message + rescue RateLimit::LimitApproached + redirect_to start_form_path(@template.slug, params: submitter_params.merge(email_verification: true)), + alert: I18n.t(:too_many_attempts) + end +end diff --git a/app/controllers/start_form_self_controller.rb b/app/controllers/start_form_self_controller.rb new file mode 100644 index 00000000..bc371eee --- /dev/null +++ b/app/controllers/start_form_self_controller.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +class StartFormSelfController < ApplicationController + load_resource :template, parent: false + + around_action :with_browser_locale + before_action :authorize_start! + + def update + @submitter = Submitters::StartForm.build_submitters_scope(@template, exclude_completed: true) + .find_or_initialize_by(email: current_user.email) + + if (is_new_record = @submitter.new_record?) + @submitter.name = current_user.full_name + + Submitters::StartForm.assign_submission_attributes( + @submitter, @template, ip: request.remote_ip, user_agent: request.user_agent + ) + + Submissions::AssignDefinedSubmitters.call(@submitter.submission) + else + @submitter.assign_attributes(ip: request.remote_ip, ua: request.user_agent) + end + + @submitter.save! + + Submitters::StartForm.enqueue_new_submitter_jobs(@submitter) if is_new_record + + redirect_to submit_form_path(@submitter.slug) + end + + private + + def authorize_start! + authorize!(:read, @template) + + redirect_to template_path(@template) if @template.archived_at? + end +end diff --git a/app/controllers/submit_form_email_2fas_controller.rb b/app/controllers/submit_form_email_2fas_controller.rb index ec627b5d..dfe518b0 100644 --- a/app/controllers/submit_form_email_2fas_controller.rb +++ b/app/controllers/submit_form_email_2fas_controller.rb @@ -8,9 +8,6 @@ class SubmitFormEmail2fasController < ApplicationController before_action :load_submitter - COOKIES_TTL = 12.hours - COOKIES_DEFAULTS = { httponly: true, secure: Rails.env.production? }.freeze - def create RateLimit.call("verify-2fa-code-#{@submitter.id}", limit: 2, ttl: 45.seconds, enabled: true) @@ -20,7 +17,8 @@ class SubmitFormEmail2fasController < ApplicationController SubmissionEvents.create_with_tracking_data(@submitter, 'email_verified', request, { email: @submitter.email }) cookies.encrypted[:email_2fa_slug] = - { value: @submitter.slug, expires: COOKIES_TTL.from_now, **COOKIES_DEFAULTS } + { value: @submitter.slug, expires: Submitters::StartForm::COOKIES_TTL.from_now, + **Submitters::StartForm::COOKIES_DEFAULTS } redirect_to submit_form_path(@submitter.slug) else diff --git a/app/javascript/template_builder/builder.vue b/app/javascript/template_builder/builder.vue index 0b49de40..a834e373 100644 --- a/app/javascript/template_builder/builder.vue +++ b/app/javascript/template_builder/builder.vue @@ -110,7 +110,7 @@ data-turbo="false" class="inline" method="post" - :action="`/d/${template.slug}`" + :action="`/start_form_self/${template.id}`" @submit="maybeShowErrorTemplateAlert" > -