Skip to content

Commit 9dc9cbf

Browse files
committed
src/Entity/User.php
1 parent 34d5654 commit 9dc9cbf

File tree

3 files changed

+189
-2
lines changed

3 files changed

+189
-2
lines changed

config/packages/security.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ security:
44
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
55
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
66
providers:
7-
users_in_memory: { memory: null }
7+
# used to reload user from session & other features (e.g. switch_user)
8+
app_user_provider:
9+
entity:
10+
class: App\Entity\User
11+
property: username
812
firewalls:
913
dev:
1014
pattern: ^/(_(profiler|wdt)|css|images|js)/
1115
security: false
1216
main:
1317
lazy: true
14-
provider: users_in_memory
18+
provider: app_user_provider
1519

1620
# activate different ways to authenticate
1721
# https://symfony.com/doc/current/security.html#the-firewall

src/Entity/User.php

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use App\Repository\UserRepository;
6+
use Doctrine\DBAL\Types\Types;
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
9+
use Symfony\Component\Security\Core\User\UserInterface;
10+
11+
#[ORM\Entity(repositoryClass: UserRepository::class)]
12+
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USERNAME', fields: ['username'])]
13+
class User implements UserInterface, PasswordAuthenticatedUserInterface
14+
{
15+
#[ORM\Id]
16+
#[ORM\GeneratedValue]
17+
#[ORM\Column]
18+
private ?int $id = null;
19+
20+
#[ORM\Column(length: 180)]
21+
private ?string $username = null;
22+
23+
/**
24+
* @var list<string> The user roles
25+
*/
26+
#[ORM\Column]
27+
private array $roles = [];
28+
29+
/**
30+
* @var string The hashed password
31+
*/
32+
#[ORM\Column]
33+
private ?string $password = null;
34+
35+
#[ORM\Column(type: Types::TEXT, nullable: true)]
36+
private ?string $description = null;
37+
38+
public function getId(): ?int
39+
{
40+
return $this->id;
41+
}
42+
43+
public function getUsername(): ?string
44+
{
45+
return $this->username;
46+
}
47+
48+
public function setUsername(string $username): static
49+
{
50+
$this->username = $username;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* A visual identifier that represents this user.
57+
*
58+
* @see UserInterface
59+
*/
60+
public function getUserIdentifier(): string
61+
{
62+
return (string) $this->username;
63+
}
64+
65+
/**
66+
* @see UserInterface
67+
* @return list<string>
68+
*/
69+
public function getRoles(): array
70+
{
71+
$roles = $this->roles;
72+
// guarantee every user at least has ROLE_USER
73+
$roles[] = 'ROLE_USER';
74+
75+
return array_unique($roles);
76+
}
77+
78+
/**
79+
* @param list<string> $roles
80+
*/
81+
public function setRoles(array $roles): static
82+
{
83+
$this->roles = $roles;
84+
85+
return $this;
86+
}
87+
88+
/**
89+
* @see PasswordAuthenticatedUserInterface
90+
*/
91+
public function getPassword(): ?string
92+
{
93+
return $this->password;
94+
}
95+
96+
public function setPassword(string $password): static
97+
{
98+
$this->password = $password;
99+
100+
return $this;
101+
}
102+
103+
/**
104+
* @see UserInterface
105+
*/
106+
public function eraseCredentials(): void
107+
{
108+
// If you store any temporary, sensitive data on the user, clear it here
109+
// $this->plainPassword = null;
110+
}
111+
112+
public function getDescription(): ?string
113+
{
114+
return $this->description;
115+
}
116+
117+
public function setDescription(?string $description): static
118+
{
119+
$this->description = $description;
120+
121+
return $this;
122+
}
123+
}

src/Repository/UserRepository.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Repository;
4+
5+
use App\Entity\User;
6+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7+
use Doctrine\Persistence\ManagerRegistry;
8+
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
9+
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
10+
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
11+
12+
/**
13+
* @extends ServiceEntityRepository<User>
14+
*/
15+
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
16+
{
17+
public function __construct(ManagerRegistry $registry)
18+
{
19+
parent::__construct($registry, User::class);
20+
}
21+
22+
/**
23+
* Used to upgrade (rehash) the user's password automatically over time.
24+
*/
25+
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
26+
{
27+
if (!$user instanceof User) {
28+
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $user::class));
29+
}
30+
31+
$user->setPassword($newHashedPassword);
32+
$this->getEntityManager()->persist($user);
33+
$this->getEntityManager()->flush();
34+
}
35+
36+
// /**
37+
// * @return User[] Returns an array of User objects
38+
// */
39+
// public function findByExampleField($value): array
40+
// {
41+
// return $this->createQueryBuilder('u')
42+
// ->andWhere('u.exampleField = :val')
43+
// ->setParameter('val', $value)
44+
// ->orderBy('u.id', 'ASC')
45+
// ->setMaxResults(10)
46+
// ->getQuery()
47+
// ->getResult()
48+
// ;
49+
// }
50+
51+
// public function findOneBySomeField($value): ?User
52+
// {
53+
// return $this->createQueryBuilder('u')
54+
// ->andWhere('u.exampleField = :val')
55+
// ->setParameter('val', $value)
56+
// ->getQuery()
57+
// ->getOneOrNullResult()
58+
// ;
59+
// }
60+
}

0 commit comments

Comments
 (0)