refs #425. OrganizationalUnitParent validator
parent
74fac8d0af
commit
5168d44fd4
|
@ -0,0 +1,33 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DoctrineMigrations;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-generated Migration: Please modify to your needs!
|
||||||
|
*/
|
||||||
|
final class Version20240621085144 extends AbstractMigration
|
||||||
|
{
|
||||||
|
public function getDescription(): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function up(Schema $schema): void
|
||||||
|
{
|
||||||
|
// this up() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->addSql('DROP INDEX UNIQ_IDENTIFIER_NAME ON organizational_unit');
|
||||||
|
$this->addSql('CREATE UNIQUE INDEX UNIQ_IDENTIFIER_NAME ON organizational_unit (name, parent_id)');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(Schema $schema): void
|
||||||
|
{
|
||||||
|
// this down() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->addSql('DROP INDEX UNIQ_IDENTIFIER_NAME ON organizational_unit');
|
||||||
|
$this->addSql('CREATE UNIQUE INDEX UNIQ_IDENTIFIER_NAME ON organizational_unit (name)');
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,10 +8,12 @@ use App\Entity\OrganizationalUnit;
|
||||||
use App\Validator\Constraints\OrganizationalUnitMulticastMode;
|
use App\Validator\Constraints\OrganizationalUnitMulticastMode;
|
||||||
use App\Validator\Constraints\OrganizationalUnitMulticastPort;
|
use App\Validator\Constraints\OrganizationalUnitMulticastPort;
|
||||||
use App\Validator\Constraints\OrganizationalUnitP2PMode;
|
use App\Validator\Constraints\OrganizationalUnitP2PMode;
|
||||||
|
use App\Validator\Constraints\OrganizationalUnitParent;
|
||||||
use App\Validator\Constraints\OrganizationalUnitType;
|
use App\Validator\Constraints\OrganizationalUnitType;
|
||||||
use Symfony\Component\Serializer\Annotation\Groups;
|
use Symfony\Component\Serializer\Annotation\Groups;
|
||||||
use Symfony\Component\Validator\Constraints as Assert;
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
|
#[OrganizationalUnitParent]
|
||||||
class OrganizationalUnitInput
|
class OrganizationalUnitInput
|
||||||
{
|
{
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank]
|
||||||
|
|
|
@ -11,7 +11,7 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||||
|
|
||||||
#[Gedmo\Tree(type: 'materializedPath')]
|
#[Gedmo\Tree(type: 'materializedPath')]
|
||||||
#[ORM\Entity(repositoryClass: MaterializedPathRepository::class)]
|
#[ORM\Entity(repositoryClass: MaterializedPathRepository::class)]
|
||||||
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_NAME', fields: ['name', 'parent_id'])]
|
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_NAME', columns: ['name', 'parent_id'])]
|
||||||
#[UniqueEntity(fields: ['name', 'parent'], message: 'This organizational unit already exists.')]
|
#[UniqueEntity(fields: ['name', 'parent'], message: 'This organizational unit already exists.')]
|
||||||
class OrganizationalUnit extends AbstractEntity
|
class OrganizationalUnit extends AbstractEntity
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Validator\Constraints;
|
||||||
|
|
||||||
|
use App\Model\UserGroupPermissions;
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
|
||||||
|
#[\Attribute]
|
||||||
|
class OrganizationalUnitParent extends Constraint
|
||||||
|
{
|
||||||
|
public string $message;
|
||||||
|
|
||||||
|
public function __construct(mixed $options = null, ?array $groups = null, mixed $payload = null)
|
||||||
|
{
|
||||||
|
parent::__construct($options, $groups, $payload);
|
||||||
|
|
||||||
|
$this->message = 'Only the root organizational unit can not have a parent.';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTargets(): array|string
|
||||||
|
{
|
||||||
|
return parent::CLASS_CONSTRAINT;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Validator\Constraints;
|
||||||
|
|
||||||
|
use App\Dto\Input\OrganizationalUnitInput;
|
||||||
|
use App\Model\OrganizationalUnitP2PModes;
|
||||||
|
use App\Model\OrganizationalUnitTypes;
|
||||||
|
use App\Model\UserGroupPermissions;
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
|
|
||||||
|
class OrganizationalUnitParentValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
public function validate($value, Constraint $constraint): void
|
||||||
|
{
|
||||||
|
if (!$value instanceof OrganizationalUnitInput) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$value->parent && in_array($value->type, [OrganizationalUnitTypes::CLASSROOMS_GROUP, OrganizationalUnitTypes::CLASSROOM, OrganizationalUnitTypes::CLIENTS_GROUP])) {
|
||||||
|
$this->context->buildViolation($constraint->message)->addViolation();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue