Skip to content

Commit

Permalink
Fix #193 Anonymize command must also anonymize customer emails in sav
Browse files Browse the repository at this point in the history
Officially release the version 1.6.2
  • Loading branch information
nenes25 committed Mar 11, 2021
1 parent 93ef506 commit 71d149e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ then
echo $PHP73VALID
fi

exit 1
#exit 0
fi

#Dev file not versionned
Expand Down
4 changes: 2 additions & 2 deletions COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,7 @@ This command will anonymize customer related data (lastname,firstname,email ) wi

#### `--type`

allowed values all|customers|addresses|newsletter
allowed values all | customers | addresses | newsletter | customer_thread

* Accept value: yes
* Is value required: no
Expand All @@ -1997,7 +1997,7 @@ emails to exclude separated by commas

#### `--names`

anonymize names (default none ) use only for customers
anonymize names (default no ) use only for customers

* Accept value: yes
* Is value required: no
Expand Down
2 changes: 1 addition & 1 deletion bin/phar/current.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62da89a6dde519cfca6477c3ae4a498d00b9a25d
dc959a46c69d474210d72f4cfeb69e08af8b94ac
Binary file modified bin/prestashopConsole.phar
Binary file not shown.
4 changes: 3 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* http://www.h-hennes.fr/blog/
*/
2020-12-29 - V 1.6.2 : #142 Error when try to start by prestashopConsole command
2021-03-11 - V 1.6.2 : #142 Error when try to start by prestashopConsole command
#170 Create Webservice key command
#173 Change link in readme
#175 New Command Webservice:key:delete
#182 Fix PhpStan Errors
#193 Anonymize command must also anonymize customer emails in sav
Disable the install command which will be refactored in next version
Branch 1.6 is the last branch which deals with Prestashop 1.6 and Php < 7.2
Small improvements
2020-06-25 - V 1.6.1 : #106 Error in generate Images commands
#107 Improve module:hook:list command
#108 Update licence header for generated code
Expand Down
106 changes: 95 additions & 11 deletions src/Hhennes/PrestashopConsole/Command/Dev/AnonymizeCustomerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,47 @@

class AnonymizeCustomerCommand extends Command
{
protected $_allowedTypes = array('all', 'customers', 'addresses', 'newsletter');
/**
* @var array Allowed types of anonymizations
*/
protected $_allowedTypes = array('all', 'customers', 'addresses', 'newsletter', 'customer_thread');

/**
* @var null|string List of email to not anonymize
*/
protected $_excludedEmail = null;

/**
* @var string Fake domain email
*/
protected $_fakeEmailsDomain = 'anonymized-email.com';

/**
* @inheritDoc
*/
protected function configure()
{
$this
->setName('dev:anonymize:customer')
->setDescription('Anonymize Customer information')
->addOption('type', null, InputOption::VALUE_OPTIONAL, 'allowed values all|customers|addresses|newsletter')
->addOption('exclude-emails', null, InputOption::VALUE_OPTIONAL, 'emails to exclude separated by commas')
->addOption('names', null, InputOption::VALUE_OPTIONAL, 'anonymize names (default none ) use only for customers')
->addOption(
'type',
null,
InputOption::VALUE_OPTIONAL,
'allowed values '.implode(' | ', $this->_allowedTypes)
)
->addOption(
'exclude-emails',
null,
InputOption::VALUE_OPTIONAL,
'emails to exclude separated by commas'
)
->addOption(
'names',
null,
InputOption::VALUE_OPTIONAL,
'anonymize names (default no ) use only for customers'
)
->setHelp('This command will anonymize customer related data (lastname,firstname,email ) without erasing them');
}

Expand All @@ -64,7 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$type = 'customers';
}

