Moved custom validators from annotations to attributes

This commit is contained in:
Jan Böhmer 2023-06-11 19:32:15 +02:00
parent e5a14557a2
commit 930adaf439
27 changed files with 50 additions and 148 deletions

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Entity\UserSystem;
use App\Validator\Constraints\NoLockout;
use Doctrine\DBAL\Types\Types;
use App\Entity\Attachments\GroupAttachment;
use App\Entity\Base\AbstractStructuralDBElement;
@ -41,6 +42,7 @@ use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Table('`groups`')]
#[ORM\Index(name: 'group_idx_name', columns: ['name'])]
#[ORM\Index(name: 'group_idx_parent_name', columns: ['parent_id', 'name'])]
#[NoLockout()]
class Group extends AbstractStructuralDBElement implements HasPermissionsInterface
{
/**
@ -66,6 +68,7 @@ class Group extends AbstractStructuralDBElement implements HasPermissionsInterfa
#[Groups(['extended', 'full', 'import'])]
#[ORM\Column(type: Types::BOOLEAN, name: 'enforce_2fa')]
protected bool $enforce2FA = false;
/**
* @var Collection<int, GroupAttachment>
*/
@ -74,15 +77,13 @@ class Group extends AbstractStructuralDBElement implements HasPermissionsInterfa
#[ORM\OrderBy(['name' => 'ASC'])]
protected Collection $attachments;
/**
* @var PermissionData|null
* @ValidPermission()
*/
#[Groups(['full'])]
#[ORM\Embedded(class: 'PermissionData', columnPrefix: 'permissions_')]
#[ValidPermission()]
protected ?PermissionData $permissions = null;
/** @var Collection<int, GroupParameter>
/**
* @var Collection<int, GroupParameter>
*/
#[Assert\Valid]
#[ORM\OneToMany(targetEntity: GroupParameter::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)]

View file

@ -24,6 +24,7 @@ namespace App\Entity\UserSystem;
use App\Repository\UserRepository;
use App\EntityListeners\TreeCacheInvalidationListener;
use App\Validator\Constraints\NoLockout;
use Doctrine\DBAL\Types\Types;
use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Entity\Attachments\UserAttachment;
@ -64,6 +65,7 @@ use Jbtronics\TFAWebauthn\Model\TwoFactorInterface as WebauthnTwoFactorInterface
#[ORM\EntityListeners([TreeCacheInvalidationListener::class])]
#[ORM\Table('`users`')]
#[ORM\Index(name: 'user_idx_username', columns: ['name'])]
#[NoLockout()]
class User extends AttachmentContainingDBElement implements UserInterface, HasPermissionsInterface, TwoFactorInterface,
BackupCodeInterface, TrustedDeviceInterface, WebauthnTwoFactorInterface, PreferredProviderInterface, PasswordAuthenticatedUserInterface, SamlUserInterface
{
@ -83,10 +85,10 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
/**
* @var string|null The theme
* @ValidTheme()
*/
#[Groups(['full', 'import'])]
#[ORM\Column(type: Types::STRING, name: 'config_theme', nullable: true)]
#[ValidTheme()]
protected ?string $theme = null;
/**
@ -127,11 +129,11 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
/**
* @var Group|null the group this user belongs to
* DO NOT PUT A fetch eager here! Otherwise, you can not unset the group of a user! This seems to be some kind of bug in doctrine. Maybe this is fixed in future versions.
* @Selectable()
*/
#[Groups(['extended', 'full', 'import'])]
#[ORM\ManyToOne(targetEntity: 'Group', inversedBy: 'users')]
#[ORM\JoinColumn(name: 'group_id')]
#[Selectable]
protected ?Group $group = null;
/**
@ -244,19 +246,16 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
* Dont use fetch=EAGER here, this will cause problems with setting the currency setting.
* TODO: This is most likely a bug in doctrine/symfony related to the UniqueEntity constraint (it makes a db call).
* TODO: Find a way to use fetch EAGER (this improves performance a bit)
* @Selectable()
*/
#[Groups(['extended', 'full', 'import'])]
#[ORM\ManyToOne(targetEntity: Currency::class)]
#[ORM\JoinColumn(name: 'currency_id')]
#[Selectable]
protected ?Currency $currency = null;
/**
* @var PermissionData|null
* @ValidPermission()
*/
#[Groups(['simple', 'extended', 'full', 'import'])]
#[ORM\Embedded(class: 'PermissionData', columnPrefix: 'permissions_')]
#[ValidPermission()]
protected ?PermissionData $permissions = null;
/**
@ -287,7 +286,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
*
* @return string
*/
public function __toString()
public function __toString(): string
{
$tmp = $this->isDisabled() ? ' [DISABLED]' : '';