From b896f69d9ab662f82afa2270d16cef10845d38db Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 27 Feb 2016 15:28:53 +0100 Subject: [PATCH] Add abstract event listeners for user merge/rename --- CHANGELOG.md | 2 + ...AbstractUserActionRenameListener.class.php | 54 +++++++++++++++++++ .../AbstractUserMergeListener.class.php | 51 ++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/event/listener/AbstractUserActionRenameListener.class.php create mode 100644 wcfsetup/install/files/lib/system/event/listener/AbstractUserMergeListener.class.php diff --git a/CHANGELOG.md b/CHANGELOG.md index de8656e303..e04d30361f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ * `parentObjectID` column added to `modification_log` and `wcf\system\log\modification\AbstractModificationLogHandler` introduced as a replacement for `wcf\system\log\modification\ModificationLogHandler`. * Add sort support for `useroptions` option type. * Make user options shown in sidebar sortable. +* `wcf\system\event\listener\AbstractUserActionRenameListener` added. +* `wcf\system\event\listener\AbstractUserMergeListener` added. #### New Traits diff --git a/wcfsetup/install/files/lib/system/event/listener/AbstractUserActionRenameListener.class.php b/wcfsetup/install/files/lib/system/event/listener/AbstractUserActionRenameListener.class.php new file mode 100644 index 0000000000..34ac36093d --- /dev/null +++ b/wcfsetup/install/files/lib/system/event/listener/AbstractUserActionRenameListener.class.php @@ -0,0 +1,54 @@ + + * @package com.woltlab.wcf + * @subpackage system.event.listener + * @category Community Framework + */ +abstract class AbstractUserActionRenameListener implements IParameterizedEventListener { + /** + * data of the updated database tables + * can either contain the database table as value if `userID` and `username` + * are the names of the database columns or an array with values `name` + * (database table name), `userID` and `username` (names of the database + * table columns containing the id and name of the user) + * `{WCF_N}` will be automatically replaced with the number of the WCF installation + * (only with PHP 5.6 string concatenation is possible in property declarations) + * @var array + */ + protected $databaseTables = []; + + /** + * @inheritDoc + */ + public function execute($eventObj, $className, $eventName, array &$parameters) { + $userID = $eventObj->getObjects()[0]->userID; + $username = $eventObj->getParameters()['data']['username']; + + WCF::getDB()->beginTransaction(); + + foreach ($this->databaseTables as $databaseTable) { + if (!is_array($databaseTable)) { + $databaseTable = ['name' => $databaseTable]; + } + if (!isset($databaseTable['userID'])) $databaseTable['userID'] = 'userID'; + if (!isset($databaseTable['username'])) $databaseTable['username'] = 'username'; + + $sql = "UPDATE ".str_replace('{WCF_N}', WCF_N, $databaseTable['name'])." + SET ".$databaseTable['username']." = ? + WHERE ".$databaseTable['userID']." = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([$username, $userID]); + } + + WCF::getDB()->commitTransaction(); + } +} diff --git a/wcfsetup/install/files/lib/system/event/listener/AbstractUserMergeListener.class.php b/wcfsetup/install/files/lib/system/event/listener/AbstractUserMergeListener.class.php new file mode 100644 index 0000000000..acefe16e6d --- /dev/null +++ b/wcfsetup/install/files/lib/system/event/listener/AbstractUserMergeListener.class.php @@ -0,0 +1,51 @@ + + * @package com.woltlab.wcf + * @subpackage system.event.listener + * @category Community Framework + */ +abstract class AbstractUserMergeListener implements IParameterizedEventListener { + /** + * data of the updated database tables + * can either contain the database table as value if `userID` is the name + * of the database column and no ignore is needed or an array with values + * `name` (database table name), `userID`(name of the database table column + * containing the id of the user) and `ignore` (optional) if an UPDATE IGNORE + * query should be used. + * `{WCF_N}` will be automatically replaced with the number of the WCF installation + * (only with PHP 5.6 string concatenation is possible in property declarations) + * @var array + */ + protected $databaseTables = []; + + /** + * @inheritDoc + */ + public function execute($eventObj, $className, $eventName, array &$parameters) { + foreach ($this->databaseTables as $databaseTable) { + if (!is_array($databaseTable)) { + $databaseTable = ['name' => $databaseTable]; + } + if (!isset($databaseTable['userID'])) $databaseTable['userID'] = 'userID'; + + $conditionBuilder = new PreparedStatementConditionBuilder(); + $conditionBuilder->add($databaseTable['userID']." IN (?)", [$eventObj->mergedUserIDs]); + + $sql = "UPDATE".(!empty($databaseTable['ignore']) ? " IGNORE" : "")." ".str_replace('{WCF_N}', WCF_N, $databaseTable['name'])." + SET ".$databaseTable['userID']." = ? + ".$conditionBuilder; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array_merge([$eventObj->destinationUserID], $conditionBuilder->getParameters())); + } + } +} -- 2.20.1