This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Util;
use FOS\UserBundle\Model\UserInterface;
/**
* Class updating the canonical fields of the user.
*
* @author Christophe Coevoet <stof@notk.org>
*/
class CanonicalFieldsUpdater
{
private $usernameCanonicalizer;
private $emailCanonicalizer;
public function __construct(CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer)
{
$this->usernameCanonicalizer = $usernameCanonicalizer;
$this->emailCanonicalizer = $emailCanonicalizer;
}
public function updateCanonicalFields(UserInterface $user)
{
$user->setUsernameCanonical($this->canonicalizeUsername($user->getUsername()));
$user->setEmailCanonical($this->canonicalizeEmail($user->getEmail()));
}
/**
* Canonicalizes an email.
*
* @param string|null $email
*
* @return string|null
*/
public function canonicalizeEmail($email)
{
return $this->emailCanonicalizer->canonicalize($email);
}
/**
* Canonicalizes a username.
*
* @param string|null $username
*
* @return string|null
*/
public function canonicalizeUsername($username)
{
return $this->usernameCanonicalizer->canonicalize($username);
}
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Util;
class Canonicalizer implements CanonicalizerInterface
{
/**
* {@inheritdoc}
*/
public function canonicalize($string)
{
if (null === $string) {
return;
}
$encoding = mb_detect_encoding($string);
$result = $encoding
? mb_convert_case($string, MB_CASE_LOWER, $encoding)
: mb_convert_case($string, MB_CASE_LOWER);
return $result;
}
}

View File

@@ -0,0 +1,22 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Util;
interface CanonicalizerInterface
{
/**
* @param string $string
*
* @return string
*/
public function canonicalize($string);
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Util;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
/**
* Class updating the hashed password in the user when there is a new password.
*
* @author Christophe Coevoet <stof@notk.org>
*/
class PasswordUpdater implements PasswordUpdaterInterface
{
private $encoderFactory;
public function __construct(EncoderFactoryInterface $encoderFactory)
{
$this->encoderFactory = $encoderFactory;
}
public function hashPassword(UserInterface $user)
{
$plainPassword = $user->getPlainPassword();
if (0 === strlen($plainPassword)) {
return;
}
$encoder = $this->encoderFactory->getEncoder($user);
if ($encoder instanceof BCryptPasswordEncoder) {
$user->setSalt(null);
} else {
$salt = rtrim(str_replace('+', '.', base64_encode(random_bytes(32))), '=');
$user->setSalt($salt);
}
$hashedPassword = $encoder->encodePassword($plainPassword, $user->getSalt());
$user->setPassword($hashedPassword);
$user->eraseCredentials();
}
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Util;
use FOS\UserBundle\Model\UserInterface;
/**
* @author Christophe Coevoet <stof@notk.org>
*/
interface PasswordUpdaterInterface
{
/**
* Updates the hashed password in the user when there is a new password.
*
* The implement should be a no-op in case there is no new password (it should not erase the
* existing hash with a wrong one).
*
* @param UserInterface $user
*
* @return void
*/
public function hashPassword(UserInterface $user);
}

View File

@@ -0,0 +1,23 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Util;
class TokenGenerator implements TokenGeneratorInterface
{
/**
* {@inheritdoc}
*/
public function generateToken()
{
return rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '=');
}
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Util;
interface TokenGeneratorInterface
{
/**
* @return string
*/
public function generateToken();
}

View File

@@ -0,0 +1,230 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Util;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Executes some manipulations on the users.
*
* @author Christophe Coevoet <stof@notk.org>
* @author Luis Cordova <cordoval@gmail.com>
*/
class UserManipulator
{
/**
* User manager.
*
* @var UserManagerInterface
*/
private $userManager;
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @var RequestStack
*/
private $requestStack;
/**
* UserManipulator constructor.
*
* @param UserManagerInterface $userManager
* @param EventDispatcherInterface $dispatcher
* @param RequestStack $requestStack
*/
public function __construct(UserManagerInterface $userManager, EventDispatcherInterface $dispatcher, RequestStack $requestStack)
{
$this->userManager = $userManager;
$this->dispatcher = $dispatcher;
$this->requestStack = $requestStack;
}
/**
* Creates a user and returns it.
*
* @param string $username
* @param string $password
* @param string $email
* @param bool $active
* @param bool $superadmin
*
* @return \FOS\UserBundle\Model\UserInterface
*/
public function create($username, $password, $email, $active, $superadmin)
{
$user = $this->userManager->createUser();
$user->setUsername($username);
$user->setEmail($email);
$user->setPlainPassword($password);
$user->setEnabled((bool) $active);
$user->setSuperAdmin((bool) $superadmin);
$this->userManager->updateUser($user);
$event = new UserEvent($user, $this->getRequest());
$this->dispatcher->dispatch(FOSUserEvents::USER_CREATED, $event);
return $user;
}
/**
* Activates the given user.
*
* @param string $username
*/
public function activate($username)
{
$user = $this->findUserByUsernameOrThrowException($username);
$user->setEnabled(true);
$this->userManager->updateUser($user);
$event = new UserEvent($user, $this->getRequest());
$this->dispatcher->dispatch(FOSUserEvents::USER_ACTIVATED, $event);
}
/**
* Deactivates the given user.
*
* @param string $username
*/
public function deactivate($username)
{
$user = $this->findUserByUsernameOrThrowException($username);
$user->setEnabled(false);
$this->userManager->updateUser($user);
$event = new UserEvent($user, $this->getRequest());
$this->dispatcher->dispatch(FOSUserEvents::USER_DEACTIVATED, $event);
}
/**
* Changes the password for the given user.
*
* @param string $username
* @param string $password
*/
public function changePassword($username, $password)
{
$user = $this->findUserByUsernameOrThrowException($username);
$user->setPlainPassword($password);
$this->userManager->updateUser($user);
$event = new UserEvent($user, $this->getRequest());
$this->dispatcher->dispatch(FOSUserEvents::USER_PASSWORD_CHANGED, $event);
}
/**
* Promotes the given user.
*
* @param string $username
*/
public function promote($username)
{
$user = $this->findUserByUsernameOrThrowException($username);
$user->setSuperAdmin(true);
$this->userManager->updateUser($user);
$event = new UserEvent($user, $this->getRequest());
$this->dispatcher->dispatch(FOSUserEvents::USER_PROMOTED, $event);
}
/**
* Demotes the given user.
*
* @param string $username
*/
public function demote($username)
{
$user = $this->findUserByUsernameOrThrowException($username);
$user->setSuperAdmin(false);
$this->userManager->updateUser($user);
$event = new UserEvent($user, $this->getRequest());
$this->dispatcher->dispatch(FOSUserEvents::USER_DEMOTED, $event);
}
/**
* Adds role to the given user.
*
* @param string $username
* @param string $role
*
* @return bool true if role was added, false if user already had the role
*/
public function addRole($username, $role)
{
$user = $this->findUserByUsernameOrThrowException($username);
if ($user->hasRole($role)) {
return false;
}
$user->addRole($role);
$this->userManager->updateUser($user);
return true;
}
/**
* Removes role from the given user.
*
* @param string $username
* @param string $role
*
* @return bool true if role was removed, false if user didn't have the role
*/
public function removeRole($username, $role)
{
$user = $this->findUserByUsernameOrThrowException($username);
if (!$user->hasRole($role)) {
return false;
}
$user->removeRole($role);
$this->userManager->updateUser($user);
return true;
}
/**
* Finds a user by his username and throws an exception if we can't find it.
*
* @param string $username
*
* @throws \InvalidArgumentException When user does not exist
*
* @return UserInterface
*/
private function findUserByUsernameOrThrowException($username)
{
$user = $this->userManager->findUserByUsername($username);
if (!$user) {
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
}
return $user;
}
/**
* @return Request
*/
private function getRequest()
{
return $this->requestStack->getCurrentRequest();
}
}