Skip to content

Commit 9e3e440

Browse files
committed
Merge branch 'master' of github.com:nowo-tech/DoctrineEncryptBundle
2 parents 2924214 + 9baae3d commit 9e3e440

22 files changed

Lines changed: 124 additions & 104 deletions

demo/symfony7/config/bundles.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
return [
4-
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
5-
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
6-
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
7-
Nowo\DoctrineEncryptBundle\NowoDoctrineEncryptBundle::class => ['all' => true],
6+
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
7+
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
8+
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
9+
Nowo\DoctrineEncryptBundle\NowoDoctrineEncryptBundle::class => ['all' => true],
810
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
9-
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
10-
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
11-
Nowo\TwigInspectorBundle\NowoTwigInspectorBundle::class => ['dev' => true, 'test' => true],
11+
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
12+
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
13+
Nowo\TwigInspectorBundle\NowoTwigInspectorBundle::class => ['dev' => true, 'test' => true],
1214
];

demo/symfony7/config/reference.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
// This file is auto-generated and is for apps only. Bundles SHOULD NOT rely on its content.
46

57
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

demo/symfony7/src/Controller/MysqlAesNoteController.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\Routing\Attribute\Route;
1818

19+
use function in_array;
20+
1921
#[Route('/mysql-aes-note')]
2022
class MysqlAesNoteController extends AbstractController
2123
{
@@ -25,21 +27,21 @@ public function index(Request $request, MysqlAesNoteRepository $repository): Res
2527
$titleFilter = $request->query->getString('title');
2628
$secretFilter = $request->query->getString('secret');
2729
$secretMode = $request->query->getString('secret_mode', 'plaintext');
28-
if (!\in_array($secretMode, ['plaintext', 'ciphertext'], true)) {
30+
if (!in_array($secretMode, ['plaintext', 'ciphertext'], true)) {
2931
$secretMode = 'plaintext';
3032
}
3133

32-
$titleLike = MysqlAesNoteRepository::buildLikePattern($titleFilter !== '' ? $titleFilter : null);
33-
$secretNeedle = $secretFilter !== '' ? $secretFilter : null;
34-
$notes = $repository->findDoctrineListFiltered($titleLike, $secretNeedle, $secretMode);
34+
$titleLike = MysqlAesNoteRepository::buildLikePattern($titleFilter !== '' ? $titleFilter : null);
35+
$secretNeedle = $secretFilter !== '' ? $secretFilter : null;
36+
$notes = $repository->findDoctrineListFiltered($titleLike, $secretNeedle, $secretMode);
3537

3638
return $this->render('mysql_aes_note/index.html.twig', [
3739
'notes' => $notes,
3840
'nativeSupported' => $repository->supportsNativeMysqlAes(),
3941
'filter' => [
40-
'title' => $titleFilter,
41-
'secret' => $secretFilter,
42-
'secret_mode' => $secretMode,
42+
'title' => $titleFilter,
43+
'secret' => $secretFilter,
44+
'secret_mode' => $secretMode,
4345
],
4446
'queryDescription' => MysqlAesNoteRepository::describeDoctrineLikeFilter(
4547
$titleLike,
@@ -91,15 +93,15 @@ public function sqlIndex(Request $request, MysqlAesNoteRepository $repository):
9193
$titleFilter = $request->query->getString('title');
9294
$secretFilter = $request->query->getString('secret');
9395
$secretMode = $request->query->getString('secret_mode', 'decrypted');
94-
if (!\in_array($secretMode, ['decrypted', 'ciphertext'], true)) {
96+
if (!in_array($secretMode, ['decrypted', 'ciphertext'], true)) {
9597
$secretMode = 'decrypted';
9698
}
9799

98100
$titleLike = MysqlAesNoteRepository::buildLikePattern($titleFilter !== '' ? $titleFilter : null);
99101
$secretLike = MysqlAesNoteRepository::buildLikePattern($secretFilter !== '' ? $secretFilter : null);
100102

101103
return $this->render('mysql_aes_note/sql_index.html.twig', [
102-
'rows' => $repository->findDecryptedWithAesDecryptFiltered($titleLike, $secretLike, $secretMode),
104+
'rows' => $repository->findDecryptedWithAesDecryptFiltered($titleLike, $secretLike, $secretMode),
103105
'filter' => [
104106
'title' => $titleFilter,
105107
'secret' => $secretFilter,
@@ -131,7 +133,7 @@ public function dbValues(
131133
EncryptorRegistry $encryptorRegistry,
132134
): Response {
133135
$mode = $request->query->getString('mode', 'both');
134-
if (!\in_array($mode, ['raw', 'decrypted', 'both'], true)) {
136+
if (!in_array($mode, ['raw', 'decrypted', 'both'], true)) {
135137
$mode = 'both';
136138
}
137139

@@ -200,5 +202,4 @@ public function delete(Request $request, MysqlAesNote $note, EntityManagerInterf
200202

201203
return $this->redirectToRoute('mysql_aes_note_index');
202204
}
203-
204205
}

demo/symfony7/src/Controller/SecretMessageController.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\HttpFoundation\Response;
1515
use Symfony\Component\Routing\Attribute\Route;
1616

17+
use function in_array;
18+
1719
#[Route('/secret-message')]
1820
class SecretMessageController extends AbstractController
1921
{
@@ -41,18 +43,18 @@ public function rawIndex(): Response
4143
public function dbValues(Request $request, Connection $conn, EncryptUtil $encryptUtil): Response
4244
{
4345
$mode = $request->query->getString('mode', 'both');
44-
if (!\in_array($mode, ['raw', 'decrypted', 'both'], true)) {
46+
if (!in_array($mode, ['raw', 'decrypted', 'both'], true)) {
4547
$mode = 'both';
4648
}
4749

4850
$rows = [];
4951
foreach ($conn->fetchAllAssociative('SELECT id, title, message FROM secret_message ORDER BY id DESC') as $row) {
50-
$raw = isset($row['message']) && $row['message'] !== '' ? (string) $row['message'] : null;
52+
$raw = isset($row['message']) && $row['message'] !== '' ? (string) $row['message'] : null;
5153
$rows[] = [
52-
'id' => (int) $row['id'],
53-
'title' => $row['title'],
54-
'message_raw' => $raw,
55-
'message_decrypted' => $raw !== null ? $encryptUtil->decrypt($raw, 'personal_data') : null,
54+
'id' => (int) $row['id'],
55+
'title' => $row['title'],
56+
'message_raw' => $raw,
57+
'message_decrypted' => $raw !== null ? $encryptUtil->decrypt($raw, 'personal_data') : null,
5658
];
5759
}
5860

demo/symfony7/src/Entity/MysqlAesNote.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Doctrine\ORM\Mapping as ORM;
99
use Nowo\DoctrineEncryptBundle\Configuration\Encrypted;
1010

11+
use function is_resource;
12+
1113
/**
1214
* Demo entity for MysqlAes encryptor (ORM path) plus native AES_ENCRYPT column (repository path).
1315
*/
@@ -30,7 +32,7 @@ class MysqlAesNote
3032

3133
/** Filled only via repository SQL (AES_ENCRYPT); not mapped for ORM writes. */
3234
#[ORM\Column(type: 'blob', nullable: true)]
33-
private $secretNative = null;
35+
private $secretNative;
3436

3537
public function getId(): ?int
3638
{

demo/symfony7/src/Form/MysqlAesNativeType.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
1818
$builder
1919
->add('title', TextType::class, [
2020
'label' => 'Title',
21-
'help' => 'Stored in plain text.',
22-
'attr' => ['class' => 'form-control'],
21+
'help' => 'Stored in plain text.',
22+
'attr' => ['class' => 'form-control'],
2323
])
2424
->add('secret', TextareaType::class, [
25-
'label' => 'Secret',
26-
'help' => 'Inserted with <code>AES_ENCRYPT</code> into <code>secret_native</code> (BLOB).',
25+
'label' => 'Secret',
26+
'help' => 'Inserted with <code>AES_ENCRYPT</code> into <code>secret_native</code> (BLOB).',
2727
'help_html' => true,
28-
'attr' => ['rows' => 4, 'class' => 'form-control'],
28+
'attr' => ['rows' => 4, 'class' => 'form-control'],
2929
]);
3030
}
3131

demo/symfony7/src/Form/MysqlAesNoteType.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
1818
$builder
1919
->add('title', TextType::class, [
2020
'label' => 'Title',
21-
'help' => 'Stored in plain text.',
22-
'attr' => ['class' => 'form-control'],
21+
'help' => 'Stored in plain text.',
22+
'attr' => ['class' => 'form-control'],
2323
])
2424
->add('secretOrm', TextareaType::class, [
25-
'label' => 'Secret',
26-
'required' => false,
27-
'help' => 'Encrypted by the bundle (MysqlAes) in column <code>secret_orm</code>.',
25+
'label' => 'Secret',
26+
'required' => false,
27+
'help' => 'Encrypted by the bundle (MysqlAes) in column <code>secret_orm</code>.',
2828
'help_html' => true,
29-
'attr' => ['rows' => 4, 'class' => 'form-control'],
29+
'attr' => ['rows' => 4, 'class' => 'form-control'],
3030
]);
3131
}
3232

demo/symfony7/src/Repository/MysqlAesNoteRepository.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Nowo\DoctrineEncryptBundle\Util\EncryptUtil;
1717
use RuntimeException;
1818

19+
use function is_resource;
20+
1921
/**
2022
* Examples of MySQL AES_ENCRYPT / AES_DECRYPT in raw SQL (native path).
2123
*/
@@ -68,7 +70,7 @@ public function findAllDecryptedWithAesDecrypt(): array
6870
/**
6971
* List native AES rows with optional LIKE filters (MySQL).
7072
*
71-
* @param 'decrypted'|'ciphertext' $secretMode decrypted = LIKE on CAST(AES_DECRYPT(...)); ciphertext = LIKE on HEX(blob)
73+
* @param 'ciphertext'|'decrypted' $secretMode decrypted = LIKE on CAST(AES_DECRYPT(...)); ciphertext = LIKE on HEX(blob)
7274
*
7375
* @return list<array{id: int, title: string, secret_plain: ?string}>
7476
*/
@@ -111,7 +113,7 @@ public function findDecryptedWithAesDecryptFiltered(
111113
/**
112114
* Doctrine list: title via DQL LIKE; secret via PHP (plaintext) or DQL LIKE on ciphertext column.
113115
*
114-
* @param 'plaintext'|'ciphertext' $secretMode
116+
* @param 'ciphertext'|'plaintext' $secretMode
115117
*
116118
* @return list<MysqlAesNote>
117119
*/
@@ -264,9 +266,9 @@ public function findAllStorageComparison(EncryptUtil $encryptUtil, EncryptorRegi
264266
'id' => (int) $row['id'],
265267
'title' => (string) $row['title'],
266268
'secret_orm_raw' => $ormRaw,
267-
'secret_orm_decrypted' => $ormRaw !== null ? $encryptUtil->decrypt($ormRaw, 'mysql_aes') : null,
268-
'secret_native_hex' => $nativeBlob !== null && $nativeBlob !== '' ? bin2hex($nativeBlob) : null,
269-
'secret_native_decrypted' => $this->decryptNativeBlob($nativeBlob, $mysqlAes),
269+
'secret_orm_decrypted' => $ormRaw !== null ? $encryptUtil->decrypt($ormRaw, 'mysql_aes') : null,
270+
'secret_native_hex' => $nativeBlob !== null && $nativeBlob !== '' ? bin2hex($nativeBlob) : null,
271+
'secret_native_decrypted' => $this->decryptNativeBlob($nativeBlob, $mysqlAes),
270272
];
271273
}
272274

@@ -308,10 +310,7 @@ private function normalizeBlob(mixed $value): ?string
308310
private function assertNativeMysql(): void
309311
{
310312
if (!$this->supportsNativeMysqlAes()) {
311-
throw new RuntimeException(
312-
'Native AES_ENCRYPT/AES_DECRYPT requires MySQL or MariaDB. '
313-
. 'Set DATABASE_URL to a MySQL DSN (see demo .env.example).',
314-
);
313+
throw new RuntimeException('Native AES_ENCRYPT/AES_DECRYPT requires MySQL or MariaDB. Set DATABASE_URL to a MySQL DSN (see demo .env.example).');
315314
}
316315
}
317316
}

demo/symfony8/config/bundles.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
return [
4-
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
5-
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
6-
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
7-
Nowo\DoctrineEncryptBundle\NowoDoctrineEncryptBundle::class => ['all' => true],
6+
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
7+
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
8+
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
9+
Nowo\DoctrineEncryptBundle\NowoDoctrineEncryptBundle::class => ['all' => true],
810
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
9-
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
10-
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
11-
Nowo\TwigInspectorBundle\NowoTwigInspectorBundle::class => ['dev' => true, 'test' => true],
11+
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
12+
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
13+
Nowo\TwigInspectorBundle\NowoTwigInspectorBundle::class => ['dev' => true, 'test' => true],
1214
];

demo/symfony8/config/reference.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
// This file is auto-generated and is for apps only. Bundles SHOULD NOT rely on its content.
46

57
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

0 commit comments

Comments
 (0)