From 9cb43a7fe29904e3e28c6348a3ad5d6d0ff056a4 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Wed, 20 Mar 2024 16:55:23 -0500 Subject: [PATCH 1/2] Internal: Remove global constant ROLE_FALLBACK in favor of User::ROLE_FALLBACK --- public/main/admin/user_list.php | 3 ++- public/main/inc/lib/api.lib.php | 7 ------- public/main/install/install.lib.php | 2 +- src/CoreBundle/Entity/User.php | 8 ++++++++ 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/public/main/admin/user_list.php b/public/main/admin/user_list.php index 06d8ebd326..fdcefff65b 100644 --- a/public/main/admin/user_list.php +++ b/public/main/admin/user_list.php @@ -2,6 +2,7 @@ /* For licensing terms, see /license.txt */ +use Chamilo\CoreBundle\Entity\User; use ChamiloSession as Session; use Chamilo\CoreBundle\Component\Utils\StateIcon; @@ -388,7 +389,7 @@ function prepare_user_sql_query(bool $getCount, bool $showDeletedUsers = false): } else { $sql .= !str_contains($sql, 'WHERE') ? ' WHERE u.active <> '.USER_SOFT_DELETED : ' AND u.active <> '.USER_SOFT_DELETED; } - $sql .= ' AND u.status <> '.ROLE_FALLBACK; + $sql .= ' AND u.status <> '.User::ROLE_FALLBACK; return $sql; } diff --git a/public/main/inc/lib/api.lib.php b/public/main/inc/lib/api.lib.php index 475f410075..f03cb6bb69 100644 --- a/public/main/inc/lib/api.lib.php +++ b/public/main/inc/lib/api.lib.php @@ -52,13 +52,6 @@ define('ANONYMOUS', 6); /** global status of a user: low security, necessary for inserting data from * the teacher through HTMLPurifier */ define('COURSEMANAGERLOWSECURITY', 10); -/** - * Global status for the fallback user. - * This special status is used for a system user that acts as a placeholder - * or fallback for content ownership when regular users are deleted. - * This ensures data integrity and prevents orphaned content within the system. - */ -define('ROLE_FALLBACK', 99); // Soft user status define('PLATFORM_ADMIN', 11); define('SESSION_COURSE_COACH', 12); diff --git a/public/main/install/install.lib.php b/public/main/install/install.lib.php index bbf469c8ed..a976dd1d1f 100644 --- a/public/main/install/install.lib.php +++ b/public/main/install/install.lib.php @@ -1539,7 +1539,7 @@ function finishInstallationWithContainer( ->setUsername('fallback_user') ->setEmail('fallback@example.com') ->setPlainPassword($passForm) - ->setStatus(ROLE_FALLBACK) + ->setStatus(User::ROLE_FALLBACK) ->setLastname('Fallback') ->setFirstname('User') ->setCreatorId(1) diff --git a/src/CoreBundle/Entity/User.php b/src/CoreBundle/Entity/User.php index 6be99c3a36..c41a6e0714 100644 --- a/src/CoreBundle/Entity/User.php +++ b/src/CoreBundle/Entity/User.php @@ -86,7 +86,15 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso public const USERNAME_MAX_LENGTH = 100; public const ROLE_DEFAULT = 'ROLE_USER'; public const ANONYMOUS = 6; + + /** + * Global status for the fallback user. + * This special status is used for a system user that acts as a placeholder + * or fallback for content ownership when regular users are deleted. + * This ensures data integrity and prevents orphaned content within the system. + */ public const ROLE_FALLBACK = 99; + /*public const COURSE_MANAGER = 1; public const TEACHER = 1; public const SESSION_ADMIN = 3; From 77eabe24481c399c3f8567cff63e6053bf718da3 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Wed, 20 Mar 2024 16:56:45 -0500 Subject: [PATCH 2/2] Internal: User: Return null instead of throw an exception if user fallback is not found --- src/CoreBundle/Repository/Node/UserRepository.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/CoreBundle/Repository/Node/UserRepository.php b/src/CoreBundle/Repository/Node/UserRepository.php index 943d75c0b4..cebe9ec426 100644 --- a/src/CoreBundle/Repository/Node/UserRepository.php +++ b/src/CoreBundle/Repository/Node/UserRepository.php @@ -318,13 +318,7 @@ class UserRepository extends ResourceRepository implements PasswordUpgraderInter public function getFallbackUser(): ?User { - $fallbackUser = $this->findOneBy(['status' => User::ROLE_FALLBACK], ['id' => 'ASC']); - - if (!$fallbackUser) { - throw new Exception('User not found.'); - } - - return $fallbackUser; + return $this->findOneBy(['status' => User::ROLE_FALLBACK], ['id' => 'ASC']); } public function addUserToResourceNode(int $userId, int $creatorId): ResourceNode