diff --git a/config/cleaners.php b/config/cleaners.php index 1688896..5981d02 100644 --- a/config/cleaners.php +++ b/config/cleaners.php @@ -8,6 +8,8 @@ ], /** Whether the super admin should be cleaned by the UserCleaner */ 'clean_super_admin' => false, + /** User controlled 'skip_user_groups' */ + 'skip_user_groups' => ['Administrators'], /** File types that we don't need to sanitize */ 'default_skip_file_types' => [ 'jpg', 'jpeg', 'png', 'gif', 'svg', 'apng', 'bmp', 'ico' diff --git a/src/Clean/UserCleaner.php b/src/Clean/UserCleaner.php index 8baf6db..faae64f 100644 --- a/src/Clean/UserCleaner.php +++ b/src/Clean/UserCleaner.php @@ -4,6 +4,7 @@ use Concrete\Core\Config\Repository\Repository; use Concrete\Core\Entity\User\User; +use Concrete\Core\User\Group\Group; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\ORMException; @@ -30,10 +31,12 @@ public function run(EntityManagerInterface $em, Repository $config) $cleanAdmin = $config->get('fresh::cleaners.clean_super_admin', false); $repository = $em->getRepository(User::class); $allUsers = $repository->findAll(); + $skipUIDs = $this->getSkipUIDs($config); // Clean users foreach ($allUsers as $user) { - if ($user->getUserID() === USER_SUPER_ID && !$cleanAdmin) { + // Skip if clean_super_admin is false or the user exists in the skip groups + if ((!$cleanAdmin && $user->getUserID() === USER_SUPER_ID) || in_array($user->getUserID(), $skipUIDs, false)) { continue; } @@ -170,4 +173,28 @@ private function inUse(EntityRepository $userRepository, string $username, strin return (bool)$result[0]['c']; } + + /** + * Get the users IDs from skip user groups + * + * @param Repository $config + * + * @return array + */ + protected function getSkipUIDs(Repository $config) + { + $skipUIDs = []; + $skipGroups = $config->get('fresh::cleaners.skip_user_groups'); + + if (is_array($skipGroups)) { + foreach ($skipGroups as $skipGroup) { + $group = Group::getByName($skipGroup); + if ($group) { + $skipUIDs = array_unique(array_merge($skipUIDs, $group->getGroupMemberIDs())); + } + } + } + + return $skipUIDs; + } }