diff --git a/Command/DoctrineEncryptStatusCommand.php b/Command/DoctrineEncryptStatusCommand.php index c24d515c..4ee3ade7 100644 --- a/Command/DoctrineEncryptStatusCommand.php +++ b/Command/DoctrineEncryptStatusCommand.php @@ -57,6 +57,6 @@ protected function execute(InputInterface $input, OutputInterface $output) } $output->writeln(""); - $output->writeln(count($metaDataArray) . " entities found which are containing " . $totalCount . " encrypted properties."); + $output->writeln(count($metaDataArray) . " entity's found which are containing " . $totalCount . " encrypted properties."); } } diff --git a/DependencyInjection/DoctrineEncryptExtension.php b/DependencyInjection/DoctrineEncryptExtension.php index 30b717e9..884a467a 100644 --- a/DependencyInjection/DoctrineEncryptExtension.php +++ b/DependencyInjection/DoctrineEncryptExtension.php @@ -59,6 +59,7 @@ public function load(array $configs, ContainerBuilder $container) { //Load service file $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load(sprintf('%s.yml', $services['orm'])); + } /** diff --git a/Encryptors/Rijndael128Encryptor.php b/Encryptors/Rijndael128Encryptor.php index afca4fbb..34b3b7fd 100644 --- a/Encryptors/Rijndael128Encryptor.php +++ b/Encryptors/Rijndael128Encryptor.php @@ -56,6 +56,9 @@ public function encrypt($data) { public function decrypt($data) { if(is_string($data)) { + + $data = str_replace("", "", $data); + return trim(mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $this->secretKey, diff --git a/Resources/config/orm-services.yml b/Resources/config/orm-services.yml index 123375f6..427db034 100644 --- a/Resources/config/orm-services.yml +++ b/Resources/config/orm-services.yml @@ -5,4 +5,9 @@ services: tags: - { name: doctrine.event_subscriber } ambta_doctrine_encrypt.subscriber: - alias: ambta_doctrine_encrypt.orm_subscriber \ No newline at end of file + alias: ambta_doctrine_encrypt.orm_subscriber + ambta_doctrine_encrypt.encryptor: + class: Ambta\DoctrineEncryptBundle\Services\Encryptor + arguments: + - %ambta_doctrine_encrypt.encryptor_class_name% + - %ambta_doctrine_encrypt.secret_key% \ No newline at end of file diff --git a/Resources/doc/commands.md b/Resources/doc/commands.md index 6cf0ba52..2039e3ed 100644 --- a/Resources/doc/commands.md +++ b/Resources/doc/commands.md @@ -10,14 +10,14 @@ You can use the comment `doctrine:encrypt:status` to get the current database an $ php app/console doctrine:encrypt:status ``` -This command will return the amount of entities and the amount of properties with the @Encrypted tag for each entity. +This command will return the amount of entity's and the amount of properties with the @Encrypted tag for each entity. The result will look like this: ``` DoctrineEncrypt\Entity\User has 3 properties which are encrypted. DoctrineEncrypt\Entity\UserDetail has 13 properties which are encrypted. -2 entities found which are containing 16 encrypted properties. +2 entity's found which are containing 16 encrypted properties. ``` ## 2) Encrypt current database @@ -71,7 +71,7 @@ $ php app/console doctrine:encrypt:database rijndael256 $ php app/console doctrine:encrypt:database \Ambta\DoctrineEncryptBundle\Encryptors\Rijndael256Encryptor ``` -This command will return the amount of entities and the amount of values decrypted in the database. +This command will return the amount of entity's and the amount of values decrypted in the database. ``` Decryption finished entities found: 26, decrypted 195 values. diff --git a/Services/Encryptor.php b/Services/Encryptor.php new file mode 100644 index 00000000..db4ce748 --- /dev/null +++ b/Services/Encryptor.php @@ -0,0 +1,36 @@ +encryptor = $reflectionClass->newInstanceArgs( array( + $key + )); + } + + public function getEncryptor() { + return $this->encryptor; + } + + public function decrypt($string) { + return $this->encryptor->decrypt($string); + } + + public function encrypt($string) { + return $this->encryptor->encrypt($string); + } +} \ No newline at end of file diff --git a/Subscribers/DoctrineEncryptSubscriber.php b/Subscribers/DoctrineEncryptSubscriber.php index f2a4ff38..370cfc58 100644 --- a/Subscribers/DoctrineEncryptSubscriber.php +++ b/Subscribers/DoctrineEncryptSubscriber.php @@ -6,6 +6,7 @@ use Doctrine\Common\EventSubscriber; use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Event\PreUpdateEventArgs; +use Doctrine\ORM\Event\PreFlushEventArgs; use Doctrine\Common\Annotations\Reader; use Doctrine\Common\Util\ClassUtils; use \ReflectionClass; @@ -120,7 +121,7 @@ public function restoreEncryptor() { /** * Listen a postUpdate lifecycle event. - * Decrypt entities property's values when post updated. + * Decrypt entity's property's values when post updated. * * So for example after form submit the preUpdate encrypted the entity * We have to decrypt them before showing them again. @@ -136,7 +137,7 @@ public function postUpdate(LifecycleEventArgs $args) { /** * Listen a preUpdate lifecycle event. - * Encrypt entities property's values on preUpdate, so they will be stored encrypted + * Encrypt entity's property's values on preUpdate, so they will be stored encrypted * * @param PreUpdateEventArgs $args */ @@ -163,9 +164,9 @@ public function postLoad(LifecycleEventArgs $args) { * Listen to preflush event * Encrypt entities that are inserted into the database * - * @param \Doctrine\ORM\Event\PreFlushEventArgs $preFlushEventArgs + * @param PreFlushEventArgs $preFlushEventArgs */ - public function preFlush(\Doctrine\ORM\Event\PreFlushEventArgs $preFlushEventArgs) { + public function preFlush(PreFlushEventArgs $preFlushEventArgs) { $unitOfWork = $preFlushEventArgs->getEntityManager()->getUnitOfWork(); foreach($unitOfWork->getScheduledEntityInsertions() as $entity) { $this->processFields($entity); @@ -185,7 +186,7 @@ public function getSubscribedEvents() { Events::preFlush ); } - + /** * Process (encrypt/decrypt) entities fields *