Restrict numbers of automatic user group assignments in cronjob
authorMatthias Schmidt <gravatronics@live.com>
Sat, 6 Apr 2019 10:07:19 +0000 (12:07 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 5 Oct 2019 11:30:46 +0000 (13:30 +0200)
Close #2883

(cherry picked from commit e1a4b3bd0f0d474d503940f477f4f768373bbc11)

wcfsetup/install/files/lib/system/cronjob/UserGroupAssignmentCronjob.class.php
wcfsetup/install/files/lib/system/user/group/assignment/UserGroupAssignmentHandler.class.php

index 374d2c12e0b474c30a1cff5dcc8e7c447ef9c1ef..07dddaf37efeffcf1ec77631629de8186e77948c 100644 (file)
@@ -14,6 +14,8 @@ use wcf\system\user\group\assignment\UserGroupAssignmentHandler;
  * @package    WoltLabSuite\Core\System\Cronjob
  */
 class UserGroupAssignmentCronjob extends AbstractCronjob {
+       const MAXIMUM_ASSIGNMENTS = 1000;
+       
        /**
         * @inheritDoc
         */
@@ -22,12 +24,20 @@ class UserGroupAssignmentCronjob extends AbstractCronjob {
                
                $assignments = UserGroupAssignmentCacheBuilder::getInstance()->getData();
                $usersToGroup = [];
+               
+               $assignmentCount = 0;
                foreach ($assignments as $assignment) {
                        if (!isset($usersToGroup[$assignment->groupID])) {
                                $usersToGroup[$assignment->groupID] = [];
                        }
                        
-                       $usersToGroup[$assignment->groupID] = array_merge($usersToGroup[$assignment->groupID], UserGroupAssignmentHandler::getInstance()->getUsers($assignment));
+                       $newUsers = UserGroupAssignmentHandler::getInstance()->getUsers($assignment, self::MAXIMUM_ASSIGNMENTS);
+                       $usersToGroup[$assignment->groupID] = array_merge($usersToGroup[$assignment->groupID], $newUsers);
+                       
+                       $assignmentCount += count($newUsers);
+                       if ($assignmentCount > self::MAXIMUM_ASSIGNMENTS) {
+                               break;
+                       }
                }
                
                foreach ($usersToGroup as $groupID => $users) {
index f18e4a97e009b76d1c1713b9254d5e18236a8f2f..6fd7ff93ff2a7eea18e16fa856e858917ef456e8 100644 (file)
@@ -91,13 +91,17 @@ class UserGroupAssignmentHandler extends SingletonFactory {
         * assignment.
         * 
         * @param       UserGroupAssignment     $assignment
+        * @param       integer                 $maxUsers
         * @return      User[]
         */
-       public function getUsers(UserGroupAssignment $assignment) {
+       public function getUsers(UserGroupAssignment $assignment, $maxUsers = null) {
                $userList = new UserList();
                $userList->getConditionBuilder()->add('user_table.userID NOT IN (SELECT userID FROM wcf'.WCF_N.'_user_to_group WHERE groupID = ?)', [
                        $assignment->groupID
                ]);
+               if ($maxUsers !== null) {
+                       $userList->sqlLimit = $maxUsers;
+               }
                
                $conditions = $assignment->getConditions();
                foreach ($conditions as $condition) {