$method = '_anonymize' . ucfirst(strtolower($type));
$method = '_anonymize' . ucfirst(strtolower(str_replace('_', '', $type)));
$message = $this->$method($input);
$output->writeln($message);
}
Expand All @@ -77,9 +107,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
protected function _anonymizeAll(InputInterface $input)
{
$message = '';
$message .= $this->_anonymizeCustomers($input)."\n";
$message .= $this->_anonymizeAddresses($input)."\n";
$message .= $this->_anonymizeNewsletter($input);
$message .= $this->_anonymizeCustomers($input) . "\n";
$message .= $this->_anonymizeAddresses($input) . "\n";
$message .= $this->_anonymizeNewsletter($input) . "\n";
$message .= $this->_anonymizeCustomerthread($input);

return $message;
}
Expand Down Expand Up @@ -128,7 +159,8 @@ protected function _anonymizeAddresses(InputInterface $input)
{
$sqlCond = '';
if ($this->_excludedEmail) {
$sqlCond .= 'WHERE id_customer NOT IN ( SELECT id_customer FROM ' . _DB_PREFIX_ . 'customer WHERE email IN (';
$sqlCond .= 'WHERE id_customer NOT IN (
SELECT id_customer FROM ' . _DB_PREFIX_ . 'customer WHERE email IN (';
$emails = explode(',', $this->_excludedEmail);
foreach ($emails as $email) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
Expand All @@ -140,7 +172,11 @@ protected function _anonymizeAddresses(InputInterface $input)
}

try {
Db::getInstance()->execute("UPDATE " . _DB_PREFIX_ . "address SET lastname = '" . $this->_randomString() . "', firstname = '" . $this->_randomString() . "' " . $sqlCond);
Db::getInstance()->execute(
"UPDATE " . _DB_PREFIX_ . "address
SET lastname = '" . $this->_randomString() . "', firstname = '" . $this->_randomString() . "' "
. $sqlCond
);
} catch (PrestaShopDatabaseException $e) {
return '<error>' . strip_tags($e->getMessage()) . '</error>';
}
Expand Down Expand Up @@ -175,14 +211,51 @@ protected function _anonymizeNewsletter(InputInterface $input)
}

try {
Db::getInstance()->execute("UPDATE " . _DB_PREFIX_ . $table . " SET email = CONCAT(MD5(email),'@fake-email.com') " . $sqlCond);
Db::getInstance()->execute(
"UPDATE " . _DB_PREFIX_ . $table . "
SET email = CONCAT(MD5(email),'@" . $this->_fakeEmailsDomain . "') "
. $sqlCond
);
} catch (PrestaShopDatabaseException $e) {
return '<error>' . strip_tags($e->getMessage()) . '</error>';
}

return '<info>Newsletter subscriber anonymized with success</info>';
}

/**
* Anonymize customerThread data
* @param InputInterface $input
* @return string
*/
protected function _anonymizeCustomerthread(InputInterface $input)
{
$sqlCond = '';
if ($this->_excludedEmail) {
$sqlCond .= 'WHERE email NOT IN (';
$emails = explode(',', $this->_excludedEmail);
foreach ($emails as $email) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$sqlCond .= "'" . pSQL($email) . "',";
}
}
$sqlCond = rtrim($sqlCond, ',');
$sqlCond .= ')';
}

try {
Db::getInstance()->execute(
"UPDATE " . _DB_PREFIX_ . "customer_thread
SET email = CONCAT(MD5(email),'@" . $this->_fakeEmailsDomain . "') "
. $sqlCond
);
} catch (PrestaShopDatabaseException $e) {
return '<error>' . strip_tags($e->getMessage()) . '</error>';
}

return '<info>Customer thread anonymized with success</info>';
}


/**
* Get Type Question
Expand Down Expand Up @@ -223,6 +296,17 @@ protected function getEmailQuestion()
return $emailQuestion;
}

/**
* Randomize email for db integration
* @param string $email
* @return string
*/
protected function _randomizeEmail($email)
{
$randomEmail = md5(sha1($email) . $this->_randomString());
return $randomEmail . '@' . $this->_fakeEmailsDomain;
}

/**
* @param int $length
* @return string
Expand Down

0 comments on commit 71d149e

Please sign in to comment.