diff --git a/config/api_platform/User.yaml b/config/api_platform/User.yaml new file mode 100644 index 0000000..e1dd16e --- /dev/null +++ b/config/api_platform/User.yaml @@ -0,0 +1,24 @@ +resources: + App\Entity\User: + normalization_context: + groups: ['user:read'] + denormalization_context: + + groups: ['user:write'] + operations: + ApiPlatform\Metadata\GetCollection: + filters: + - 'api_platform.filter.user.order' + - 'api_platform.filter.user.search' + ApiPlatform\Metadata\Get: ~ + ApiPlatform\Metadata\Put: ~ + ApiPlatform\Metadata\Patch: ~ + ApiPlatform\Metadata\Post: ~ + ApiPlatform\Metadata\Delete: ~ + +properties: + App\Entity\User: + id: + identifier: false + uuid: + identifier: true \ No newline at end of file diff --git a/config/api_platform/UserGroup.yaml b/config/api_platform/UserGroup.yaml new file mode 100644 index 0000000..bb6931c --- /dev/null +++ b/config/api_platform/UserGroup.yaml @@ -0,0 +1,23 @@ +resources: + App\Entity\UserGroup: + normalization_context: + groups: ['user-group:read'] + denormalization_context: + groups: ['user-group:write'] + operations: + ApiPlatform\Metadata\GetCollection: + filters: + - 'api_platform.filter.user_group.order' + - 'api_platform.filter.user_group.search' + ApiPlatform\Metadata\Get: ~ + ApiPlatform\Metadata\Put: ~ + ApiPlatform\Metadata\Patch: ~ + ApiPlatform\Metadata\Post: ~ + ApiPlatform\Metadata\Delete: ~ + +properties: + App\Entity\UserGroup: + id: + identifier: false + uuid: + identifier: true \ No newline at end of file diff --git a/migrations/Version20240523091838.php b/migrations/Version20240523091838.php new file mode 100644 index 0000000..6f955c4 --- /dev/null +++ b/migrations/Version20240523091838.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE user ADD uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', ADD migration_id VARCHAR(255) DEFAULT NULL'); + $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649D17F50A6 ON user (uuid)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP INDEX UNIQ_8D93D649D17F50A6 ON user'); + $this->addSql('ALTER TABLE user DROP uuid, DROP migration_id'); + } +} diff --git a/migrations/Version20240523093000.php b/migrations/Version20240523093000.php new file mode 100644 index 0000000..16f1851 --- /dev/null +++ b/migrations/Version20240523093000.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE user ADD created_at DATETIME NOT NULL, ADD updated_at DATETIME NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE user DROP created_at, DROP updated_at'); + } +} diff --git a/migrations/Version20240523093036.php b/migrations/Version20240523093036.php new file mode 100644 index 0000000..5d736be --- /dev/null +++ b/migrations/Version20240523093036.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE user ADD created_at DATETIME NOT NULL, ADD updated_at DATETIME NOT NULL, ADD created_by VARCHAR(255) DEFAULT NULL, ADD updated_by VARCHAR(255) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE user DROP created_at, DROP updated_at, DROP created_by, DROP updated_by'); + } +} diff --git a/migrations/Version20240523094836.php b/migrations/Version20240523094836.php new file mode 100644 index 0000000..478930d --- /dev/null +++ b/migrations/Version20240523094836.php @@ -0,0 +1,37 @@ +addSql('CREATE TABLE user_group (id INT AUTO_INCREMENT NOT NULL, uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', migration_id VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, created_by VARCHAR(255) DEFAULT NULL, updated_by VARCHAR(255) DEFAULT NULL, name VARCHAR(255) NOT NULL, roles JSON NOT NULL COMMENT \'(DC2Type:json)\', UNIQUE INDEX UNIQ_8F02BF9DD17F50A6 (uuid), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE user_group_user (user_group_id INT NOT NULL, user_id INT NOT NULL, INDEX IDX_3AE4BD51ED93D47 (user_group_id), INDEX IDX_3AE4BD5A76ED395 (user_id), PRIMARY KEY(user_group_id, user_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE user_group_user ADD CONSTRAINT FK_3AE4BD51ED93D47 FOREIGN KEY (user_group_id) REFERENCES user_group (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE user_group_user ADD CONSTRAINT FK_3AE4BD5A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE user_group_user DROP FOREIGN KEY FK_3AE4BD51ED93D47'); + $this->addSql('ALTER TABLE user_group_user DROP FOREIGN KEY FK_3AE4BD5A76ED395'); + $this->addSql('DROP TABLE user_group'); + $this->addSql('DROP TABLE user_group_user'); + } +} diff --git a/src/Entity/UserGroup.php b/src/Entity/UserGroup.php new file mode 100644 index 0000000..2962b4b --- /dev/null +++ b/src/Entity/UserGroup.php @@ -0,0 +1,80 @@ + + */ + #[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'userGroups')] + private Collection $users; + + public function __construct() + { + parent::__construct(); + + $this->users = new ArrayCollection(); + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): static + { + $this->name = $name; + + return $this; + } + + public function getRoles(): array + { + return $this->roles; + } + + public function setRoles(array $roles): static + { + $this->roles = $roles; + + return $this; + } + + /** + * @return Collection + */ + public function getUsers(): Collection + { + return $this->users; + } + + public function addUser(User $user): static + { + if (!$this->users->contains($user)) { + $this->users->add($user); + } + + return $this; + } + + public function removeUser(User $user): static + { + $this->users->removeElement($user); + + return $this; + } +} diff --git a/src/Repository/UserGroupRepository.php b/src/Repository/UserGroupRepository.php new file mode 100644 index 0000000..33ef735 --- /dev/null +++ b/src/Repository/UserGroupRepository.php @@ -0,0 +1,43 @@ + + */ +class UserGroupRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, UserGroup::class); + } + + // /** + // * @return UserGroup[] Returns an array of UserGroup objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('u') + // ->andWhere('u.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('u.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?UserGroup + // { + // return $this->createQueryBuilder('u') + // ->andWhere('u.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +}