Skip to content

Commit

Permalink
Added encryptor service
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-marcel committed Aug 3, 2015
1 parent dddabaf commit 1850a0b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Command/DoctrineEncryptStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
1 change: 1 addition & 0 deletions DependencyInjection/DoctrineEncryptExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));

}

/**
Expand Down
3 changes: 3 additions & 0 deletions Encryptors/Rijndael128Encryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public function encrypt($data) {
public function decrypt($data) {

if(is_string($data)) {

$data = str_replace("<ENC>", "", $data);

return trim(mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$this->secretKey,
Expand Down
7 changes: 6 additions & 1 deletion Resources/config/orm-services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ services:
tags:
- { name: doctrine.event_subscriber }
ambta_doctrine_encrypt.subscriber:
alias: ambta_doctrine_encrypt.orm_subscriber
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%
6 changes: 3 additions & 3 deletions Resources/doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions Services/Encryptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* Copyright 2015 Soeezy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Ambta\DoctrineEncryptBundle\Services;

class Encryptor
{
/** @var \Ambta\DoctrineEncryptBundle\Encryptors\EncryptorInterface */
protected $encryptor;

public function __construct($encryptName, $key)
{

$reflectionClass = new \ReflectionClass($encryptName);
$this->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);
}
}
11 changes: 6 additions & 5 deletions Subscribers/DoctrineEncryptSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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
*/
Expand All @@ -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);
Expand All @@ -185,7 +186,7 @@ public function getSubscribedEvents() {
Events::preFlush
);
}

/**
* Process (encrypt/decrypt) entities fields
*
Expand Down

0 comments on commit 1850a0b

Please sign in to comment.