Skip to content

Commit

Permalink
allow uncancelling if edits are disabled (#10373)
Browse files Browse the repository at this point in the history
  • Loading branch information
dunkOnIT authored Dec 6, 2024
1 parent 940a1f7 commit efe79b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/registrations/registration_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions spec/lib/registrations/registration_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
)

Expand Down

0 comments on commit efe79b3

Please sign in to comment.