Skip to content

Commit

Permalink
feature #1460 [make:user] handle ORM\Column.unique deprecation - use …
Browse files Browse the repository at this point in the history
…ORM\UniqueConstraint

Co-authored-by: Jesse Rushlow <40327885+jrushlow@users.noreply.github.com>
  • Loading branch information
maelanleborgne and jrushlow authored Feb 27, 2024
1 parent c4c6b5f commit cdcd168
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
19 changes: 18 additions & 1 deletion src/Security/UserClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\MakerBundle\Security;

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

$this->addUniqueConstraint($manipulator, $userClassConfig);

$this->addGetUsername($manipulator, $userClassConfig);

$this->addGetRoles($manipulator, $userClassConfig);
Expand Down Expand Up @@ -57,7 +60,6 @@ private function addGetUsername(ClassSourceManipulator $manipulator, UserClassCo
propertyName: $userClassConfig->getIdentityPropertyName(),
type: 'string',
length: 180,
unique: true,
)
);
} else {
Expand Down Expand Up @@ -259,4 +261,19 @@ private function addEraseCredentials(ClassSourceManipulator $manipulator): void

$manipulator->addMethodBuilder($builder);
}

private function addUniqueConstraint(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig): void
{
if (!$userClassConfig->isEntity()) {
return;
}

$manipulator->addAttributeToClass(
'ORM\\UniqueConstraint',
[
'name' => 'UNIQ_IDENTIFIER_'.strtoupper(Str::asSnakeCase($userClassConfig->getIdentityPropertyName())),
'fields' => [$userClassConfig->getIdentityPropertyName()],
]
);
}
}
8 changes: 7 additions & 1 deletion src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,9 @@ public function addAttributeToClass(string $attributeClass, array $options): voi

$classNode = $this->getClassNode();

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

$classNode->attrGroups[] = new Node\AttributeGroup([$this->buildAttributeNode($attributeClass, $options, $attributePrefix)]);

$this->updateSourceCodeFromNewStmts();
}
Expand Down Expand Up @@ -783,6 +785,10 @@ public function addUseStatementIfNecessary(string $class): string
return $alias;
}

if (str_starts_with($class, $alias)) {
return $class;
}

if ($alias === $shortClassName) {
// we have a conflicting alias!
// to be safe, use the fully-qualified class name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column()]
private ?int $id = null;

#[ORM\Column(length: 180, unique: true)]
#[ORM\Column(length: 180)]
private ?string $email = null;

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Security/fixtures/expected/UserEntityWithPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['userIdentifier'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column()]
private ?int $id = null;

#[ORM\Column(length: 180, unique: true)]
#[ORM\Column(length: 180)]
private ?string $userIdentifier = null;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['user_identifier'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column()]
private ?int $id = null;

#[ORM\Column(length: 180, unique: true)]
#[ORM\Column(length: 180)]
private ?string $user_identifier = null;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['userIdentifier'])]
class User implements UserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column()]
private ?int $id = null;

#[ORM\Column(length: 180, unique: true)]
#[ORM\Column(length: 180)]
private ?string $userIdentifier = null;

/**
Expand Down

0 comments on commit cdcd168

Please sign in to comment.