refs #377. Test UserGroups

pull/5/head
Manuel Aranda Rosales 2024-05-27 15:55:41 +02:00
parent e3664f4fd0
commit 5b4248bfda
18 changed files with 538 additions and 49 deletions

View File

@ -13,6 +13,7 @@ resources:
filters:
- 'api_platform.filter.user.order'
- 'api_platform.filter.user.search'
- 'api_platform.filter.user.boolean'
ApiPlatform\Metadata\Get:
provider: App\State\Provider\UserProvider
ApiPlatform\Metadata\Put:

View File

@ -1,17 +1,25 @@
resources:
App\Entity\UserGroup:
processor: App\State\Processor\UserGroupProcessor
input: App\Dto\Input\UserGroupInput
output: App\Dto\Output\UserGroupOutput
normalization_context:
groups: ['default', 'user-group:read']
denormalization_context:
groups: ['user-group:write']
operations:
ApiPlatform\Metadata\GetCollection:
provider: App\State\Provider\UserGroupProvider
filters:
- 'api_platform.filter.user_group.order'
- 'api_platform.filter.user_group.search'
ApiPlatform\Metadata\Get: ~
ApiPlatform\Metadata\Put: ~
ApiPlatform\Metadata\Patch: ~
- 'api_platform.filter.user_group.boolean'
ApiPlatform\Metadata\Get:
provider: App\State\Provider\UserGroupProvider
ApiPlatform\Metadata\Put:
provider: App\State\Provider\UserGroupProvider
ApiPlatform\Metadata\Patch:
provider: App\State\Provider\UserGroupProvider
ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~

View File

@ -29,3 +29,9 @@ services:
bind:
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'
App\State\Provider\UserGroupProvider:
bind:
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'

View File

@ -13,6 +13,12 @@ services:
tags:
- ['api_platform.filter' ]
api_platform.filter.user.boolean:
parent: 'api_platform.doctrine.orm.boolean_filter'
arguments: [ { 'enabled': ~ } ]
tags:
- [ 'api_platform.filter' ]
api_platform.filter.user_group.order:
parent: 'api_platform.doctrine.orm.order_filter'
arguments:
@ -26,3 +32,9 @@ services:
arguments: [ { 'id': 'exact', 'name': 'partial' } ]
tags:
- [ 'api_platform.filter' ]
api_platform.filter.user_group.boolean:
parent: 'api_platform.doctrine.orm.boolean_filter'
arguments: [ { 'enabled': ~ } ]
tags:
- [ 'api_platform.filter' ]

View File

@ -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 Version20240527103936 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('ALTER TABLE user_group CHANGE roles permissions JSON NOT NULL COMMENT \'(DC2Type:json)\'');
$this->addSql('CREATE UNIQUE INDEX UNIQ_IDENTIFIER_NAME ON user_group (name)');
}
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 user_group');
$this->addSql('ALTER TABLE user_group CHANGE permissions roles JSON NOT NULL COMMENT \'(DC2Type:json)\'');
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace App\Command;
use App\Entity\UserGroup;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class LoadDefaultUserGroupsCommand extends Command
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
)
{
parent::__construct('app:load-default-user-groups');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$userGroups = [
[
'name' => 'Super Admin',
'permissions' => ['ROLE_SUPER_ADMIN'],
'enabled' => true
],
[
'name' => 'Administrador de aulas',
'permissions' => ['ROLE_ORGANIZATIONAL_UNIT_ADMIN'],
'enabled' => true
],
[
'name' => 'Operador de aulas',
'permissions' => ['ROLE_ORGANIZATIONAL_UNIT_OPERATOR'],
'enabled' => true
],
[
'name' => 'Usuario',
'permissions' => ['ROLE_USER'],
'enabled' => true
],
];
foreach ($userGroups as $userGroup) {
$entity = new UserGroup();
$entity->setName($userGroup['name']);
$entity->setPermissions($userGroup['permissions']);
$entity->setEnabled($userGroup['enabled']);
$this->entityManager->persist($entity);
}
$this->entityManager->flush();
return 1;
}
}

View File

