From efe79b350cd42a986a3491d83142d4564b7140e8 Mon Sep 17 00:00:00 2001 From: Duncan <52967253+dunkOnIT@users.noreply.github.com> Date: Fri, 6 Dec 2024 16:53:18 +0200 Subject: [PATCH] allow uncancelling if edits are disabled (#10373) --- lib/registrations/registration_checker.rb | 10 +++++++--- spec/lib/registrations/registration_checker_spec.rb | 7 ++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/registrations/registration_checker.rb b/lib/registrations/registration_checker.rb index 3630f18ac2..0646ab8308 100644 --- a/lib/registrations/registration_checker.rb +++ b/lib/registrations/registration_checker.rb @@ -26,7 +26,7 @@ def self.update_registration_allowed!(update_request, competition, current_user) new_status = update_request.dig('competing', 'status') events = update_request.dig('competing', 'event_ids') - user_can_modify_registration!(competition, current_user, target_user, registration) + user_can_modify_registration!(competition, current_user, target_user, registration, new_status) validate_guests!(guests.to_i, competition) unless guests.nil? validate_comment!(comment, competition, registration) validate_organizer_fields!(update_request, current_user, competition) @@ -71,17 +71,21 @@ def user_can_create_registration!(competition, current_user, target_user) raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if existing_registration_in_series?(competition, target_user) end - def user_can_modify_registration!(competition, current_user, target_user, registration) + def user_can_modify_registration!(competition, current_user, target_user, registration, new_status) raise WcaExceptions::RegistrationError.new(:unauthorized, Registrations::ErrorCodes::USER_INSUFFICIENT_PERMISSIONS) unless can_administer_or_current_user?(competition, current_user, target_user) raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::USER_EDITS_NOT_ALLOWED) unless - competition.registration_edits_allowed? || current_user.can_manage_competition?(competition) + competition.registration_edits_allowed? || current_user.can_manage_competition?(competition) || user_uncancelling_registration?(registration, new_status) raise WcaExceptions::RegistrationError.new(:unauthorized, Registrations::ErrorCodes::REGISTRATION_IS_REJECTED) if user_is_rejected?(current_user, target_user, registration) && !organizer_modifying_own_registration?(competition, current_user, target_user) raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if existing_registration_in_series?(competition, target_user) && !current_user.can_manage_competition?(competition) end + def user_uncancelling_registration?(registration, new_status) + registration.competing_status_cancelled? && new_status == Registrations::Helper::STATUS_PENDING + end + def user_is_rejected?(current_user, target_user, registration) current_user.id == target_user.id && registration.rejected? end diff --git a/spec/lib/registrations/registration_checker_spec.rb b/spec/lib/registrations/registration_checker_spec.rb index 306fd8e8eb..1f5b07df42 100644 --- a/spec/lib/registrations/registration_checker_spec.rb +++ b/spec/lib/registrations/registration_checker_spec.rb @@ -1395,12 +1395,13 @@ end it 'user can change state from cancelled to pending' do - deleted_reg = FactoryBot.create(:registration, :cancelled, competition: default_competition) + no_edits_comp = FactoryBot.create(:competition, :registration_open) + cancelled_reg = FactoryBot.create(:registration, :cancelled, competition: no_edits_comp) update_request = FactoryBot.build( :update_request, - user_id: deleted_reg.user_id, - competition_id: deleted_reg.competition.id, + user_id: cancelled_reg.user_id, + competition_id: cancelled_reg.competition.id, competing: { 'status' => 'pending' }, )