From a785ac2ecf21f259ced5533cfd0841d492a123a2 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 29 Jan 2018 16:14:10 +0100 Subject: [PATCH] Helper methods for permission retrieval inside workers Their internals are rather hacky, but come with a only a minor impact on the worker performance. See #2524 --- .../AbstractRebuildDataWorker.class.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/wcfsetup/install/files/lib/system/worker/AbstractRebuildDataWorker.class.php b/wcfsetup/install/files/lib/system/worker/AbstractRebuildDataWorker.class.php index 45fcc746b8..d3b9d23ee4 100644 --- a/wcfsetup/install/files/lib/system/worker/AbstractRebuildDataWorker.class.php +++ b/wcfsetup/install/files/lib/system/worker/AbstractRebuildDataWorker.class.php @@ -1,6 +1,9 @@ objectList->sqlOffset = $this->limit * $this->loopCount; } + /** + * Returns the value of the permissions for the provided user ids. The special index `0` is + * automatically added and represents a guest user. + * + * @param integer[] $userIDs + * @param string[] $permissions + * @return mixed[] permission value per user id + */ + protected function getBulkUserPermissions(array $userIDs, array $permissions) { + $conditions = new PreparedStatementConditionBuilder(); + $conditions->add("userID IN (?)", [$userIDs]); + + $sql = "SELECT userID, groupID + FROM wcf".WCF_N."_user_to_group + ".$conditions; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute($conditions->getParameters()); + + $groupData = []; + while ($row = $statement->fetchArray()) { + $userID = $row['userID']; + if (!isset($groupData[$userID])) $groupData[$userID] = []; + + $groupData[$userID][] = $row['groupID']; + } + + $userPermissions = []; + foreach ($groupData as $userID => $groupIDs) { + $data = UserGroupPermissionCacheBuilder::getInstance()->getData($groupIDs); + + $userPermissions[$userID] = []; + foreach ($permissions as $permission) { + $userPermissions[$userID][$permission] = (isset($data[$permission])) ? $data[$permission] : false; + } + } + + // add guest user + $data = UserGroupPermissionCacheBuilder::getInstance()->getData(UserGroup::getGroupIDsByType([UserGroup::GUESTS, UserGroup::EVERYONE])); + $userPermissions[0] = []; + foreach ($permissions as $permission) { + $userPermissions[0][$permission] = (isset($data[$permission])) ? $data[$permission] : false; + } + + return $userPermissions; + } + + /** + * Returns the permission value for the provided user id, will be treated as guest + * if the user id cannot be found or is invalid. This method is designed to be used + * with the return value of `getBulkUserPermissions()`. + * + * @param mixed[] $userPermissions + * @param integer $userID + * @param string $permission + * @return mixed + */ + protected function getBulkUserPermissionValue(array &$userPermissions, $userID, $permission) { + $userID = intval($userID); + + // resolve non-existing users against the guest permission + if ($userID && !isset($userPermissions[$userID])) { + return $this->getBulkUserPermissionValue($userPermissions, 0, $permission); + } + + return $userPermissions[$userID][$permission]; + } + /** * @inheritDoc */ -- 2.20.1