From f77136ba37be52f54bc51acc4a5132be4a9ae842 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 11 Nov 2020 15:14:02 +0100 Subject: [PATCH] Add helper methods to MultifactorManageForm These will be required for a future commit, but they also improve readability. --- .../lib/form/MultifactorManageForm.class.php | 57 ++++++++++++------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/wcfsetup/install/files/lib/form/MultifactorManageForm.class.php b/wcfsetup/install/files/lib/form/MultifactorManageForm.class.php index 4880c043c2..2967cfab09 100644 --- a/wcfsetup/install/files/lib/form/MultifactorManageForm.class.php +++ b/wcfsetup/install/files/lib/form/MultifactorManageForm.class.php @@ -100,28 +100,10 @@ class MultifactorManageForm extends AbstractFormBuilderForm { /** @var int|null $setupId */ $setupId = null; if ($this->setupId) { - $sql = "SELECT setupId - FROM wcf".WCF_N."_user_multifactor - WHERE setupId = ? - FOR UPDATE"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute([ - $this->setupId, - ]); - - $setupId = \intval($statement->fetchSingleColumn()); + $setupId = $this->lockSetup($this->setupId); } else { - $sql = "INSERT INTO wcf".WCF_N."_user_multifactor - (userID, objectTypeID) - VALUES (?, ?)"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute([ - WCF::getUser()->userID, - $this->method->objectTypeID, - ]); - - $setupId = \intval(WCF::getDB()->getInsertID("wcf".WCF_N."_user_multifactor", 'setupID')); + $setupId = $this->allocateSetUpId($this->method->objectTypeID); } if (!$setupId) { @@ -136,6 +118,41 @@ class MultifactorManageForm extends AbstractFormBuilderForm { $this->saved(); } + /** + * Locks the set up, preventing any concurrent changes. + */ + protected function lockSetup(int $setupId): int { + $sql = "SELECT setupId + FROM wcf".WCF_N."_user_multifactor + WHERE setupId = ? + FOR UPDATE"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([ + $setupId, + ]); + + $dbSetupId = \intval($statement->fetchSingleColumn()); + assert($setupId === $dbSetupId); + + return $dbSetupId; + } + + /** + * Allocates a fresh setup ID for the given objectTypeID. + */ + protected function allocateSetUpId(int $objectTypeID): int { + $sql = "INSERT INTO wcf".WCF_N."_user_multifactor + (userID, objectTypeID) + VALUES (?, ?)"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([ + WCF::getUser()->userID, + $objectTypeID, + ]); + + return \intval(WCF::getDB()->getInsertID("wcf".WCF_N."_user_multifactor", 'setupID')); + } + /** * @inheritDoc */ -- 2.20.1