Skip to content

Commit

Permalink
Merge pull request #339 from dotkernel/issue-325
Browse files Browse the repository at this point in the history
Issue #325: Implemented enums in database
  • Loading branch information
arhimede authored Oct 31, 2024
2 parents 5c998ce + 3d43912 commit 4184881
Show file tree
Hide file tree
Showing 33 changed files with 311 additions and 134 deletions.
12 changes: 9 additions & 3 deletions config/autoload/doctrine.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

declare(strict_types=1);

use Api\Admin\DBAL\Types\AdminStatusEnumType;
use Api\App\Entity\EntityListenerResolver;
use Api\User\DBAL\Types\UserResetPasswordStatusEnumType;
use Api\User\DBAL\Types\UserStatusEnumType;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Dot\Cache\Adapter\ArrayAdapter;
use Dot\Cache\Adapter\FilesystemAdapter;
Expand All @@ -27,9 +30,12 @@
],
],
'types' => [
UuidType::NAME => UuidType::class,
UuidBinaryType::NAME => UuidBinaryType::class,
UuidBinaryOrderedTimeType::NAME => UuidBinaryOrderedTimeType::class,
UuidType::NAME => UuidType::class,
UuidBinaryType::NAME => UuidBinaryType::class,
UuidBinaryOrderedTimeType::NAME => UuidBinaryOrderedTimeType::class,
AdminStatusEnumType::NAME => AdminStatusEnumType::class,
UserStatusEnumType::NAME => UserStatusEnumType::class,
UserResetPasswordStatusEnumType::NAME => UserResetPasswordStatusEnumType::class,
],
'fixtures' => getcwd() . '/data/doctrine/fixtures',
'configuration' => [
Expand Down
14 changes: 6 additions & 8 deletions data/doctrine/fixtures/AdminLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,35 @@

use Api\Admin\Entity\Admin;
use Api\Admin\Entity\AdminRole;
use Api\User\Entity\User;
use Api\Admin\Enum\AdminStatusEnum;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;

/**
* Class AdminLoader
* @package Api\Fixtures
*/
use function assert;

class AdminLoader implements FixtureInterface, DependentFixtureInterface
{
public function load(ObjectManager $manager): void
{
$adminRoleRepository = $manager->getRepository(AdminRole::class);

/** @var AdminRole $adminRole */
$adminRole = $adminRoleRepository->findOneBy([
'name' => AdminRole::ROLE_ADMIN,
]);
assert($adminRole instanceof AdminRole);

/** @var AdminRole $superUserRole */
$superUserRole = $adminRoleRepository->findOneBy([
'name' => AdminRole::ROLE_SUPERUSER,
]);
assert($superUserRole instanceof AdminRole);

$admin = (new Admin())
->setIdentity('admin')
->usePassword('dotkernel')
->setFirstName('DotKernel')
->setLastName('Admin')
->setStatus(User::STATUS_ACTIVE)
->setStatus(AdminStatusEnum::Active)
->addRole($adminRole)
->addRole($superUserRole);

Expand Down
4 changes: 0 additions & 4 deletions data/doctrine/fixtures/AdminRoleLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;

/**
* Class AdminRoleLoader
* @package Api\Fixtures
*/
class AdminRoleLoader implements FixtureInterface
{
public function load(ObjectManager $manager): void
Expand Down
4 changes: 0 additions & 4 deletions data/doctrine/fixtures/OAuthClientLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;

/**
* Class OAuthClientLoader
* @package Api\Fixtures
*/
class OAuthClientLoader extends AbstractFixture implements FixtureInterface
{
public function load(ObjectManager $manager): void
Expand Down
4 changes: 0 additions & 4 deletions data/doctrine/fixtures/OAuthScopeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;

/**
* Class OAuthScopeLoader
* @package Api\Fixtures
*/
class OAuthScopeLoader implements FixtureInterface
{
public function load(ObjectManager $manager): void
Expand Down
13 changes: 6 additions & 7 deletions data/doctrine/fixtures/UserLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,33 @@
use Api\User\Entity\User;
use Api\User\Entity\UserDetail;
use Api\User\Entity\UserRole;
use Api\User\Enum\UserStatusEnum;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;

/**
* Class UserLoader
* @package Api\Fixtures
*/
use function assert;

class UserLoader implements FixtureInterface, DependentFixtureInterface
{
public function load(ObjectManager $manager): void
{
$userRoleRepository = $manager->getRepository(UserRole::class);

/** @var UserRole $guestRole */
$guestRole = $userRoleRepository->findOneBy([
'name' => UserRole::ROLE_GUEST,
]);
assert($guestRole instanceof UserRole);

/** @var UserRole $userRole */
$userRole = $userRoleRepository->findOneBy([
'name' => UserRole::ROLE_USER,
]);
assert($userRole instanceof UserRole);

$user = (new User())
->setIdentity('test@dotkernel.com')
->usePassword('dotkernel')
->setStatus(User::STATUS_ACTIVE)
->setStatus(UserStatusEnum::Active)
->setIsDeleted(false)
->setHash(User::generateHash())
->addRole($guestRole)
Expand Down
4 changes: 0 additions & 4 deletions data/doctrine/fixtures/UserRoleLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;

/**
* Class UserRoleLoader
* @package Api\Fixtures
*/
class UserRoleLoader implements FixtureInterface
{
public function load(ObjectManager $manager): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240613153602 extends AbstractMigration
final class Version20241030082958 extends AbstractMigration
{
public function getDescription(): string
{
Expand All @@ -20,7 +20,7 @@ public function getDescription(): string
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE admin (uuid BINARY(16) NOT NULL, identity VARCHAR(100) NOT NULL, firstName VARCHAR(191) NOT NULL, lastName VARCHAR(191) NOT NULL, password VARCHAR(100) NOT NULL, status VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_880E0D766A95E9C4 (identity), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE admin (uuid BINARY(16) NOT NULL, identity VARCHAR(100) NOT NULL, firstName VARCHAR(191) NOT NULL, lastName VARCHAR(191) NOT NULL, password VARCHAR(100) NOT NULL, status ENUM(\'active\', \'inactive\') DEFAULT \'active\' NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_880E0D766A95E9C4 (identity), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE admin_roles (userUuid BINARY(16) NOT NULL, roleUuid BINARY(16) NOT NULL, INDEX IDX_1614D53DD73087E9 (userUuid), INDEX IDX_1614D53D88446210 (roleUuid), PRIMARY KEY(userUuid, roleUuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE admin_role (uuid BINARY(16) NOT NULL, name VARCHAR(30) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_7770088A5E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE oauth_access_tokens (id INT UNSIGNED AUTO_INCREMENT NOT NULL, user_id VARCHAR(25) DEFAULT NULL, token VARCHAR(100) NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expires_at DATETIME NOT NULL, client_id INT UNSIGNED DEFAULT NULL, INDEX IDX_CA42527C19EB6921 (client_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
Expand All @@ -30,11 +30,11 @@ public function up(Schema $schema): void
$this->addSql('CREATE TABLE oauth_clients (id INT UNSIGNED AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, secret VARCHAR(100) DEFAULT NULL, redirect VARCHAR(191) NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, isConfidential TINYINT(1) DEFAULT 0 NOT NULL, user_id BINARY(16) DEFAULT NULL, INDEX IDX_13CE8101A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE oauth_refresh_tokens (id INT UNSIGNED AUTO_INCREMENT NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expires_at DATETIME NOT NULL, access_token_id INT UNSIGNED DEFAULT NULL, INDEX IDX_5AB6872CCB2688 (access_token_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE oauth_scopes (id INT UNSIGNED AUTO_INCREMENT NOT NULL, scope VARCHAR(191) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user (uuid BINARY(16) NOT NULL, identity VARCHAR(191) NOT NULL, password VARCHAR(191) NOT NULL, status VARCHAR(20) NOT NULL, isDeleted TINYINT(1) NOT NULL, hash VARCHAR(64) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D6496A95E9C4 (identity), UNIQUE INDEX UNIQ_8D93D649D1B862B8 (hash), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user (uuid BINARY(16) NOT NULL, identity VARCHAR(191) NOT NULL, password VARCHAR(191) NOT NULL, status ENUM(\'active\', \'pending\') DEFAULT \'pending\' NOT NULL, isDeleted TINYINT(1) NOT NULL, hash VARCHAR(64) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D6496A95E9C4 (identity), UNIQUE INDEX UNIQ_8D93D649D1B862B8 (hash), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_roles (userUuid BINARY(16) NOT NULL, roleUuid BINARY(16) NOT NULL, INDEX IDX_54FCD59FD73087E9 (userUuid), INDEX IDX_54FCD59F88446210 (roleUuid), PRIMARY KEY(userUuid, roleUuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_avatar (uuid BINARY(16) NOT NULL, name VARCHAR(191) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_73256912D73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_detail (uuid BINARY(16) NOT NULL, firstName VARCHAR(191) DEFAULT NULL, lastName VARCHAR(191) DEFAULT NULL, email VARCHAR(191) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_4B5464AED73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_reset_password (uuid BINARY(16) NOT NULL, expires DATETIME NOT NULL, hash VARCHAR(64) NOT NULL, status VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_D21DE3BCD1B862B8 (hash), INDEX IDX_D21DE3BCD73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_reset_password (uuid BINARY(16) NOT NULL, expires DATETIME NOT NULL, hash VARCHAR(64) NOT NULL, status ENUM(\'completed\', \'requested\') DEFAULT \'requested\' NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_D21DE3BCD1B862B8 (hash), INDEX IDX_D21DE3BCD73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE user_role (uuid BINARY(16) NOT NULL, name VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_2DE8C6A35E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('ALTER TABLE admin_roles ADD CONSTRAINT FK_1614D53DD73087E9 FOREIGN KEY (userUuid) REFERENCES admin (uuid)');
$this->addSql('ALTER TABLE admin_roles ADD CONSTRAINT FK_1614D53D88446210 FOREIGN KEY (roleUuid) REFERENCES admin_role (uuid)');
Expand Down
23 changes: 23 additions & 0 deletions src/Admin/src/DBAL/Types/AdminStatusEnumType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Api\Admin\DBAL\Types;

use Api\Admin\Enum\AdminStatusEnum;
use Api\App\DBAL\Types\AbstractEnumType;

class AdminStatusEnumType extends AbstractEnumType
{
public const NAME = 'admin_status_enum';

protected function getEnumClass(): string
{
return AdminStatusEnum::class;
}

public function getName(): string
{
return self::NAME;
}
}
22 changes: 8 additions & 14 deletions src/Admin/src/Entity/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Api\Admin\Entity;

use Api\Admin\Enum\AdminStatusEnum;
use Api\Admin\Repository\AdminRepository;
use Api\App\Entity\AbstractEntity;
use Api\App\Entity\PasswordTrait;
Expand All @@ -22,13 +23,6 @@ class Admin extends AbstractEntity implements UserEntityInterface
use PasswordTrait;
use TimestampsTrait;

public const STATUS_ACTIVE = 'active';
public const STATUS_INACTIVE = 'inactive';
public const STATUSES = [
self::STATUS_ACTIVE,
self::STATUS_INACTIVE,
];

#[ORM\Column(name: "identity", type: "string", length: 100, unique: true)]
protected string $identity = '';

Expand All @@ -41,8 +35,8 @@ class Admin extends AbstractEntity implements UserEntityInterface
#[ORM\Column(name: "password", type: "string", length: 100)]
protected string $password = '';

#[ORM\Column(name: "status", type: "string", length: 20)]
protected string $status = self::STATUS_ACTIVE;
#[ORM\Column(type: 'admin_status_enum', options: ['default' => AdminStatusEnum::Active])]
protected AdminStatusEnum $status = AdminStatusEnum::Active;

#[ORM\ManyToMany(targetEntity: AdminRole::class)]
#[ORM\JoinTable(name: "admin_roles")]
Expand Down Expand Up @@ -122,12 +116,12 @@ public function setPassword(string $password): self
return $this;
}

public function getStatus(): string
public function getStatus(): AdminStatusEnum
{
return $this->status;
}

public function setStatus(string $status): self
public function setStatus(AdminStatusEnum $status): self
{
$this->status = $status;

Expand Down Expand Up @@ -178,21 +172,21 @@ public function hasRoles(): bool

public function activate(): self
{
$this->status = self::STATUS_ACTIVE;
$this->status = AdminStatusEnum::Active;

return $this;
}

public function deactivate(): self
{
$this->status = self::STATUS_INACTIVE;
$this->status = AdminStatusEnum::Inactive;

return $this;
}

public function isActive(): bool
{
return $this->status === self::STATUS_ACTIVE;
return $this->status === AdminStatusEnum::Active;
}

public function getIdentifier(): string
Expand Down
11 changes: 11 additions & 0 deletions src/Admin/src/Enum/AdminStatusEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Api\Admin\Enum;

enum AdminStatusEnum: string
{
case Active = 'active';
case Inactive = 'inactive';
}
7 changes: 4 additions & 3 deletions src/Admin/src/InputFilter/Input/StatusInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Api\Admin\InputFilter\Input;

use Api\Admin\Entity\Admin;
use Api\Admin\Enum\AdminStatusEnum;
use Api\App\Message;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
Expand All @@ -23,11 +23,12 @@ public function __construct(?string $name = null, bool $isRequired = true)

$this->getFilterChain()
->attachByName(StringTrim::class)
->attachByName(StripTags::class);
->attachByName(StripTags::class)
->attach(fn($value) => $value === null ? AdminStatusEnum::Active : AdminStatusEnum::from($value));

$this->getValidatorChain()
->attachByName(InArray::class, [
'haystack' => Admin::STATUSES,
'haystack' => AdminStatusEnum::cases(),
'message' => sprintf(Message::INVALID_VALUE, 'status'),
], true);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Admin/src/OpenAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Api\Admin\Collection\AdminRoleCollection;
use Api\Admin\Entity\Admin;
use Api\Admin\Entity\AdminRole;
use Api\Admin\Enum\AdminStatusEnum;
use Api\Admin\Handler\AdminAccountHandler;
use Api\Admin\Handler\AdminHandler;
use Api\Admin\Handler\AdminRoleHandler;
Expand Down Expand Up @@ -223,7 +224,7 @@
new OA\Property(property: 'passwordConfirm', type: 'string'),
new OA\Property(property: 'firstName', type: 'string'),
new OA\Property(property: 'lastName', type: 'string'),
new OA\Property(property: 'status', type: 'string', default: Admin::STATUS_ACTIVE),
new OA\Property(property: 'status', type: 'string', default: AdminStatusEnum::Active),
new OA\Property(
property: 'roles',
type: 'array',
Expand Down Expand Up @@ -291,7 +292,7 @@
new OA\Property(property: 'passwordConfirm', type: 'string'),
new OA\Property(property: 'firstName', type: 'string'),
new OA\Property(property: 'lastName', type: 'string'),
new OA\Property(property: 'status', type: 'string', default: Admin::STATUS_ACTIVE),
new OA\Property(property: 'status', type: 'string', default: AdminStatusEnum::Active),
new OA\Property(
property: 'roles',
type: 'array',
Expand Down Expand Up @@ -440,7 +441,7 @@
new OA\Property(property: 'identity', type: 'string'),
new OA\Property(property: 'firstName', type: 'string'),
new OA\Property(property: 'lastName', type: 'string'),
new OA\Property(property: 'status', type: 'string', example: Admin::STATUS_ACTIVE),
new OA\Property(property: 'status', type: 'string', example: AdminStatusEnum::Active),
new OA\Property(
property: 'roles',
type: 'array',
Expand Down
3 changes: 2 additions & 1 deletion src/Admin/src/Service/AdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Api\Admin\Collection\AdminCollection;
use Api\Admin\Entity\Admin;
use Api\Admin\Enum\AdminStatusEnum;
use Api\Admin\Repository\AdminRepository;
use Api\App\Exception\BadRequestException;
use Api\App\Exception\ConflictException;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function createAdmin(array $data = []): Admin
->usePassword($data['password'])
->setFirstName($data['firstName'])
->setLastName($data['lastName'])
->setStatus($data['status'] ?? Admin::STATUS_ACTIVE);
->setStatus($data['status'] ?? AdminStatusEnum::Active);

foreach ($data['roles'] as $roleData) {
$admin->addRole(
Expand Down
Loading

0 comments on commit 4184881

Please sign in to comment.