From 0bf1409b79812f79e93677ddc929c7547bc128ce Mon Sep 17 00:00:00 2001 From: Duncan Date: Sun, 12 Jan 2025 15:49:58 +0200 Subject: [PATCH 1/4] return false if comp has started --- app/models/competition.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/competition.rb b/app/models/competition.rb index 67a4989a04..14c5b4551d 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -1170,6 +1170,7 @@ def has_event_change_deadline_date? # can registration edits be done right now # must be allowed in general, and if the deadline field exists, is it a date and in the future def registration_edits_allowed? + return false if started? self.allow_registration_edits && (!has_event_change_deadline_date? || !event_change_deadline_date.present? || event_change_deadline_date > DateTime.now) end From fa56b274d6cd7a11682477eda5e03cdfdee5a0b2 Mon Sep 17 00:00:00 2001 From: Duncan <52967253+dunkOnIT@users.noreply.github.com> Date: Mon, 13 Jan 2025 12:23:09 +0200 Subject: [PATCH 2/4] Update app/models/competition.rb Co-authored-by: Gregor Billing --- app/models/competition.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/competition.rb b/app/models/competition.rb index 14c5b4551d..0f67ec9145 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -1170,8 +1170,7 @@ def has_event_change_deadline_date? # can registration edits be done right now # must be allowed in general, and if the deadline field exists, is it a date and in the future def registration_edits_allowed? - return false if started? - self.allow_registration_edits && + !started? && self.allow_registration_edits && (!has_event_change_deadline_date? || !event_change_deadline_date.present? || event_change_deadline_date > DateTime.now) end From 2696c08a3fcfe1992acee363a0ad1373fffa0f9b Mon Sep 17 00:00:00 2001 From: Duncan Date: Sat, 18 Jan 2025 08:02:22 +0200 Subject: [PATCH 3/4] changed function name and added tests --- app/models/competition.rb | 4 ++-- app/models/user.rb | 2 +- lib/registrations/registration_checker.rb | 2 +- .../registration_checker_spec.rb | 19 +++++++++++++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/models/competition.rb b/app/models/competition.rb index 0f67ec9145..b7d576ecbf 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -1169,7 +1169,7 @@ def has_event_change_deadline_date? # can registration edits be done right now # must be allowed in general, and if the deadline field exists, is it a date and in the future - def registration_edits_allowed? + def registration_edits_permitted? !started? && self.allow_registration_edits && (!has_event_change_deadline_date? || !event_change_deadline_date.present? || event_change_deadline_date > DateTime.now) end @@ -1564,7 +1564,7 @@ def ineligible_events(user) end def started? - start_date.present? && start_date < Date.today + start_date.present? && start_date <= Date.today end def organizers_or_delegates diff --git a/app/models/user.rb b/app/models/user.rb index a6ed85808e..cb598dea1e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -873,7 +873,7 @@ def can_view_hidden_competitions? def can_edit_registration?(registration) # A registration can be edited by a user if it hasn't been accepted yet, and if edits are allowed. - editable_by_user = (!registration.accepted? || registration.competition.registration_edits_allowed?) + editable_by_user = (!registration.accepted? || registration.competition.registration_edits_permitted?) can_manage_competition?(registration.competition) || (registration.user_id == self.id && editable_by_user) end diff --git a/lib/registrations/registration_checker.rb b/lib/registrations/registration_checker.rb index eb87bd08d7..0c3cacd962 100644 --- a/lib/registrations/registration_checker.rb +++ b/lib/registrations/registration_checker.rb @@ -78,7 +78,7 @@ def user_can_modify_registration!(competition, current_user, target_user, regist 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) || user_uncancelling_registration?(registration, new_status) + competition.registration_edits_permitted? || 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 diff --git a/spec/lib/registrations/registration_checker_spec.rb b/spec/lib/registrations/registration_checker_spec.rb index b86343aa73..3ef7f412c2 100644 --- a/spec/lib/registrations/registration_checker_spec.rb +++ b/spec/lib/registrations/registration_checker_spec.rb @@ -797,6 +797,25 @@ end end + it 'user cant change events after comp has started', :tag do + comp_started = FactoryBot.create(:competition, :ongoing, allow_registration_edits: true) + registration = FactoryBot.create(:registration, competition: comp_started) + + update_request = FactoryBot.build( + :update_request, + competition_id: registration.competition.id, + user_id: registration.user_id, + ) + + expect { + Registrations::RegistrationChecker.update_registration_allowed!(update_request, Competition.find(update_request['competition_id']), User.find(update_request['submitted_by'])) + }.to raise_error(WcaExceptions::RegistrationError) do |error| + expect(error.status).to eq(:forbidden) + expect(error.error).to eq(Registrations::ErrorCodes::USER_EDITS_NOT_ALLOWED) + end + + end + it 'user cant change events after event change deadline' do edit_deadline_passed = FactoryBot.create(:competition, :event_edit_passed) registration = FactoryBot.create(:registration, competition: edit_deadline_passed) From 58b3e51ff153f45bc07f082b410acfb28b0d2367 Mon Sep 17 00:00:00 2001 From: Duncan Date: Sat, 18 Jan 2025 08:05:07 +0200 Subject: [PATCH 4/4] rubocop --- spec/lib/registrations/registration_checker_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/lib/registrations/registration_checker_spec.rb b/spec/lib/registrations/registration_checker_spec.rb index 3ef7f412c2..f36a2bf0ba 100644 --- a/spec/lib/registrations/registration_checker_spec.rb +++ b/spec/lib/registrations/registration_checker_spec.rb @@ -813,7 +813,6 @@ expect(error.status).to eq(:forbidden) expect(error.error).to eq(Registrations::ErrorCodes::USER_EDITS_NOT_ALLOWED) end - end it 'user cant change events after event change deadline' do