@ -5,13 +5,36 @@ namespace App\DataFixtures;
use App\Factory\UserFactory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpKernel\KernelInterface;
class AppFixtures extends Fixture
{
CONST ADMIN_USER = 'ogadmin';
public function __construct(
private readonly KernelInterface $kernel,
)
{
}
/**
* @throws \Exception
*/
public function load(ObjectManager $manager): void
{
UserFactory::createOne(['username' => self::ADMIN_USER]);
$application = new Application($this->kernel);
$input = new ArrayInput([
'command' => 'app:load-default-user-groups'
]);
$output = new BufferedOutput();
$application->run($input, $output);
}
}

View File

@ -13,20 +13,33 @@ final class UserGroupInput
public ?string $name = null;
#[Groups(['user-group:write'])]
public array $roles = [];
public array $permissions = [];
#[Assert\NotNull]
#[Groups(['user-group:write'])]
public ?bool $enabled = false;
public function __construct(?UserGroup $userGroup = null)
public function __construct(?UserGroup $userGroupGroup = null)
{
if (!$userGroup) {
if (!$userGroupGroup) {
return;
}
$this->name = $userGroup->getName();
$this->roles = $userGroup->getRoles();
$this->enabled= $userGroup->isEnabled();
$this->name = $userGroupGroup->getName();
$this->permissions = $userGroupGroup->getPermissions();
$this->enabled= $userGroupGroup->isEnabled();
}
public function createOrUpdateEntity(?UserGroup $userGroup = null): UserGroup
{
if (!$userGroup) {
$userGroup = new UserGroup();
}
$userGroup->setName($this->name);
$userGroup->setPermissions($this->permissions);
$userGroup->setEnabled($this->enabled);
return $userGroup;
}
}

View File

