Comments (8)
Hello Michael, did you have any progress on this? What kind of change do you think would need to be made? I could try to make a PR if you have some general direction.
from doctrineencryptbundle.
For anyone else who's interested, I had a solid look into this. All the events that doctrine fires are before the field is transformed from an object (or array) into a string. Therefore there's really no way this code can hook into the persistence process to encrypt the string after conversion. Thanks Doctrine 🙄
from doctrineencryptbundle.
Does this mean that there won't be a fix/workaround for this problem?
from doctrineencryptbundle.
I'd rather the maintainer respond to your question @McCrafterIV , but from where I sit, it doesn't seem possible. You could, as I have, roll your own custom doctrine type that does the same thing that the listener does. Call it 'encrypted_json' or some such
from doctrineencryptbundle.
Any update regarding this issue?
from doctrineencryptbundle.
If it helps you, I can confirm that making this change constitutes a significant deviation from this project's implementation. You might consider using this package instead? https://github.com/brighte-capital/doctrine-encrypted-json, though the README isn't really up-to-date
from doctrineencryptbundle.
Otherwise, you just have to give the type text
to your field (let's imagine foo
) and in the methods getFoo()
, implement the decoding of your json and in setFoo($foo)
, implement the encoding in json. No ? With json_decode()
and json_encode()
.
Or, instead of JSON, use the serialize
and unserialize
methods directly on the array in the getter & setter methods.
EDIT: Ah heck, I just took the time to test what I said, and since it's the getters and setters that are called for encryption and decryption the problem is the same.
So we have to implement serialization and unserialization in other methods if we want use the encryption.
from doctrineencryptbundle.
Here is an example of how to implement the encryption of an array (or others, DateTime for exemple, juste adapt to your needs) field in the database:
<?php
namespace App\Entity;
use Ambta\DoctrineEncryptBundle\Configuration\Encrypted;
use App\Repository\FooRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="app_foo")
* @ORM\Entity(repositoryClass=FooRepository::class)
*/
class Foo {
/**
* @var int|null $id
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="id", type="integer")
*
* @note `null` by default to avoid the type error before the persistence of the entity.
*/
private ?int $id = null;
/**
* @var string $array
*
* @ORM\Column(name="array", type="text", nullable=false)
* @Encrypted
*/
private string $array;
// -------------------- Magic method -------------------- \\
/**
* Foo constructor
*/
public function __construct(array $array = []) {
$this->array = serialize($array);
}
// -------------------- Public methods -------------------- \\
/**
* @return array
*/
public function getUnserializedArray(): array {
return unserialize($this->array);
}
/**
* @param array $array
* @return Foo
*/
public function setUserializedArray(array $array): self {
$this->setArray(serialize($array));
return $this;
}
/**
* @param array $add
* @return Foo
*/
public function addToArray(array $add): self {
$array = $this->getUnserializedarray();
$array[] = $add;
$this->setArray(serialize($array));
return $this;
}
/**
* @param int $key - An integer or a string depending on your table and your needs.
* @return Foo
*/
public function removeFromArray(int $key): self {
$array = $this->getUnserializedarray();
if (array_key_exists($key, $array)) {
unset($array[$key]);
$this->setArray(serialize($array));
}
return $this;
}
// -------------------- Getters / Setters -------------------- \\
/**
* @return int|null
*/
public function getId(): ?int {
return $this->id;
}
/**
* @return string
*/
public function getArray(): string {
return $this->array;
}
/**
* @param string $array
* @return Foo
*/
public function setArray(string $array): self {
$this->array = $array;
return $this;
}
}
Then document the class well to remind yourself or explain to other developers these specificities.
PS: To encrypt a date for example, use a string field (or text depending on the length of the hash), in this field, put the formatted date, for example: $date->format(DateTimeInterface::W3C);
and to retrieve the object: return new DateTime($this->date);
NOTE:
To do all this, we could also try to use text fields to save our data for encrypted/decrypted and use non-persisted fields with an ORM Subscriber on this entity to listen for events: prePersist
, preUpdate
, preFlush
, postLoad
or whatever you need.
See the Doctrine events: here
If you have problems with this, you can configure the priority to make sure your events are triggered before or after the encryption/decryption events. Have a look at the documentation: here.
from doctrineencryptbundle.
Related Issues (20)
- Issue with Custom Encryptor
- @UniqueEntity HOT 1
- Prod/Dev/Test Environment configs HOT 2
- Fix symfony 5.4 depreciation messages
- [Upstream] Halite v5 Released (Security Improvements) HOT 3
- Question about DoctrineEncryptSubscriber events
- Issue with doctrine:decrypt:database command HOT 3
- upgrading to 5.1 from 5.0.3 made the tests crash HOT 1
- Upgrading to 5.1 from 5.0.3 huge performance decrease when using annotations HOT 14
- Whats the state of this project? HOT 4
- Does this library support entities which mappings are defined using YAML? HOT 1
- V5.1.0 : problem in doctrine:decrypt:database command HOT 1
- Symfony 6 compatibility HOT 2
- Invalid message authentication code HOT 2
- Deprecation issues with symfony 6.3 HOT 1
- Support Symfony 7 HOT 1
- Disable encryption regarding to the environment
- ORM\Embedded attribute not working (formerly: attributes instead of annotations) HOT 1
- Package sensio/framework-extra-bundle is abandoned.
- Is this project still maintained? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from doctrineencryptbundle.