mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-23 10:18:56 +02:00
Make PermissionData an embeddable so doctrine can properly track changes to the data array
This commit is contained in:
parent
687ee80255
commit
59ddf91527
5 changed files with 53 additions and 47 deletions
33
migrations/Version20221031195841.php
Normal file
33
migrations/Version20221031195841.php
Normal 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');
|
||||
}
|
||||
}
|
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -97,13 +97,14 @@ class Group extends AbstractStructuralDBElement implements HasPermissionsInterfa
|
|||
|
||||
/**
|
||||
* @var PermissionData
|
||||
* @ORM\Column(type="permission_data", nullable=false, name="permissions")
|
||||
* @ValidPermission()
|
||||
* @ORM\Embedded(class="PermissionData", columnPrefix="permissions_")
|
||||
*/
|
||||
protected PermissionData $permissions;
|
||||
|
||||
|
||||
/** @var PermissionsEmbed
|
||||
* @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
|
||||
* @ValidPermission()
|
||||
*/
|
||||
protected $permissions_old;
|
||||
|
||||
|
@ -146,10 +147,6 @@ class Group extends AbstractStructuralDBElement implements HasPermissionsInterfa
|
|||
|
||||
public function getPermissions(): PermissionData
|
||||
{
|
||||
if (!isset($this->permissions)) {
|
||||
$this->permissions = new PermissionData();
|
||||
}
|
||||
|
||||
return $this->permissions;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,14 @@
|
|||
|
||||
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
|
||||
{
|
||||
/**
|
||||
|
@ -17,8 +25,9 @@ final class PermissionData implements \JsonSerializable
|
|||
* permission => [
|
||||
* operation => value,
|
||||
* ]
|
||||
* @ORM\Column(type="json", name="data")
|
||||
*/
|
||||
protected array $data = [];
|
||||
protected ?array $data = [];
|
||||
|
||||
/**
|
||||
* Creates a new Permission Data Instance using the given data.
|
||||
|
@ -91,6 +100,11 @@ final class PermissionData implements \JsonSerializable
|
|||
return new self($data);
|
||||
}
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$this->data = $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an JSON encodable representation of this object.
|
||||
* @return array|mixed
|
||||
|
|
|
@ -267,13 +267,13 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
|
|||
|
||||
/**
|
||||
* @var PermissionData
|
||||
* @ORM\Column(type="permission_data", nullable=false, name="permissions")
|
||||
* @ValidPermission()
|
||||
* @ORM\Embedded(class="PermissionData", columnPrefix="permissions_")
|
||||
*/
|
||||
protected PermissionData $permissions;
|
||||
|
||||
/** @var PermissionsEmbed
|
||||
* @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
|
||||
* @ValidPermission()
|
||||
*/
|
||||
protected $permissions_old;
|
||||
|
||||
|
@ -435,10 +435,6 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
|
|||
|
||||
public function getPermissions(): PermissionData
|
||||
{
|
||||
if (!isset($this->permissions)) {
|
||||
$this->permissions = new PermissionData();
|
||||
}
|
||||
|
||||
return $this->permissions;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue