Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[make:user] handle ORM\Column.unique deprecation - use ORM\UniqueConstrain #1460

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 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,17 @@ private function addEraseCredentials(ClassSourceManipulator $manipulator): void

$manipulator->addMethodBuilder($builder);
}

private function addUniqueConstraint(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig): void
{
if ($userClassConfig->isEntity()) {
maelanleborgne marked this conversation as resolved.
Show resolved Hide resolved
$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
Loading