-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
303 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
namespace Swissup\Email\Api; | ||
|
||
interface EncryptorInterface | ||
{ | ||
/** | ||
* Encrypt a string | ||
* | ||
* @param string $data | ||
* @return string | ||
*/ | ||
public function encrypt($data); | ||
|
||
/** | ||
* Decrypt a string | ||
* | ||
* @param string $data | ||
* @return string | ||
*/ | ||
public function decrypt($data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
namespace Swissup\Email\Api; | ||
|
||
use Swissup\Email\Api\Data\ServiceInterface; | ||
|
||
interface ServiceEncryptorInterface | ||
{ | ||
/** | ||
* Encrypt a string | ||
* | ||
* @param string $data | ||
* @return string | ||
*/ | ||
public function encrypt(ServiceInterface $object): void; | ||
|
||
/** | ||
* Decrypt a string | ||
* | ||
* @param string $data | ||
* @return string | ||
*/ | ||
public function decrypt(ServiceInterface $object): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
namespace Swissup\Email\Model\Data; | ||
|
||
use Swissup\Email\Api\EncryptorInterface; | ||
|
||
class Encryptor implements EncryptorInterface | ||
{ | ||
const PREFIX = 'encrypted:'; | ||
|
||
/** | ||
* @var \Magento\Framework\Encryption\EncryptorInterface | ||
*/ | ||
private $encryptor; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\Encryption\EncryptorInterface $encryptor | ||
) { | ||
$this->encryptor = $encryptor; | ||
} | ||
|
||
private function isEncryptionEnabled() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Encrypt a string | ||
* | ||
* @param string $data | ||
* @return string | ||
*/ | ||
public function encrypt($data) | ||
{ | ||
$isEncryptionEnabled = $this->isEncryptionEnabled(); | ||
$data = (string) $data; | ||
if ($isEncryptionEnabled) { | ||
$data = self::PREFIX . $this->encryptor->encrypt($data); | ||
} | ||
return $data; | ||
} | ||
|
||
/** | ||
* @param string $data | ||
* @return bool | ||
*/ | ||
private function isEncryptedData(string $data) | ||
{ | ||
return strpos($data, self::PREFIX) === 0; | ||
} | ||
|
||
/** | ||
* Decrypt a string | ||
* | ||
* @param string $data | ||
* @return string | ||
*/ | ||
public function decrypt($data) | ||
{ | ||
$isEncryptionEnabled = $this->isEncryptionEnabled(); | ||
if ($isEncryptionEnabled) { | ||
$data = (string) $data; | ||
$prefix = self::PREFIX; | ||
if ($this->isEncryptedData($data)) { | ||
$data = substr($data, strlen($prefix)); | ||
$data = $this->encryptor->decrypt($data); | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
namespace Swissup\Email\Model\Service; | ||
|
||
use Swissup\Email\Api\EncryptorInterface; | ||
use Swissup\Email\Api\ServiceEncryptorInterface; | ||
use Swissup\Email\Api\Data\ServiceInterface; | ||
|
||
class Encryptor implements ServiceEncryptorInterface | ||
{ | ||
/** | ||
* @var EncryptorInterface | ||
*/ | ||
private $encryptor; | ||
|
||
/** | ||
* @param EncryptorInterface $encryptor | ||
*/ | ||
public function __construct(EncryptorInterface $encryptor) | ||
{ | ||
$this->encryptor = $encryptor; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
private function getAttributes() | ||
{ | ||
return ['password']; | ||
} | ||
|
||
/** | ||
* @param DataObject $object | ||
* @return void | ||
*/ | ||
public function encrypt(ServiceInterface $object): void | ||
{ | ||
foreach ($this->getAttributes() as $attributeCode) { | ||
$value = $object->getData($attributeCode); | ||
if ($value) { | ||
$object->setData($attributeCode, $this->encryptor->encrypt($value)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param DataObject $object | ||
* @return void | ||
*/ | ||
public function decrypt(ServiceInterface $object): void | ||
{ | ||
foreach ($this->getAttributes() as $attributeCode) { | ||
$value = $object->getData($attributeCode); | ||
if ($value) { | ||
try { | ||
$object->setData($attributeCode, $this->encryptor->decrypt($value)); | ||
} catch (\Exception $e) { | ||
// value is not encrypted or something wrong with encrypted data | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Swissup\Email\Plugin\Model; | ||
|
||
use Magento\Framework\DataObject; | ||
use Swissup\Email\Model\Service; | ||
|
||
class ServicePlugin | ||
{ | ||
/** | ||
* @var \Swissup\Email\Api\ServiceEncryptorInterface | ||
*/ | ||
private $serviceEncryptor; | ||
|
||
/** | ||
* @param \Swissup\Email\Api\ServiceEncryptorInterface $serviceEncryptor | ||
*/ | ||
public function __construct(\Swissup\Email\Api\ServiceEncryptorInterface $serviceEncryptor) | ||
{ | ||
$this->serviceEncryptor = $serviceEncryptor; | ||
} | ||
|
||
public function afterBeforeSave(Service $subject): void | ||
{ | ||
$this->serviceEncryptor->encrypt($subject); | ||
} | ||
|
||
public function afterAfterSave(Service $subject): void | ||
{ | ||
$this->serviceEncryptor->decrypt($subject); | ||
} | ||
|
||
public function afterAfterLoad(Service $subject): void | ||
{ | ||
$this->serviceEncryptor->decrypt($subject); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Swissup\Email\Setup\Patch\Data; | ||
|
||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
use Magento\Framework\Setup\Patch\DataPatchInterface; | ||
use Magento\Framework\Setup\Patch\PatchVersionInterface; | ||
|
||
class EncryptPasswords implements DataPatchInterface, PatchVersionInterface | ||
{ | ||
/** | ||
* @var ModuleDataSetupInterface | ||
*/ | ||
private $moduleDataSetup; | ||
|
||
/** | ||
* @var \Swissup\Email\Model\Service\CollectionFactory | ||
*/ | ||
private $serviceCollectionFactory; | ||
|
||
/** | ||
* @param ModuleDataSetupInterface $moduleDataSetup | ||
* @param \Swissup\Email\Model\Service\CollectionFactory $serviceCollectionFactory | ||
*/ | ||
public function __construct( | ||
ModuleDataSetupInterface $moduleDataSetup, | ||
\Swissup\Email\Model\Service\CollectionFactory $serviceCollectionFactory | ||
) { | ||
$this->moduleDataSetup = $moduleDataSetup; | ||
$this->serviceCollectionFactory = $serviceCollectionFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function apply() | ||
{ | ||
$setup = $this->moduleDataSetup; | ||
$setup->startSetup(); | ||
|
||
$collection = $this->serviceCollectionFactory->create(); | ||
foreach ($collection as $service) { | ||
$service->load($service->getId()); | ||
$service->setDataChanges(true); | ||
$service->save(); | ||
} | ||
|
||
$setup->endSetup(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getDependencies() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getVersion() | ||
{ | ||
return '2.3.0'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAliases() | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters