diff --git a/src/Service/UserService.php b/src/Service/UserService.php index b2dee75..16c3c34 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -16,6 +16,7 @@ use Doctrine\ORM\EntityNotFoundException; use Exception; use League\Bundle\OAuth2ServerBundle\Model\AccessToken; use Random\RandomException; +use RuntimeException; use SebastianBergmann\CodeCoverage\Util\DirectoryCouldNotBeCreatedException; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HttpFoundation\File\Exception\FileException; @@ -46,16 +47,7 @@ class UserService */ public function generateRandomPassword(): string { - $length = 50; // Length of the password - $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+'; - $charactersLength = strlen($characters); - $randomPassword = ''; - - for ($i = 0; $i < $length; $i++) { - $randomPassword .= $characters[random_int(0, $charactersLength - 1)]; - } - - return $randomPassword; + return bin2hex(random_bytes(32)); } @@ -478,7 +470,14 @@ class UserService $user->setEmail(trim($user->getEmail())); if($setPassword) { //FOR SETTING A DEFAULT RANDOM PASSWORD OF 50 CHARACTERS until user set his own password - $user->setPassword($this->generateRandomPassword()); + try { + $user->setPassword(bin2hex(random_bytes(50))); + } catch (RandomException $e) { + $this->loggerService->logError('Error generating random password: ' . $e->getMessage(), [ + 'target_user_id' => $user->getId(), + ]); + throw new RuntimeException('Error generating random password: ' . $e->getMessage()); + } } if($picture) {