@ -60,7 +60,11 @@ final class UserInput
$user->setUsername($this->username);
$user->setRoles($this->roles);
$user->setEnabled($this->enabled);
$user->setUserGroups($this->userGroups);
foreach ($this->userGroups as $userGroup) {
$userGroupsToAdd[] = $userGroup->getEntity();
}
$user->setUserGroups( $userGroupsToAdd ?? [] );
if ($this->password !== null) {
$user->setPassword($this->password);

View File

@ -17,9 +17,14 @@ abstract class AbstractOutput
#[Groups(['default'])]
public int $id;
public function __construct(AbstractEntity $entity)
public function __construct(private readonly AbstractEntity $entity)
{
$this->uuid = $entity->getUuid();
$this->id = $entity->getId();
}
public function getEntity(): AbstractEntity
{
return $this->entity;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Dto\Output;
use ApiPlatform\Metadata\Get;
use App\Entity\User;
use App\Entity\UserGroup;
use Symfony\Component\Serializer\Annotation\Groups;
#[Get(shortName: 'UserGroup')]
final class UserGroupOutput extends AbstractOutput
{
#[Groups(['user-group:read'])]
public string $name;
#[Groups(['user-group:read'])]
public array $permissions;
#[Groups(['user-group:read'])]
public bool $enabled;
#[Groups(['user-group:read'])]
public \DateTime $createAt;
#[Groups(['user-group:read'])]
public ?string $createBy = null;
public function __construct(UserGroup $userGroup)
{
parent::__construct($userGroup);
$this->name = $userGroup->getName();
$this->permissions = $userGroup->getPermissions();
$this->enabled = $userGroup->isEnabled();
$this->createAt = $userGroup->getCreatedAt();
$this->createBy = $userGroup->getCreatedBy();
}
}

View File

@ -7,8 +7,11 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
#[ORM\Entity(repositoryClass: UserGroupRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_NAME', fields: ['name'])]
#[UniqueEntity(fields: ['name'], message: 'There is already an role with this name')]
class UserGroup extends AbstractEntity
{
use ToggleableTrait;
@ -17,7 +20,7 @@ class UserGroup extends AbstractEntity
private ?string $name = null;
#[ORM\Column(type: Types::JSON)]
private array $roles = [];
private array $permissions = [];
/**
* @var Collection<int, User>
@ -44,14 +47,14 @@ class UserGroup extends AbstractEntity
return $this;
}
public function getRoles(): array
public function getPermissions(): array
{
return $this->roles;
return $this->permissions;
}
public function setRoles(array $roles): static
public function setPermissions(array $permissions): static
{
$this->roles = $roles;
$this->permissions = $permissions;
return $this;
}

View File

@ -0,0 +1,45 @@
<?php
namespace App\Factory;
use App\Entity\UserGroup;
use App\Repository\UserGroupRepository;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;
/**
* @extends ModelFactory<UserGroup>
*/
final class UserGroupFactory extends ModelFactory
{
public function __construct()
{
parent::__construct();
}
protected function getDefaults(): array
{
return [
'createdAt' => self::faker()->dateTime(),
'permissions' => [],
'updatedAt' => self::faker()->dateTime(),
];
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
{
return $this
// ->afterInstantiate(function(UserGroup $userGroup): void {})
;
}
protected static function getClass(): string
{
return UserGroup::class;
}
}

View File

@ -9,35 +9,10 @@ use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<UserGroup>
*/
class UserGroupRepository extends ServiceEntityRepository
class UserGroupRepository extends AbstractRepository
{
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()
// ;
// }
}

View File

@ -0,0 +1,69 @@
<?php
namespace App\State\Processor;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\Validator\ValidatorInterface;
use App\Dto\Input\UserGroupInput;
use App\Dto\Input\UserInput;
use App\Dto\Output\UserGroupOutput;
use App\Repository\UserGroupRepository;
class UserGroupProcessor implements ProcessorInterface
{
public function __construct(
private readonly UserGroupRepository $userGroupRepository,
private readonly ValidatorInterface $validator
)
{
}
/**
* @throws \Exception
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): UserGroupOutput|null
{
switch ($operation){
case $operation instanceof Post:
case $operation instanceof Put:
case $operation instanceof Patch:
return $this->processCreateOrUpdate($data, $operation, $uriVariables, $context);
case $operation instanceof Delete:
return $this->processDelete($data, $operation, $uriVariables, $context);
}
}
/**
* @throws \Exception
*/
private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): UserGroupOutput
{
if (!($data instanceof UserGroupInput)) {
throw new \Exception(sprintf('data is not instance of %s', UserGroupInput::class));
}
$entity = null;
if (isset($uriVariables['uuid'])) {
$entity = $this->userGroupRepository->findOneByUuid($uriVariables['uuid']);
}
$userGroup = $data->createOrUpdateEntity($entity);
$this->validator->validate($userGroup);
$this->userGroupRepository->save($userGroup);
return new UserGroupOutput($userGroup);
}
private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null
{
$user = $this->userGroupRepository->findOneByUuid($uriVariables['uuid']);
$this->userGroupRepository->delete($user);
return null;
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace App\State\Provider;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\Pagination\TraversablePaginator;
use ApiPlatform\State\ProviderInterface;
use App\Dto\Input\UserGroupInput;
use App\Dto\Output\UserGroupOutput;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class UserGroupProvider implements ProviderInterface
{
public function __construct(
private readonly ProviderInterface $collectionProvider,
private readonly ProviderInterface $itemProvider
)
{
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
switch ($operation){
case $operation instanceof GetCollection:
return $this->provideCollection($operation, $uriVariables, $context);
case $operation instanceof Patch:
case $operation instanceof Put:
return $this->provideInput($operation, $uriVariables, $context);
case $operation instanceof Get:
return $this->provideItem($operation, $uriVariables, $context);
}
}
private function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$paginator = $this->collectionProvider->provide($operation, $uriVariables, $context);
$items = new \ArrayObject();
foreach ($paginator->getIterator() as $item){
$items[] = new UserGroupOutput($item);
}
return new TraversablePaginator($items, $paginator->getCurrentPage(), $paginator->getItemsPerPage(), $paginator->getTotalItems());
}
public function provideItem(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
if (!$item) {
throw new NotFoundHttpException('User Group not found');
}
return new UserGroupOutput($item);
}
public function provideInput(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (isset($uriVariables['uuid'])) {
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
return $item !== null ? new UserGroupInput($item) : null;
}
return new UserGroupInput();
}
}

View File

@ -0,0 +1,125 @@
<?php
namespace Functional;
use App\Entity\User;
use App\Entity\UserGroup;
use App\Factory\UserFactory;
use App\Factory\UserGroupFactory;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class UserGroupTest extends AbstractTest
{
CONST USER_ADMIN = 'ogadmin';
CONST USER_GROUP_CREATE = 'test-user-group-create';
CONST USER_GROUP_UPDATE = 'test-user-group-update';
CONST USER_GROUP_DELETE = 'test-user-group-delete';
CONST ROLE_ORGANIZATIONAL_UNIT_ADMIN = 'ROLE_ORGANIZATIONAL_UNIT_ADMIN';
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testGetCollectionUserGroup(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN]);
UserGroupFactory::createOne(['name' => 'Super Admin', 'permissions' => ['ROLE_SUPER_ADMIN'], 'enabled' => true]);
UserGroupFactory::createOne(['name' => 'Administrador de aulas', 'permissions' => ['ROLE_ORGANIZATIONAL_UNIT_ADMIN'], 'enabled' => true]);
UserGroupFactory::createOne(['name' => 'Operador de aulas', 'permissions' => ['ROLE_ORGANIZATIONAL_UNIT_OPERATOR'], 'enabled' => true]);
UserGroupFactory::createOne(['name' => 'Usuario', 'permissions' => ['ROLE_USER'], 'enabled' => true]);
$this->createClientWithCredentials()->request('GET', '/api/user-groups');
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/api/contexts/UserGroup',
'@id' => '/api/user-groups',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 4,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testCreateUserGroup(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN]);
$this->createClientWithCredentials()->request('POST', '/api/user-groups',['json' => [
'name' => self::USER_GROUP_CREATE,
'enabled' => true,
]]);
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/api/contexts/UserGroupOutput',
'@type' => 'UserGroup',
'name' => self::USER_GROUP_CREATE,
'enabled' => true,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testUpdateUserGroup(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN]);
UserGroupFactory::createOne(['name' => self::USER_GROUP_UPDATE]);
$iri = $this->findIriBy(UserGroup::class, ['name' => self::USER_GROUP_UPDATE]);
$this->createClientWithCredentials()->request('PATCH', $iri, ['json' => [
'name' => self::USER_GROUP_UPDATE,
'enabled' => false
]]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@id' => $iri,
'name' => self::USER_GROUP_UPDATE,
'enabled' => false
]);
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function testDeleteUser(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN]);
UserGroupFactory::createOne(['name' => self::USER_GROUP_DELETE]);
$iri = $this->findIriBy(UserGroup::class, ['name' => self::USER_GROUP_DELETE]);
$this->createClientWithCredentials()->request('DELETE', $iri);
$this->assertResponseStatusCodeSame(204);
$this->assertNull(
static::getContainer()->get('doctrine')->getRepository(UserGroup::class)->findOneBy(['name' => self::USER_GROUP_DELETE])
);
}
}

View File

@ -14,9 +14,9 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class UserTest extends AbstractTest
{
CONST USER_ADMIN = 'ogadmin';
CONST USER_CREATE = 'test-create';
CONST USER_UPDATE = 'test-update';
CONST USER_DELETE = 'test-delete';
CONST USER_CREATE = 'test-user-create';
CONST USER_UPDATE = 'test-user-update';
CONST USER_DELETE = 'test-user-delete';
/**
* @throws RedirectionExceptionInterface
@ -25,7 +25,7 @@ class UserTest extends AbstractTest
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testGetCollection(): void
public function testGetCollectionUser(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN]);
UserFactory::createMany(10);
@ -48,7 +48,7 @@ class UserTest extends AbstractTest
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testCreate(): void
public function testCreateUser(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN]);
$this->createClientWithCredentials()->request('POST', '/api/users',['json' => [
@ -74,7 +74,7 @@ class UserTest extends AbstractTest
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testUpdateBook(): void
public function testUpdateUser(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN]);
UserFactory::createOne(['username' => self::USER_UPDATE]);
@ -99,7 +99,7 @@ class UserTest extends AbstractTest
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function testDeleteBook(): void
public function testDeleteUser(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN]);
UserFactory::createOne(['username' => self::USER_DELETE]);