Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/cleaners.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's set this to be empty by default

/** File types that we don't need to sanitize */
'default_skip_file_types' => [
'jpg', 'jpeg', 'png', 'gif', 'svg', 'apng', 'bmp', 'ico'
Expand Down
29 changes: 28 additions & 1 deletion src/Clean/UserCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
}