Skip to content

Commit cdcd168

Browse files
feature #1460 [make:user] handle ORM\Column.unique deprecation - use ORM\UniqueConstraint
Co-authored-by: Jesse Rushlow <40327885+jrushlow@users.noreply.github.com>
1 parent c4c6b5f commit cdcd168

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

src/Security/UserClassBuilder.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\MakerBundle\Security;
1313

1414
use PhpParser\Node;
15+
use Symfony\Bundle\MakerBundle\Str;
1516
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassProperty;
1617
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
1718
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
@@ -28,6 +29,8 @@ public function addUserInterfaceImplementation(ClassSourceManipulator $manipulat
2829
{
2930
$manipulator->addInterface(UserInterface::class);
3031

32+
$this->addUniqueConstraint($manipulator, $userClassConfig);
33+
3134
$this->addGetUsername($manipulator, $userClassConfig);
3235

3336
$this->addGetRoles($manipulator, $userClassConfig);
@@ -57,7 +60,6 @@ private function addGetUsername(ClassSourceManipulator $manipulator, UserClassCo
5760
propertyName: $userClassConfig->getIdentityPropertyName(),
5861
type: 'string',
5962
length: 180,
60-
unique: true,
6163
)
6264
);
6365
} else {
@@ -259,4 +261,19 @@ private function addEraseCredentials(ClassSourceManipulator $manipulator): void
259261

260262
$manipulator->addMethodBuilder($builder);
261263
}
264+
265+
private function addUniqueConstraint(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig): void
266+
{
267+
if (!$userClassConfig->isEntity()) {
268+
return;
269+
}
270+
271+
$manipulator->addAttributeToClass(
272+
'ORM\\UniqueConstraint',
273+
[
274+
'name' => 'UNIQ_IDENTIFIER_'.strtoupper(Str::asSnakeCase($userClassConfig->getIdentityPropertyName())),
275+
'fields' => [$userClassConfig->getIdentityPropertyName()],
276+
]
277+
);
278+
}
262279
}

src/Util/ClassSourceManipulator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ public function addAttributeToClass(string $attributeClass, array $options): voi
395395

396396
$classNode = $this->getClassNode();
397397

398-
$classNode->attrGroups[] = new Node\AttributeGroup([$this->buildAttributeNode($attributeClass, $options)]);
398+
$attributePrefix = str_starts_with($attributeClass, 'ORM\\') ? 'ORM' : null;
399+
400+
$classNode->attrGroups[] = new Node\AttributeGroup([$this->buildAttributeNode($attributeClass, $options, $attributePrefix)]);
399401

400402
$this->updateSourceCodeFromNewStmts();
401403
}
@@ -783,6 +785,10 @@ public function addUseStatementIfNecessary(string $class): string
783785
return $alias;
784786
}
785787

788+
if (str_starts_with($class, $alias)) {
789+
return $class;
790+
}
791+
786792
if ($alias === $shortClassName) {
787793
// we have a conflicting alias!
788794
// to be safe, use the fully-qualified class name

tests/Security/fixtures/expected/UserEntityWithEmailAsIdentifier.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
use Symfony\Component\Security\Core\User\UserInterface;
88

99
#[ORM\Entity]
10+
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
1011
class User implements UserInterface, PasswordAuthenticatedUserInterface
1112
{
1213
#[ORM\Id]
1314
#[ORM\GeneratedValue]
1415
#[ORM\Column()]
1516
private ?int $id = null;
1617

17-
#[ORM\Column(length: 180, unique: true)]
18+
#[ORM\Column(length: 180)]
1819
private ?string $email = null;
1920

2021
/**

tests/Security/fixtures/expected/UserEntityWithPassword.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
use Symfony\Component\Security\Core\User\UserInterface;
88

99
#[ORM\Entity]
10+
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['userIdentifier'])]
1011
class User implements UserInterface, PasswordAuthenticatedUserInterface
1112
{
1213
#[ORM\Id]
1314
#[ORM\GeneratedValue]
1415
#[ORM\Column()]
1516
private ?int $id = null;
1617

17-
#[ORM\Column(length: 180, unique: true)]
18+
#[ORM\Column(length: 180)]
1819
private ?string $userIdentifier = null;
1920

2021
/**

tests/Security/fixtures/expected/UserEntityWithUser_IdentifierAsIdentifier.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
use Symfony\Component\Security\Core\User\UserInterface;
88

99
#[ORM\Entity]
10+
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['user_identifier'])]
1011
class User implements UserInterface, PasswordAuthenticatedUserInterface
1112
{
1213
#[ORM\Id]
1314
#[ORM\GeneratedValue]
1415
#[ORM\Column()]
1516
private ?int $id = null;
1617

17-
#[ORM\Column(length: 180, unique: true)]
18+
#[ORM\Column(length: 180)]
1819
private ?string $user_identifier = null;
1920

2021
/**

tests/Security/fixtures/expected/UserEntityWithoutPassword.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
use Symfony\Component\Security\Core\User\UserInterface;
77

88
#[ORM\Entity]
9+
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['userIdentifier'])]
910
class User implements UserInterface
1011
{
1112
#[ORM\Id]
1213
#[ORM\GeneratedValue]
1314
#[ORM\Column()]
1415
private ?int $id = null;
1516

16-
#[ORM\Column(length: 180, unique: true)]
17+
#[ORM\Column(length: 180)]
1718
private ?string $userIdentifier = null;
1819

1920
/**

0 commit comments

Comments
 (0)