From dd1645b0f2602cab37151d78272d02257d3ef1a3 Mon Sep 17 00:00:00 2001 From: Mattia Date: Sat, 21 Nov 2026 11:52:14 +0100 Subject: [PATCH] [3.0] Record poll votes without poll edit permission Persist vote totals directly instead of calling Poll::save(), which requires poll editing privileges. Keep guest voter totals in sync and use atomic increments/decrements for individual choice counts. Fixes #9590 Signed-off-by: Mattia Agostini --- Sources/Actions/PollVote.php | 40 ++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/Sources/Actions/PollVote.php b/Sources/Actions/PollVote.php index 9d4e59d664..747454f01d 100644 --- a/Sources/Actions/PollVote.php +++ b/Sources/Actions/PollVote.php @@ -86,14 +86,23 @@ public function execute(): void $changed = true; $poll->choices[$id]->votes--; $poll->choices[$id]->voted_this = false; + + // Voting should not require permission to edit the poll itself. + Db::$db->query( + 'UPDATE {db_prefix}poll_choices + SET votes = votes - 1 + WHERE id_poll = {int:id_poll} + AND id_choice = {int:id_choice}', + [ + 'id_poll' => $poll->id, + 'id_choice' => $id, + ], + ); } } // Just skip it if they had voted for nothing before. if ($changed) { - // Update the poll. - $poll->save(); - // Delete off the log. Db::$db->query( 'DELETE FROM {db_prefix}log_polls @@ -128,6 +137,18 @@ public function execute(): void $id_choice = (int) $id_choice; $poll->choices[$id_choice]->votes++; $inserts[] = [$poll->id, User::$me->id, $id_choice]; + + // Update the tally without requiring poll editing privileges. + Db::$db->query( + 'UPDATE {db_prefix}poll_choices + SET votes = votes + 1 + WHERE id_poll = {int:id_poll} + AND id_choice = {int:id_choice}', + [ + 'id_poll' => $poll->id, + 'id_choice' => $id_choice, + ], + ); } // If it's a guest don't let them vote again. @@ -141,11 +162,18 @@ public function execute(): void $cookie = new Cookie('guest_poll_vote', $_COOKIE['guest_poll_vote'], time() + 2500000); $cookie->set(); - // Increase num_guest_voters by 1 + // Increase num_guest_voters by 1. $poll->num_guest_voters++; - } - $poll->save(); + Db::$db->query( + 'UPDATE {db_prefix}polls + SET num_guest_voters = num_guest_voters + 1 + WHERE id_poll = {int:id_poll}', + [ + 'id_poll' => $poll->id, + ], + ); + } // Add their vote to the tally. Db::$db->insert(