我的應用程式正在使用 Symfony 5.4.2,并且依賴于新的 Authenticator Manager。
我最近遇到了與session_open(). 在調查時,我注意到整個 User 物體都存盤在會話檔案中(作為 PHP serialized 字串),以_sf2_attributes|.
因此,每個相關物體也都存盤在那里(每個屬性、用戶購買及其詳細資訊等),這可能非常繁重(我有超過 100 Kb 的會話檔案,這對我來說聽起來很大)。
預計會以這種方式作業嗎?
有沒有辦法防止序列化所有用戶物體屬性?
uj5u.com熱心網友回復:
您必須序列化您的用戶物體類實作\Serializable介面和定義serialize,unserialize和__sleep方法,定義哪些屬性將包含在會話中。您將__sleep授予只有特定屬性才能進入序列化程序;例如:
/**
* Usuario
*
* @ORM\Table(name="usuario", uniqueConstraints={@ORM\UniqueConstraint(name="usuario_username_uniq", columns={"username"})}, indexes={@ORM\Index(name="usuario_rol_idx", columns={"rol_id"}), @ORM\Index(name="usuario_nodo_idx", columns={"estructura_organizativa_id"}), @ORM\Index(name="usuario_id_publico_idx", columns={"id_publico"})})
* @ORM\Entity(repositoryClass="App\Repository\UsuarioRepository")
* @UniqueEntity("username", message="Ya existe un usuario con ese identificador")
*/
class Usuario implements UserInterface, PasswordAuthenticatedUserInterface, EquatableInterface, \Serializable
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\SequenceGenerator(sequenceName="usuario_id_id_seq", initialValue=1, allocationSize=100)
*/
private $id;
/**
* @var string
*
* @Assert\NotBlank(message="El identificador es obligatorio.")
* @ORM\Column(name="username", type="string", unique=true, length=100, nullable=false)
*/
private $username;
/**
* @var string
*
* @Assert\NotBlank(message="El nombre es obligatorio.")
* @ORM\Column(name="nombre_completo", type="string", length=255, nullable=false)
*/
private $nombreCompleto;
/**
* @var string
*
* @Assert\Length(
* min=6,
* max=255,
* minMessage="La contrase?a debe tener como mínimo {{ limit }} caracteres",
* maxMessage="La contrase?a no puede exceder los {{ limit }} caracteres."
* )
* @ORM\Column(name="password", type="string", length=255, nullable=false)
*/
private $password;
/**
* @var boolean
*
* @ORM\Column(name="activo", type="boolean", nullable=false)
*/
private $activo;
/**
* @var type \App\Entity\Rol
*
* @ORM\ManyToOne(targetEntity="Rol", fetch="EAGER")
* @ORM\JoinColumn(name="rol_id", referencedColumnName="id", nullable=false, onDelete="RESTRICT")
*/
private $rol;
/**
* Constructor
*/
public function __construct()
{
$this->activo = true;
}
/**
* Set username
*
* @param string $username
* @return Usuario
*/
public function setUsername(?string $username): self
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername(): string
{
return $this->username;
}
/**
* Set nombreCompleto
*
* @param string $nombreCompleto
* @return Usuario
*/
public function setNombreCompleto(string $nombreCompleto): self
{
$this->nombreCompleto = $nombreCompleto;
return $this;
}
/**
* Get nombreCompleto
*
* @return string
*/
public function getNombreCompleto(): string
{
return $this->nombreCompleto;
}
/**
*
* @return array
*/
public function getRoles(): array
{
$coleccion = new ArrayCollection();
$coleccion->add($this->getRol()->getToken());
$coleccion[] = "ROLE_USER";
return array_unique($coleccion->toArray());
}
/**
* Set password
*
* @param string $password
* @return Usuario
*/
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword(): string
{
return $this->password;
}
/**
* Set activo
*
* @param bool $activo
* @return Usuario
*/
public function setActivo(bool $activo): self
{
$this->activo = $activo;
return $this;
}
/**
* Get activo
*
* @return bool
*/
public function getActivo(): bool
{
return $this->activo;
}
/**
* Get id
*
* @return integer
*/
public function getId(): ?int
{
return $this->id;
}
/**
*
* @return void
*/
public function eraseCredentials(): void
{
}
/**
*
* @return string
*/
public function getSalt(): string
{
return '';
}
/**
*
* @return bool
*/
public function isAccountNonExpired(): bool
{
return true;
}
/**
*
* @return bool
*/
public function isAccountNonLocked(): bool
{
return true;
}
/**
*
* @return bool
*/
public function isCredentialsNonExpired(): bool
{
return true;
}
/**
*
* @return bool
*/
public function isEnabled(): bool
{
return $this->activo;
}
/**
*
* @return string
*/
public function serialize(): string
{
return serialize([
$this->username,
$this->nombreCompleto,
$this->password,
$this->activo,
$this->id,
$this->idPublico
]);
}
/**
*
* @param type $serialized
* @return void
*/
public function unserialize($serialized): void
{
list(
$this->username,
$this->nombreCompleto,
$this->password,
$this->activo,
$this->id,
$this->idPublico
) = unserialize($serialized);
}
/**
*
* @return array
*/
public function __sleep(): array
{
return ['username', 'nombreCompleto', 'password', 'activo', 'id', 'idPublico'];
}
/**
*
* @return string
*/
public function __toString(): string
{
return $this->nombreCompleto;
}
/**
*
* @param UserInterface $user
* @return bool
*/
public function isEqualTo(UserInterface $user): bool
{
if ($this->getUsername() === $user->getUsername() && $this->getPassword() === $user->getPassword() && $this->getSalt() === $user->getSalt()) {
return true;
}
return false;
}
/**
* @return string
* * */
public function getUserIdentifier(): string
{
return (string) $this->username;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/441571.html
