Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented doctrine enums #339

Open
wants to merge 16 commits into
base: 6.0
Choose a base branch
from
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"Admin\\Admin\\": "src/Admin/src/",
"Admin\\Page\\": "src/Page/src/",
"Admin\\Dashboard\\": "src/Dashboard/src/",
"Admin\\Setting\\": "src/Setting/src/"
"Admin\\Setting\\": "src/Setting/src/",
"Admin\\Fixtures\\": "data/doctrine/fixtures"
}
},
"autoload-dev": {
Expand Down
2 changes: 1 addition & 1 deletion config/autoload/authentication.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'doctrine' => [
'authentication' => [
'orm_default' => [
'object_manager' => 'doctrine.entitymanager.orm_default',
'object_manager' => 'doctrine.entity_manager.orm_default',
'identity_class' => Admin::class,
'identity_property' => 'identity',
'credential_property' => 'password',
Expand Down
3 changes: 0 additions & 3 deletions config/cli-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,4 @@

$entityManager = $container->get(EntityManager::class);

// register enum type for doctrine
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

return DependencyFactory::fromEntityManager($config, new ExistingEntityManager($entityManager));
3 changes: 2 additions & 1 deletion data/doctrine/fixtures/AdminLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Admin\Admin\Entity\Admin;
use Admin\Admin\Entity\AdminRole;
use Admin\Admin\Enum\AdminRoleEnum;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;
Expand All @@ -24,7 +25,7 @@ public function load(ObjectManager $manager): void
->setFirstName('Dotkernel')
->setLastName('Admin')
->addRole(
$manager->getRepository(AdminRole::class)->findOneBy(['name' => AdminRole::ROLE_SUPERUSER])
$manager->getRepository(AdminRole::class)->findOneBy(['name' => AdminRoleEnum::Superuser])
);

$manager->persist($admin);
Expand Down
5 changes: 3 additions & 2 deletions data/doctrine/fixtures/AdminRoleLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Admin\Fixtures;

use Admin\Admin\Entity\AdminRole;
use Admin\Admin\Enum\AdminRoleEnum;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;

Expand All @@ -13,10 +14,10 @@ class AdminRoleLoader implements FixtureInterface
public function load(ObjectManager $manager): void
{
$manager->persist(
(new AdminRole())->setName(AdminRole::ROLE_SUPERUSER)
(new AdminRole())->setName(AdminRoleEnum::Superuser)
);
$manager->persist(
(new AdminRole())->setName(AdminRole::ROLE_ADMIN)
(new AdminRole())->setName(AdminRoleEnum::Admin)
);

$manager->flush();
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 Version20240627134952 extends AbstractMigration
final class Version20250310084727 extends AbstractMigration
{
public function getDescription(): string
{
Expand All @@ -20,11 +20,11 @@ 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(255) DEFAULT NULL, lastName VARCHAR(255) DEFAULT NULL, password VARCHAR(100) NOT NULL, status ENUM(\'pending\', \'active\'), 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(255) DEFAULT NULL, lastName VARCHAR(255) DEFAULT 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_login (uuid BINARY(16) NOT NULL, adminIp VARCHAR(50) DEFAULT NULL, country VARCHAR(50) DEFAULT NULL, continent VARCHAR(50) DEFAULT NULL, organization VARCHAR(50) DEFAULT NULL, deviceType VARCHAR(20) DEFAULT NULL, deviceBrand VARCHAR(20) DEFAULT NULL, deviceModel VARCHAR(40) DEFAULT NULL, isMobile ENUM(\'yes\', \'no\'), osName VARCHAR(20) DEFAULT NULL, osVersion VARCHAR(20) DEFAULT NULL, osPlatform VARCHAR(20) DEFAULT NULL, clientType VARCHAR(20) DEFAULT NULL, clientName VARCHAR(40) DEFAULT NULL, clientEngine VARCHAR(20) DEFAULT NULL, clientVersion VARCHAR(20) DEFAULT NULL, loginStatus ENUM(\'success\', \'fail\'), identity VARCHAR(100) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, PRIMARY KEY(uuid)) 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 settings (uuid BINARY(16) NOT NULL, identifier VARCHAR(50) NOT NULL, value LONGTEXT NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, admin_uuid BINARY(16) DEFAULT NULL, INDEX IDX_E545A0C5F166D246 (admin_uuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE admin_login (uuid BINARY(16) NOT NULL, adminIp VARCHAR(50) DEFAULT NULL, country VARCHAR(50) DEFAULT NULL, continent VARCHAR(50) DEFAULT NULL, organization VARCHAR(50) DEFAULT NULL, deviceType VARCHAR(20) DEFAULT NULL, deviceBrand VARCHAR(20) DEFAULT NULL, deviceModel VARCHAR(40) DEFAULT NULL, isMobile ENUM(\'yes\', \'no\') DEFAULT NULL, osName VARCHAR(20) DEFAULT NULL, osVersion VARCHAR(20) DEFAULT NULL, osPlatform VARCHAR(20) DEFAULT NULL, clientType VARCHAR(20) DEFAULT NULL, clientName VARCHAR(40) DEFAULT NULL, clientEngine VARCHAR(20) DEFAULT NULL, clientVersion VARCHAR(20) DEFAULT NULL, loginStatus ENUM(\'success\', \'fail\') DEFAULT NULL, identity VARCHAR(100) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE admin_role (uuid BINARY(16) NOT NULL, name ENUM(\'admin\', \'superuser\') DEFAULT \'admin\', created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE settings (uuid BINARY(16) NOT NULL, identifier ENUM(\'table_admin_list_selected_columns\', \'table_admin_list_logins_selected_columns\') NOT NULL, value LONGTEXT NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, admin_uuid BINARY(16) DEFAULT NULL, INDEX IDX_E545A0C5F166D246 (admin_uuid), 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)');
$this->addSql('ALTER TABLE settings ADD CONSTRAINT FK_E545A0C5F166D246 FOREIGN KEY (admin_uuid) REFERENCES admin (uuid)');
Expand Down
Binary file modified public/images/loading-sm.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 16 additions & 16 deletions public/js/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/table_settings.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Admin/src/Adapter/AuthenticationAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function authenticate(): Result
));
}

if ($identityClass->$methodName() !== $option['value']) {
if ($identityClass->$methodName()->value !== $option['value']) {
return new Result(
Result::FAILURE,
null,
Expand Down
10 changes: 10 additions & 0 deletions src/Admin/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace Admin\Admin;

use Admin\Admin\Adapter\AuthenticationAdapter;
use Admin\Admin\DBAL\Types\AdminRoleEnumType;
use Admin\Admin\DBAL\Types\AdminStatusEnumType;
use Admin\Admin\DBAL\Types\SuccessFailureEnumType;
use Admin\Admin\DBAL\Types\YesNoEnumType;
use Admin\Admin\Delegator\AdminRoleDelegator;
use Admin\Admin\Entity\Admin;
use Admin\Admin\Entity\AdminInterface;
Expand Down Expand Up @@ -136,6 +140,12 @@ public function getDoctrineConfig(): array
'paths' => [__DIR__ . '/Entity'],
],
],
'types' => [
AdminStatusEnumType::NAME => AdminStatusEnumType::class,
SuccessFailureEnumType::NAME => SuccessFailureEnumType::class,
YesNoEnumType::NAME => YesNoEnumType::class,
AdminRoleEnumType::NAME => AdminRoleEnumType::class,
],
];
}
}
23 changes: 23 additions & 0 deletions src/Admin/src/DBAL/Types/AdminRoleEnumType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Admin\Admin\DBAL\Types;

use Admin\Admin\Enum\AdminRoleEnum;
use Admin\App\DBAL\Types\AbstractEnumType;

class AdminRoleEnumType extends AbstractEnumType
{
public const NAME = 'admin_role_enum';

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

public function getName(): string
{
return self::NAME;
}
}
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 Admin\Admin\DBAL\Types;

use Admin\Admin\Enum\AdminStatusEnum;
use Admin\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;
}
}
23 changes: 23 additions & 0 deletions src/Admin/src/DBAL/Types/SuccessFailureEnumType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Admin\Admin\DBAL\Types;

