Make PermissionData an embeddable so doctrine can properly track changes to the data array

This commit is contained in:
Jan Böhmer 2022-10-31 21:12:01 +01:00
parent 687ee80255
commit 59ddf91527
5 changed files with 53 additions and 47 deletions

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 Version20221031195841 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 groups ADD permissions_data LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\'');
$this->addSql('ALTER TABLE users ADD permissions_data LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\'');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE `groups` DROP permissions_data');
$this->addSql('ALTER TABLE `users` DROP permissions_data');
}
}

View file

@ -1,34 +0,0 @@
<?php
namespace App\Doctrine\Types;
use App\Entity\UserSystem\PermissionData;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\JsonType;
use JsonException;
class PermissionDataType extends JsonType
{
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null || $value === '') {
return null;
}
if (is_resource($value)) {
$value = stream_get_contents($value);
}
try {
return PermissionData::fromJSON($value);
} catch (JsonException $e) {
throw ConversionException::conversionFailed($value, $this->getName(), $e);
}
}
public function getName(): string
{
return 'permission_data';
}
}

View file

@ -97,13 +97,14 @@ class Group extends AbstractStructuralDBElement implements HasPermissionsInterfa
/** /**
* @var PermissionData * @var PermissionData
* @ORM\Column(type="permission_data", nullable=false, name="permissions") * @ValidPermission()
* @ORM\Embedded(class="PermissionData", columnPrefix="permissions_")
*/ */
protected PermissionData $permissions; protected PermissionData $permissions;
/** @var PermissionsEmbed /** @var PermissionsEmbed
* @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_") * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
* @ValidPermission()
*/ */
protected $permissions_old; protected $permissions_old;
@ -146,10 +147,6 @@ class Group extends AbstractStructuralDBElement implements HasPermissionsInterfa
public function getPermissions(): PermissionData public function getPermissions(): PermissionData
{ {
if (!isset($this->permissions)) {
$this->permissions = new PermissionData();
}
return $this->permissions; return $this->permissions;
} }

View file

@ -2,6 +2,14 @@
namespace App\Entity\UserSystem; namespace App\Entity\UserSystem;
use Doctrine\ORM\Mapping as ORM;
/**
* This class is used to store the permissions of a user.
* This has to be an embeddable or otherwise doctrine could not track the changes of the underlying data array (which is serialized to JSON in the database)
*
* @ORM\Embeddable()
*/
final class PermissionData implements \JsonSerializable final class PermissionData implements \JsonSerializable
{ {
/** /**
@ -17,8 +25,9 @@ final class PermissionData implements \JsonSerializable
* permission => [ * permission => [
* operation => value, * operation => value,
* ] * ]
* @ORM\Column(type="json", name="data")
*/ */
protected array $data = []; protected ?array $data = [];
/** /**
* Creates a new Permission Data Instance using the given data. * Creates a new Permission Data Instance using the given data.
@ -91,6 +100,11 @@ final class PermissionData implements \JsonSerializable
return new self($data); return new self($data);
} }
public function __clone()
{
$this->data = $this->data;
}
/** /**
* Returns an JSON encodable representation of this object. * Returns an JSON encodable representation of this object.
* @return array|mixed * @return array|mixed

View file

@ -267,13 +267,13 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
/** /**
* @var PermissionData * @var PermissionData
* @ORM\Column(type="permission_data", nullable=false, name="permissions") * @ValidPermission()
* @ORM\Embedded(class="PermissionData", columnPrefix="permissions_")
*/ */
protected PermissionData $permissions; protected PermissionData $permissions;
/** @var PermissionsEmbed /** @var PermissionsEmbed
* @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_") * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
* @ValidPermission()
*/ */
protected $permissions_old; protected $permissions_old;
@ -435,10 +435,6 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
public function getPermissions(): PermissionData public function getPermissions(): PermissionData
{ {
if (!isset($this->permissions)) {
$this->permissions = new PermissionData();
}
return $this->permissions; return $this->permissions;
} }