From c20358e683ce05d8ed8b21f31214e31d2d31c131 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 24 Oct 2012 17:44:26 +0200 Subject: [PATCH] Add UserGroup::isAdminGroup() --- .../lib/data/user/group/UserGroup.class.php | 14 ++++ .../data/user/group/UserGroupEditor.class.php | 66 +++++++------------ ...pOptionPackageInstallationPlugin.class.php | 37 ++++------- 3 files changed, 49 insertions(+), 68 deletions(-) diff --git a/wcfsetup/install/files/lib/data/user/group/UserGroup.class.php b/wcfsetup/install/files/lib/data/user/group/UserGroup.class.php index 5e54d63dcf..6230a68e5f 100644 --- a/wcfsetup/install/files/lib/data/user/group/UserGroup.class.php +++ b/wcfsetup/install/files/lib/data/user/group/UserGroup.class.php @@ -190,6 +190,20 @@ class UserGroup extends DatabaseObject { return $groups; } + /** + * Returns true when the current group is an admin-group. + * Every group that may access EVERY group is an admin-group. + * + * @return boolean + */ + public function isAdminGroup() { + $groupIDs = array_keys(self::getGroupsByType()); + $accessibleGroupIDs = explode(',', $this->getGroupOption('admin.user.accessibleGroups')); + + // no differences -> all groups are included + return count(array_diff($groupIDs, $accessibleGroupIDs)) == 0 ? true : false; + } + /** * Loads the group cache. */ diff --git a/wcfsetup/install/files/lib/data/user/group/UserGroupEditor.class.php b/wcfsetup/install/files/lib/data/user/group/UserGroupEditor.class.php index 6ff8611e73..2470f0ce9e 100644 --- a/wcfsetup/install/files/lib/data/user/group/UserGroupEditor.class.php +++ b/wcfsetup/install/files/lib/data/user/group/UserGroupEditor.class.php @@ -117,9 +117,9 @@ class UserGroupEditor extends DatabaseObjectEditor implements IEditableCachedObj protected static function updateAccessibleGroups($groupID, $delete = false) { if ($delete) { $sql = "UPDATE wcf".WCF_N."_user_group_option_value - SET optionValue = ? - WHERE groupID = ? - AND optionID = ?"; + SET optionValue = ? + WHERE groupID = ? + AND optionID = ?"; $updateStatement = WCF::getDB()->prepareStatement($sql); $sql = "SELECT groupID, optionValue, groupOption.optionID @@ -142,56 +142,36 @@ class UserGroupEditor extends DatabaseObjectEditor implements IEditableCachedObj return; } - // get existing groups - $groupIDs = array(); - $sql = "SELECT groupID - FROM wcf".WCF_N."_user_group - ORDER BY groupID ASC"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute(); - while ($row = $statement->fetchArray()) { - if ($row['groupID'] == $groupID) continue; - $groupIDs[] = $row['groupID']; - } - - $conditions = new PreparedStatementConditionBuilder(); - $conditions->add("groupOption.optionName = ?", array('admin.user.accessibleGroups')); - $conditions->add("groupID IN (?)", array($groupIDs)); - - $sql = "SELECT groupID, optionValue, groupOption.optionID - FROM wcf".WCF_N."_user_group_option groupOption - LEFT JOIN wcf".WCF_N."_user_group_option_value optionValue - ON (groupOption.optionID = optionValue.optionID) - ".$conditions; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute($conditions->getParameters()); - - $updateGroupIDs = array(); - $optionID = 0; - // get groups which got "accessibleGroups"-option with all groupIDs - while ($row = $statement->fetchArray()) { - - // check for differences in options-groups and existing-groups - $optionGroupIDs = explode(',', $row['optionValue']); - $differences = array_diff($optionGroupIDs, $groupIDs); + $userGroupList = new UserGroupList(); + $userGroupList->sqlLimit = 0; + $userGroupList->readObjects(); + foreach ($userGroupList as $userGroup) { + $groupIDs[] = $userGroup->groupID; - // get groups which got the right to change all groups - if (empty($differences) && (count($optionGroupIDs) == count($groupIDs))) { - $updateGroupIDs[] = $row['groupID']; - $optionID = $row['optionID']; + if ($userGroup->isAdminGroup()) { + $updateGroupIDs[] = $userGroup->groupID; } } + $sql = "SELECT optionID + FROM wcf".WCF_N."_user_group_option + WHERE optionName = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array('admin.user.accessibleGroups')); + $row = $statement->fetchArray(); + $optionID = $row['optionID']; + // update optionValue from groups which got all existing groups as value if (count($updateGroupIDs)) { - $groupIDs[] = $groupID; + $conditionBuilder = new PreparedStatementConditionBuilder(); + $conditionBuilder->add('groupID IN (?)', array($updateGroupIDs)); + $conditionBuilder->add('optionID = ?', array($optionID)); $sql = "UPDATE wcf".WCF_N."_user_group_option_value SET optionValue = ? - WHERE groupID IN (".implode(',', $updateGroupIDs).") - AND optionID = ?"; + ".$conditionBuilder; $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute(array(implode(',', $groupIDs), $optionID)); + $statement->execute(array_merge((array) implode(',', $groupIDs), $conditionBuilder->getParameters())); } } diff --git a/wcfsetup/install/files/lib/system/package/plugin/UserGroupOptionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/UserGroupOptionPackageInstallationPlugin.class.php index 132a977180..46021a711b 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/UserGroupOptionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/UserGroupOptionPackageInstallationPlugin.class.php @@ -115,34 +115,21 @@ class UserGroupOptionPackageInstallationPlugin extends AbstractOptionPackageInst $statement->execute(array($row['groupID'], $optionID, $defaultValue)); if ($adminDefaultValue && $defaultValue != $adminDefaultValue) { - $sql = "SELECT groupID - FROM wcf".WCF_N."_user_group_option_value - WHERE optionID = ( - SELECT optionID - FROM wcf".WCF_N."_user_group_option - WHERE optionName = ? - ) - AND optionValue = '1'"; - $statement2 = WCF::getDB()->prepareStatement($sql); - $statement2->execute(array('admin.general.canUseAcp')); + $userGroupList = new UserGroupList(); + $userGroupList->sqlLimit = 0; + $userGroupList->readObjects(); - $acpGroups = array(); - while ($row = $statement2->fetchArray()) { - $acpGroups[] = $row['groupID']; - } - - $statement2->execute(array('admin.user.canEditGroup')); - while ($row = $statement2->fetchArray()) { - if (!in_array($row['groupID'], $acpGroups)) { - continue; + WCF::getDB()->beginTransaction(); + foreach ($userGroupList as $userGroup) { + if ($userGroup->isAdminGroup()) { + $statement->execute(array( + $userGroup->groupID, + $optionID, + $adminDefaultValue + )); } - - $statement->execute(array( - $row['groupID'], - $optionID, - $adminDefaultValue - )); } + WCF::getDB()->commitTransaction(); } } } -- 2.20.1