use Admin\Admin\Enum\SuccessFailureEnum;
use Admin\App\DBAL\Types\AbstractEnumType;

class SuccessFailureEnumType extends AbstractEnumType
{
public const NAME = 'success_failure_enum';

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

public function getName(): string
{
return self::NAME;
}
}
23 changes: 23 additions & 0 deletions src/Admin/src/DBAL/Types/YesNoEnumType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Admin\Admin\DBAL\Types;

use Admin\Admin\Enum\YesNoEnum;
use Admin\App\DBAL\Types\AbstractEnumType;

class YesNoEnumType extends AbstractEnumType
{
public const NAME = 'yes_no_enum';

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

public function getName(): string
{
return self::NAME;
}
}
2 changes: 1 addition & 1 deletion src/Admin/src/Delegator/AdminRoleDelegator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __invoke(ContainerInterface $container, $name, callable $callbac
$roleService = $container->get(AdminRoleServiceInterface::class);
$adminForm->setRoles(array_map(function (AdminRole $role) {
return [
'label' => $role->getName(),
'label' => $role->getName()->value,
'value' => $role->getUuid()->toString(),
'selected' => false,
];
Expand Down
51 changes: 24 additions & 27 deletions src/Admin/src/Entity/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Admin\Admin\Entity;

use Admin\Admin\Enum\AdminStatusEnum;
use Admin\Admin\Repository\AdminRepository;
use Admin\App\Entity\AbstractEntity;
use Admin\App\Entity\TimestampsTrait;
Expand All @@ -16,48 +17,39 @@
use function password_verify;

#[ORM\Entity(repositoryClass: AdminRepository::class)]
#[ORM\Table(name: "admin")]
#[ORM\Table(name: 'admin')]
#[ORM\HasLifecycleCallbacks]
#[ORM\Cache(usage: "NONSTRICT_READ_WRITE")]
#[ORM\Cache(usage: 'NONSTRICT_READ_WRITE')]
class Admin extends AbstractEntity implements AdminInterface
{
use TimestampsTrait;

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

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

#[ORM\Column(name: "firstName", type: "string", length: 255, nullable: true)]
#[ORM\Column(name: 'firstName', type: 'string', length: 255, nullable: true)]
protected ?string $firstName = null;

#[ORM\Column(name: "lastName", type: "string", length: 255, nullable: true)]
#[ORM\Column(name: 'lastName', type: 'string', length: 255, nullable: true)]
protected ?string $lastName = null;

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

#[ORM\Column(
name: "status",
type: "string",
length: 20,
nullable: false,
columnDefinition: "ENUM('pending', 'active')"
type: 'admin_status_enum',
enumType: AdminStatusEnum::class,
options: ['default' => AdminStatusEnum::Active]
)]
protected string $status = self::STATUS_ACTIVE;
protected AdminStatusEnum $status = AdminStatusEnum::Active;

#[ORM\ManyToMany(targetEntity: AdminRole::class, fetch: "EAGER")]
#[ORM\JoinTable(name: "admin_roles")]
#[ORM\JoinColumn(name: "userUuid", referencedColumnName: "uuid")]
#[ORM\InverseJoinColumn(name: "roleUuid", referencedColumnName: "uuid")]
#[ORM\ManyToMany(targetEntity: AdminRole::class, fetch: 'EAGER')]
#[ORM\JoinTable(name: 'admin_roles')]
#[ORM\JoinColumn(name: 'userUuid', referencedColumnName: 'uuid')]
#[ORM\InverseJoinColumn(name: 'roleUuid', referencedColumnName: 'uuid')]
protected Collection $roles;

#[ORM\OneToMany(mappedBy: 'admin', targetEntity: Setting::class)]
#[ORM\OneToMany(targetEntity: Setting::class, mappedBy: 'admin')]
protected Collection $settings;

public function __construct()
Expand All @@ -75,7 +67,7 @@ public function getArrayCopy(): array
'identity' => $this->getIdentity(),
'firstName' => $this->getfirstName(),
'lastName' => $this->getlastName(),
'status' => $this->getStatus(),
'status' => $this->getStatus()->value,
'roles' => array_map(function (AdminRole $role) {
return $role->getArrayCopy();
}, $this->getRoles()),
Expand Down Expand Up @@ -137,12 +129,12 @@ public function verifyPassword(string $password): bool
return password_verify($password, $this->getPassword());
}

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 @@ -183,4 +175,9 @@ public function removeRole(AdminRole $role): AdminInterface

return $this;
}

public function isActive(): bool
{
return $this->getStatus() === AdminStatusEnum::Active;
}
}
5 changes: 3 additions & 2 deletions src/Admin/src/Entity/AdminIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace Admin\Admin\Entity;

use Admin\Admin\Enum\AdminStatusEnum;
use Mezzio\Authentication\UserInterface;

class AdminIdentity implements UserInterface
{
public function __construct(
protected string $uuid,
protected string $identity,
protected string $status,
protected AdminStatusEnum $status,
protected array $roles = [],
protected array $details = []
) {
Expand All @@ -27,7 +28,7 @@ public function getIdentity(): string
return $this->identity;
}

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