PHP code example of nowo-tech / password-policy-bundle

1. Go to this page and download the library: Download nowo-tech/password-policy-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nowo-tech / password-policy-bundle example snippets




return [
  // ...
  Nowo\PasswordPolicyBundle\NowoPasswordPolicyBundle::class => ['all' => true],
];

use Nowo\PasswordPolicyBundle\Validator\PasswordPolicy;

class User implements HasPasswordPolicyInterface
{
  /**
   * @PasswordPolicy()
   */
  private ?string $plainPassword = null;
  
  // ... rest of your entity
}

use Doctrine\ORM\Mapping as ORM;
use Nowo\PasswordPolicyBundle\Model\HasPasswordPolicyInterface;
use Nowo\PasswordPolicyBundle\Model\PasswordHistoryInterface;
use Nowo\PasswordPolicyBundle\Validator\PasswordPolicy;

#[ORM\Entity]
class User implements HasPasswordPolicyInterface
{
  #[ORM\Id]
  #[ORM\GeneratedValue]
  #[ORM\Column]
  private ?int $id = null;
  
  #[ORM\Column]
  private string $password;
  
  #[ORM\Column(type: 'datetime', nullable: true)]
  private ?\DateTime $passwordChangedAt = null;
  
  #[ORM\OneToMany(targetEntity: UserPasswordHistory::class, mappedBy: 'user', cascade: ['persist', 'remove'])]
  private Collection $passwordHistory;
  
  #[PasswordPolicy(
      detectExtensions: true,
      extensionMinLength: 4,
      extensionMessage: 'Cannot use an extension of an old password',
  )]
  private ?string $plainPassword = null;
  
  public function getId(): ?int
  {
    return $this->id;
  }
  
  public function getPassword(): string
  {
    return $this->password;
  }
  
  public function setPassword(string $password): self
  {
    $this->password = $password;
    return $this;
  }
  
  public function getPasswordChangedAt(): ?\DateTime
  {
    return $this->passwordChangedAt;
  }
  
  public function setPasswordChangedAt(\DateTime $dateTime): self
  {
    $this->passwordChangedAt = $dateTime;
    return $this;
  }
  
  public function getPasswordHistory(): Collection
  {
    return $this->passwordHistory;
  }
  
  public function addPasswordHistory(PasswordHistoryInterface $passwordHistory): static
  {
    if (!$this->passwordHistory->contains($passwordHistory)) {
      $this->passwordHistory->add($passwordHistory);
    }
    return $this;
  }
  
  public function removePasswordHistory(PasswordHistoryInterface $passwordHistory): static
  {
    if ($this->passwordHistory->contains($passwordHistory)) {
      $this->passwordHistory->removeElement($passwordHistory);
    }
    return $this;
  }
}

use Doctrine\ORM\Mapping as ORM;
use Nowo\PasswordPolicyBundle\Model\PasswordHistoryInterface;

#[ORM\Entity]
class UserPasswordHistory implements PasswordHistoryInterface
{
  #[ORM\Id]
  #[ORM\GeneratedValue]
  #[ORM\Column]
  private ?int $id = null;
  
  #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'passwordHistory')]
  private User $user;
  
  #[ORM\Column]
  private string $password;
  
  #[ORM\Column(type: 'string', nullable: true)]
  private ?string $salt = null;
  
  #[ORM\Column(type: 'datetime')]
  private \DateTime $createdAt;
  
  public function getId(): ?int
  {
    return $this->id;
  }
  
  public function getPassword(): string
  {
    return $this->password;
  }
  
  public function setPassword(string $password): self
  {
    $this->password = $password;
    return $this;
  }
  
  public function getSalt(): ?string
  {
    return $this->salt;
  }
  
  public function setSalt(?string $salt): self
  {
    $this->salt = $salt;
    return $this;
  }
  
  public function getCreatedAt(): \DateTime
  {
    return $this->createdAt;
  }
  
  public function setCreatedAt(\DateTime $createdAt): self
  {
    $this->createdAt = $createdAt;
    return $this;
  }
}