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,122 @@
<?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\Model;
/**
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
abstract class Group implements GroupInterface
{
/**
* @var mixed
*/
protected $id;
/**
* @var string
*/
protected $name;
/**
* @var array
*/
protected $roles;
/**
* Group constructor.
*
* @param string $name
* @param array $roles
*/
public function __construct($name, $roles = array())
{
$this->name = $name;
$this->roles = $roles;
}
/**
* {@inheritdoc}
*/
public function addRole($role)
{
if (!$this->hasRole($role)) {
$this->roles[] = strtoupper($role);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
/**
* {@inheritdoc}
*/
public function hasRole($role)
{
return in_array(strtoupper($role), $this->roles, true);
}
/**
* {@inheritdoc}
*/
public function getRoles()
{
return $this->roles;
}
/**
* {@inheritdoc}
*/
public function removeRole($role)
{
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
unset($this->roles[$key]);
$this->roles = array_values($this->roles);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* {@inheritdoc}
*/
public function setRoles(array $roles)
{
$this->roles = $roles;
return $this;
}
}

View File

@@ -0,0 +1,69 @@
<?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\Model;
/**
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @author Christophe Coevoet <stof@notk.org>
*/
interface GroupInterface
{
/**
* @param string $role
*
* @return static
*/
public function addRole($role);
/**
* @return int
*/
public function getId();
/**
* @return string
*/
public function getName();
/**
* @param string $role
*
* @return bool
*/
public function hasRole($role);
/**
* @return array
*/
public function getRoles();
/**
* @param string $role
*
* @return static
*/
public function removeRole($role);
/**
* @param string $name
*
* @return static
*/
public function setName($name);
/**
* @param array $roles
*
* @return static
*/
public function setRoles(array $roles);
}

View File

@@ -0,0 +1,39 @@
<?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\Model;
/**
* Abstract Group Manager implementation which can be used as base class for your
* concrete manager.
*
* @author Christophe Coevoet <stof@notk.org>
*/
abstract class GroupManager implements GroupManagerInterface
{
/**
* {@inheritdoc}
*/
public function createGroup($name)
{
$class = $this->getClass();
return new $class($name);
}
/**
* {@inheritdoc}
*/
public function findGroupByName($name)
{
return $this->findGroupBy(array('name' => $name));
}
}

View File

@@ -0,0 +1,78 @@
<?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\Model;
/**
* Interface to be implemented by group managers. This adds an additional level
* of abstraction between your application, and the actual repository.
*
* All changes to groups should happen through this interface.
*
* @author Christophe Coevoet <stof@notk.org>
*/
interface GroupManagerInterface
{
/**
* Returns an empty group instance.
*
* @param string $name
*
* @return GroupInterface
*/
public function createGroup($name);
/**
* Deletes a group.
*
* @param GroupInterface $group
*/
public function deleteGroup(GroupInterface $group);
/**
* Finds one group by the given criteria.
*
* @param array $criteria
*
* @return GroupInterface
*/
public function findGroupBy(array $criteria);
/**
* Finds a group by name.
*
* @param string $name
*
* @return GroupInterface
*/
public function findGroupByName($name);
/**
* Returns a collection with all group instances.
*
* @return \Traversable
*/
public function findGroups();
/**
* Returns the group's fully qualified class name.
*
* @return string
*/
public function getClass();
/**
* Updates a group.
*
* @param GroupInterface $group
*/
public function updateGroup(GroupInterface $group);
}

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\Model;
/**
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @author Christophe Coevoet <stof@notk.org>
*/
interface GroupableInterface
{
/**
* Gets the groups granted to the user.
*
* @return \Traversable
*/
public function getGroups();
/**
* Gets the name of the groups which includes the user.
*
* @return array
*/
public function getGroupNames();
/**
* Indicates whether the user belongs to the specified group or not.
*
* @param string $name Name of the group
*
* @return bool
*/
public function hasGroup($name);
/**
* Add a group to the user groups.
*
* @param GroupInterface $group
*
* @return static
*/
public function addGroup(GroupInterface $group);
/**
* Remove a group from the user groups.
*
* @param GroupInterface $group
*
* @return static
*/
public function removeGroup(GroupInterface $group);
}

View File

@@ -0,0 +1,557 @@
<?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\Model;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* Storage agnostic user object.
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
abstract class User implements UserInterface, GroupableInterface
{
/**
* @var mixed
*/
protected $id;
/**
* @var string
*/
protected $username;
/**
* @var string
*/
protected $usernameCanonical;
/**
* @var string
*/
protected $email;
/**
* @var string
*/
protected $emailCanonical;
/**
* @var bool
*/
protected $enabled;
/**
* The salt to use for hashing.
*
* @var string
*/
protected $salt;
/**
* Encrypted password. Must be persisted.
*
* @var string
*/
protected $password;
/**
* Plain password. Used for model validation. Must not be persisted.
*
* @var string
*/
protected $plainPassword;
/**
* @var \DateTime|null
*/
protected $lastLogin;
/**
* Random string sent to the user email address in order to verify it.
*
* @var string|null
*/
protected $confirmationToken;
/**
* @var \DateTime|null
*/
protected $passwordRequestedAt;
/**
* @var GroupInterface[]|Collection
*/
protected $groups;
/**
* @var array
*/
protected $roles;
/**
* User constructor.
*/
public function __construct()
{
$this->enabled = false;
$this->roles = array();
}
/**
* @return string
*/
public function __toString()
{
return (string) $this->getUsername();
}
/**
* {@inheritdoc}
*/
public function addRole($role)
{
$role = strtoupper($role);
if ($role === static::ROLE_DEFAULT) {
return $this;
}
if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
return $this;
}
/**
* {@inheritdoc}
*/
public function serialize()
{
return serialize(array(
$this->password,
$this->salt,
$this->usernameCanonical,
$this->username,
$this->enabled,
$this->id,
$this->email,
$this->emailCanonical,
));
}
/**
* {@inheritdoc}
*/
public function unserialize($serialized)
{
$data = unserialize($serialized);
if (13 === count($data)) {
// Unserializing a User object from 1.3.x
unset($data[4], $data[5], $data[6], $data[9], $data[10]);
$data = array_values($data);
} elseif (11 === count($data)) {
// Unserializing a User from a dev version somewhere between 2.0-alpha3 and 2.0-beta1
unset($data[4], $data[7], $data[8]);
$data = array_values($data);
}
list(
$this->password,
$this->salt,
$this->usernameCanonical,
$this->username,
$this->enabled,
$this->id,
$this->email,
$this->emailCanonical
) = $data;
}
/**
* {@inheritdoc}
*/
public function eraseCredentials()
{
$this->plainPassword = null;
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getUsername()
{
return $this->username;
}
/**
* {@inheritdoc}
*/
public function getUsernameCanonical()
{
return $this->usernameCanonical;
}
/**
* {@inheritdoc}
*/
public function getSalt()
{
return $this->salt;
}
/**
* {@inheritdoc}
*/
public function getEmail()
{
return $this->email;
}
/**
* {@inheritdoc}
*/
public function getEmailCanonical()
{
return $this->emailCanonical;
}
/**
* {@inheritdoc}
*/
public function getPassword()
{
return $this->password;
}
/**
* {@inheritdoc}
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* Gets the last login time.
*
* @return \DateTime|null
*/
public function getLastLogin()
{
return $this->lastLogin;
}
/**
* {@inheritdoc}
*/
public function getConfirmationToken()
{
return $this->confirmationToken;
}
/**
* {@inheritdoc}
*/
public function getRoles()
{
$roles = $this->roles;
foreach ($this->getGroups() as $group) {
$roles = array_merge($roles, $group->getRoles());
}
// we need to make sure to have at least one role
$roles[] = static::ROLE_DEFAULT;
return array_unique($roles);
}
/**
* {@inheritdoc}
*/
public function hasRole($role)
{
return in_array(strtoupper($role), $this->getRoles(), true);
}
/**
* {@inheritdoc}
*/
public function isAccountNonExpired()
{
return true;
}
/**
* {@inheritdoc}
*/
public function isAccountNonLocked()
{
return true;
}
/**
* {@inheritdoc}
*/
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->enabled;
}
/**
* {@inheritdoc}
*/
public function isSuperAdmin()
{
return $this->hasRole(static::ROLE_SUPER_ADMIN);
}
/**
* {@inheritdoc}
*/
public function removeRole($role)
{
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
unset($this->roles[$key]);
$this->roles = array_values($this->roles);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* {@inheritdoc}
*/
public function setUsernameCanonical($usernameCanonical)
{
$this->usernameCanonical = $usernameCanonical;
return $this;
}
/**
* {@inheritdoc}
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* {@inheritdoc}
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* {@inheritdoc}
*/
public function setEmailCanonical($emailCanonical)
{
$this->emailCanonical = $emailCanonical;
return $this;
}
/**
* {@inheritdoc}
*/
public function setEnabled($boolean)
{
$this->enabled = (bool) $boolean;
return $this;
}
/**
* {@inheritdoc}
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* {@inheritdoc}
*/
public function setSuperAdmin($boolean)
{
if (true === $boolean) {
$this->addRole(static::ROLE_SUPER_ADMIN);
} else {
$this->removeRole(static::ROLE_SUPER_ADMIN);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function setPlainPassword($password)
{
$this->plainPassword = $password;
return $this;
}
/**
* {@inheritdoc}
*/
public function setLastLogin(\DateTime $time = null)
{
$this->lastLogin = $time;
return $this;
}
/**
* {@inheritdoc}
*/
public function setConfirmationToken($confirmationToken)
{
$this->confirmationToken = $confirmationToken;
return $this;
}
/**
* {@inheritdoc}
*/
public function setPasswordRequestedAt(\DateTime $date = null)
{
$this->passwordRequestedAt = $date;
return $this;
}
/**
* Gets the timestamp that the user requested a password reset.
*
* @return null|\DateTime
*/
public function getPasswordRequestedAt()
{
return $this->passwordRequestedAt;
}
/**
* {@inheritdoc}
*/
public function isPasswordRequestNonExpired($ttl)
{
return $this->getPasswordRequestedAt() instanceof \DateTime &&
$this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
}
/**
* {@inheritdoc}
*/
public function setRoles(array $roles)
{
$this->roles = array();
foreach ($roles as $role) {
$this->addRole($role);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getGroups()
{
return $this->groups ?: $this->groups = new ArrayCollection();
}
/**
* {@inheritdoc}
*/
public function getGroupNames()
{
$names = array();
foreach ($this->getGroups() as $group) {
$names[] = $group->getName();
}
return $names;
}
/**
* {@inheritdoc}
*/
public function hasGroup($name)
{
return in_array($name, $this->getGroupNames());
}
/**
* {@inheritdoc}
*/
public function addGroup(GroupInterface $group)
{
if (!$this->getGroups()->contains($group)) {
$this->getGroups()->add($group);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function removeGroup(GroupInterface $group)
{
if ($this->getGroups()->contains($group)) {
$this->getGroups()->removeElement($group);
}
return $this;
}
}

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\Model;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface UserInterface extends AdvancedUserInterface, \Serializable
{
const ROLE_DEFAULT = 'ROLE_USER';
const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
/**
* Returns the user unique id.
*
* @return mixed
*/
public function getId();
/**
* Sets the username.
*
* @param string $username
*
* @return static
*/
public function setUsername($username);
/**
* Gets the canonical username in search and sort queries.
*
* @return string
*/
public function getUsernameCanonical();
/**
* Sets the canonical username.
*
* @param string $usernameCanonical
*
* @return static
*/
public function setUsernameCanonical($usernameCanonical);
/**
* @param string|null $salt
*
* @return static
*/
public function setSalt($salt);
/**
* Gets email.
*
* @return string
*/
public function getEmail();
/**
* Sets the email.
*
* @param string $email
*
* @return static
*/
public function setEmail($email);
/**
* Gets the canonical email in search and sort queries.
*
* @return string
*/
public function getEmailCanonical();
/**
* Sets the canonical email.
*
* @param string $emailCanonical
*
* @return static
*/
public function setEmailCanonical($emailCanonical);
/**
* Gets the plain password.
*
* @return string
*/
public function getPlainPassword();
/**
* Sets the plain password.
*
* @param string $password
*
* @return static
*/
public function setPlainPassword($password);
/**
* Sets the hashed password.
*
* @param string $password
*
* @return static
*/
public function setPassword($password);
/**
* Tells if the the given user has the super admin role.
*
* @return bool
*/
public function isSuperAdmin();
/**
* @param bool $boolean
*
* @return static
*/
public function setEnabled($boolean);
/**
* Sets the super admin status.
*
* @param bool $boolean
*
* @return static
*/
public function setSuperAdmin($boolean);
/**
* Gets the confirmation token.
*
* @return string|null
*/
public function getConfirmationToken();
/**
* Sets the confirmation token.
*
* @param string|null $confirmationToken
*
* @return static
*/
public function setConfirmationToken($confirmationToken);
/**
* Sets the timestamp that the user requested a password reset.
*
* @param null|\DateTime $date
*
* @return static
*/
public function setPasswordRequestedAt(\DateTime $date = null);
/**
* Checks whether the password reset request has expired.
*
* @param int $ttl Requests older than this many seconds will be considered expired
*
* @return bool
*/
public function isPasswordRequestNonExpired($ttl);
/**
* Sets the last login time.
*
* @param \DateTime|null $time
*
* @return static
*/
public function setLastLogin(\DateTime $time = null);
/**
* Never use this to check if this user has access to anything!
*
* Use the AuthorizationChecker, or an implementation of AccessDecisionManager
* instead, e.g.
*
* $authorizationChecker->isGranted('ROLE_USER');
*
* @param string $role
*
* @return bool
*/
public function hasRole($role);
/**
* Sets the roles of the user.
*
* This overwrites any previous roles.
*
* @param array $roles
*
* @return static
*/
public function setRoles(array $roles);
/**
* Adds a role to the user.
*
* @param string $role
*
* @return static
*/
public function addRole($role);
/**
* Removes a role to the user.
*
* @param string $role
*
* @return static
*/
public function removeRole($role);
}

View File

@@ -0,0 +1,115 @@
<?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\Model;
use FOS\UserBundle\Util\CanonicalFieldsUpdater;
use FOS\UserBundle\Util\PasswordUpdaterInterface;
/**
* Abstract User Manager implementation which can be used as base class for your
* concrete manager.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
abstract class UserManager implements UserManagerInterface
{
private $passwordUpdater;
private $canonicalFieldsUpdater;
public function __construct(PasswordUpdaterInterface $passwordUpdater, CanonicalFieldsUpdater $canonicalFieldsUpdater)
{
$this->passwordUpdater = $passwordUpdater;
$this->canonicalFieldsUpdater = $canonicalFieldsUpdater;
}
/**
* {@inheritdoc}
*/
public function createUser()
{
$class = $this->getClass();
$user = new $class();
return $user;
}
/**
* {@inheritdoc}
*/
public function findUserByEmail($email)
{
return $this->findUserBy(array('emailCanonical' => $this->canonicalFieldsUpdater->canonicalizeEmail($email)));
}
/**
* {@inheritdoc}
*/
public function findUserByUsername($username)
{
return $this->findUserBy(array('usernameCanonical' => $this->canonicalFieldsUpdater->canonicalizeUsername($username)));
}
/**
* {@inheritdoc}
*/
public function findUserByUsernameOrEmail($usernameOrEmail)
{
if (preg_match('/^.+\@\S+\.\S+$/', $usernameOrEmail)) {
$user = $this->findUserByEmail($usernameOrEmail);
if (null !== $user) {
return $user;
}
}
return $this->findUserByUsername($usernameOrEmail);
}
/**
* {@inheritdoc}
*/
public function findUserByConfirmationToken($token)
{
return $this->findUserBy(array('confirmationToken' => $token));
}
/**
* {@inheritdoc}
*/
public function updateCanonicalFields(UserInterface $user)
{
$this->canonicalFieldsUpdater->updateCanonicalFields($user);
}
/**
* {@inheritdoc}
*/
public function updatePassword(UserInterface $user)
{
$this->passwordUpdater->hashPassword($user);
}
/**
* @return PasswordUpdaterInterface
*/
protected function getPasswordUpdater()
{
return $this->passwordUpdater;
}
/**
* @return CanonicalFieldsUpdater
*/
protected function getCanonicalFieldsUpdater()
{
return $this->canonicalFieldsUpdater;
}
}

View File

@@ -0,0 +1,129 @@
<?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\Model;
/**
* Interface to be implemented by user managers. This adds an additional level
* of abstraction between your application, and the actual repository.
*
* All changes to users should happen through this interface.
*
* The class also contains ACL annotations which will only work if you have the
* SecurityExtraBundle installed, otherwise they will simply be ignored.
*
* @author Gordon Franke <info@nevalon.de>
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface UserManagerInterface
{
/**
* Creates an empty user instance.
*
* @return UserInterface
*/
public function createUser();
/**
* Deletes a user.
*
* @param UserInterface $user
*/
public function deleteUser(UserInterface $user);
/**
* Finds one user by the given criteria.
*
* @param array $criteria
*
* @return UserInterface|null
*/
public function findUserBy(array $criteria);
/**
* Find a user by its username.
*
* @param string $username
*
* @return UserInterface|null
*/
public function findUserByUsername($username);
/**
* Finds a user by its email.
*
* @param string $email
*
* @return UserInterface|null
*/
public function findUserByEmail($email);
/**
* Finds a user by its username or email.
*
* @param string $usernameOrEmail
*
* @return UserInterface|null
*/
public function findUserByUsernameOrEmail($usernameOrEmail);
/**
* Finds a user by its confirmationToken.
*
* @param string $token
*
* @return UserInterface|null
*/
public function findUserByConfirmationToken($token);
/**
* Returns a collection with all user instances.
*
* @return \Traversable
*/
public function findUsers();
/**
* Returns the user's fully qualified class name.
*
* @return string
*/
public function getClass();
/**
* Reloads a user.
*
* @param UserInterface $user
*/
public function reloadUser(UserInterface $user);
/**
* Updates a user.
*
* @param UserInterface $user
*/
public function updateUser(UserInterface $user);
/**
* Updates the canonical username and email fields for a user.
*
* @param UserInterface $user
*/
public function updateCanonicalFields(UserInterface $user);
/**
* Updates a user password if a plain password is set.
*
* @param UserInterface $user
*/
public function updatePassword(UserInterface $user);
}