psolutions / encrypt-bundle
该套餐提供通过属性加密值的服务。
Requires
- php: >=8.2
- ext-openssl: *
- doctrine/annotations: ^1.13|^2.0
- doctrine/doctrine-bundle: ^2.6
- doctrine/orm: ^2.12
- symfony/console: ^6.4|^7.0
- symfony/framework-bundle: ^6.4|^7.0
- symfony/monolog-bundle: ^3.8
Requires (Dev)
- symfony/phpunit-bridge: ^6.4|^7.0
This package is auto-updated.
Last update: 2024-09-17 00:27:50 UTC
README
A bundle to handle encoding and decoding of parameters using OpenSSL and Doctrine lifecycle events. It's a fork of https://github.com/mogilvie/EncryptBundle
功能包括
- v1 兼容 Symfony 6.4 和 7.0。
- 使用 OpenSSL
- 使用事件监听器
功能路线图
- 创建一个工厂方法以扩展不同的加密器
- 创建一个 twig 函数以解密编码的值
- 扩展参数以允许选择编码方法
- 创建 CLI 命令以加密和解密整个数据库
- 通过套餐处理 DateTime 数据类型。
许可证
此套餐受 MIT 许可证保护。请参阅套餐中的完整许可证。
Resources/meta/LICENSE
关于
EncryptBundle 是为 Parolla Plugins 和 Parolla 网站编写的,用于编码用户的私人数据。该套餐在更大的 gdpr-bundle 中扩展。
报告问题或功能请求
问题和功能请求在 Github 问题跟踪器 中跟踪。
当报告错误时,在基本项目中重现它可能是个好主意,该项目使用 Symfony Standard Edition 构建,以便套餐的开发者可以通过简单地克隆它并遵循一些步骤来重现问题。
安装
步骤 1: 从包中安装
打开命令行,进入您的项目目录,并执行以下命令以下载此套餐的最新开发版本
$ composer require psolutions/encrypt-bundle
步骤 2: 启用套餐
食谱将在 config/packages/psolutions_encrypt.yaml 下创建一个包配置文件。
如果需要,可以通过将其添加到项目的 config/bundles.php 文件中注册的套餐列表中来启用套餐
<?php return [ ... PSolutions\EncryptBundle\PSolutionsEncryptBundle::class => ['all' => true], ];
步骤 2: 配置套餐
使用套餐中提供的命令生成一个 256 位密钥。
$ bin/console encrypt:genkey
将密钥复制到您的 .env 文件中。
###> encrypt-bundle ###
PSOLUTIONS_ENCRYPT_KEY= change_me!
###< encrypt-bundle ###
将密钥解析到已创建的 packages yaml 文件中。
# app/config/packages/psolutions_encrypt.yaml psolutions_encrypt: encrypt_key: '%env(PSOLUTIONS_ENCRYPT_KEY)%' is_disabled: false # Turn this to true to disable the encryption. connections: # Optional, define the connection name(s) for the subscriber to listen to. - 'default' - 'tenant' encryptor_class: App\Encryptors\MyCustomEncryptor # Optional to override the bundle OpenSslEncryptor. annotation_classes: # Optional to override the default annotation/Attribute object. - App\Annotation\MyAttribute
您可以通过将 'is_disabled' 选项设置为 true 来禁用加密。如果任何值包含 <ENC> 后缀,则仍然继续解密。
如果您想定义自己的注解/属性,则可以使用此来通过将注解类名称添加到 'annotation_classes' 选项数组中来触发加密。
您可以使用可选的 encryptorClass 选项传递您自己的加密器服务类名称。
替代 EncryptKeyEvent
可以通过派发的监听器设置 EncryptKey,这会覆盖任何 .env 或 param.yml 中定义的密钥。为 EncryptKeyEvents::LOAD_KEY 事件创建一个监听器,并在该点设置您的加密密钥。
步骤 3: 创建实体
在实体中添加 Encrypted 属性类。
<?php ... use PSolutions\EncryptBundle\Annotations\Encrypted;
将 #[Encrypted] 属性添加到您想要加密的属性中。
<?php #[Encrypted] #[Column] protected string $taxNumber; #[Column(type: string, nullable: true)] #[Encrypted] protected ?bool $isSelfEmployed; /** * Date of birth */ #[Encrypted] #[Column] protected ?String $dob;
在加密字段时,您需要将列类型设置为字符串。
您的getter和setter可能也需要声明类型。
例如,布尔值应该返回声明为bool的值,或者使用三元方法返回bool。
<?php /** * Get isSelfEmployed * * @return boolean */ public function isSelfEmployed(): bool { return $this->isSelfEmployed; } /** * Get isSelfEmployed * * @return boolean */ public function isSelfEmployed(): bool { return ($this->isSelfEmployed == 1 ? true: false); }
对于DateTime参数,将日期存储为字符串,并使用getter和setter转换该字符串。
如果您在带有DateType表单类型的表单中使用该参数,可能需要创建一个DataTransformer。
步骤4:通用使用
此捆绑包包含一个DoctrineEncryptListener。此监听器捕获doctrine的onLoad、onFlush和postFlush事件。
onLoad事件监听器将在加载时解密实体参数。这意味着您的表单和表单字段已经被解密。
onFlush和postFlush事件监听器将检查是否启用了加密,并在将数据存储到数据库之前加密数据。
因此,在常规的CRUD操作中,您不需要在控制器中进行加密或解密数据的操作。
步骤5:在服务和控制器中解密
当然,您可以在任何时候通过自动装配或定义服务定义中的注入,将EncryptorInterface服务注入到类中。
<?php use PSolutions\EncryptBundle\Encryptors\EncryptorInterface; // Inject the Encryptor from the service container at class construction public function __construct(private readonly EncryptorInterface $encryptor) { } // Inject the Encryptor in controller actions. public function editAction(EncryptorInterface $encryptor) { ... // An example encrypted value, you would get this from your database query. $encryptedValue = "3DDOXwqZAEEDPJDK8/LI4wDsftqaNCN2kkyt8+QWr8E=<ENC>"; $decrypted = $encryptor->decrypt($encryptedValue); ... }
或者您可以派发EncryptEvent。
<?php ... use PSolutions\EncryptBundle\Event\EncryptEvent; use PSolutions\EncryptBundle\Event\EncryptEvents; use Symfony\Component\EventDispatcher\EventDispatcherInterface; ... public function indexAction(EventDispatcherInterface $dispatcher) { ... // An example encrypted value, you would get this from your database query. $event = new EncryptEvent("3DDOXwqZAEEDPJDK8/LI4wDsftqaNCN2kkyt8+QWr8E=<ENC>"); $dispatcher->dispatch(EncryptEvents::DECRYPT, $event); $decrypted = $event->getValue(); }
步骤5:在模板中解密
如果您使用具有数组结果的select查询查询存储库,则doctrine的onLoad事件订阅者不会解密任何加密值。
在这种情况下,请使用twig过滤器在渲染时解密您的值。
{{ employee.bankAccountNumber | decrypt }}
命令
您已经看到了生成加密键的命令
$ bin/console encrypt:genkey
您可以使用以下方式解密/加密整个数据库
$ bin/console encrypt:database decrypt connection
必需的参数应该是decrypt或encrypt。
如果您的应用程序使用了多个连接,可以选择定义数据库连接。