Skip to content
Snippets Groups Projects
Commit d02150b9 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

hotline #78923 : add expired user deletion script

parent 71d86998
Branches
Tags
3 merge requests!3297WIP: Master,!3047Hotline,!3044hotline #78923 : add expired user deletion script
Pipeline #6490 passed with stage
in 33 minutes and 41 seconds
- ticket #78923 : RGPD : Ajout d'un script de suppression des informations des abonnés dont l'abonnement est expiré
\ No newline at end of file
<?php
require './console.php';
echo 'Users Deletion
\ . ./
\ .:";\'.:.." /
(M^^.^~~:.\'").
- (/ . . . \ \) -
O ((| :. ~ ^ :. .|))
|\\ - (\- | \ / | /) -
| T -\ \ / /-
/ \[_]..........................\ \ / /
I will detect users no longer exported by ILS
and with expired registration.
Then I will delete them and their datas.
';
readline('press enter to continue...');
class ExpiredUserDeletion {
use Trait_MemoryCleaner;
protected
$_delay,
$_deleted = 0;
public function __construct($delay) {
$this->_delay = $delay;
}
public function count() {
return Class_Users::countBy(['where' => $this->_whereClause()]);
}
public function run($for_real=false) {
$this->_deleted = $last_id = 0;
while($users = Class_Users::findAllBy(['where' => $this->_whereClause($last_id),
'limit' => 200])) {
foreach($users as $user) {
if ($for_real)
$user->delete();
$last_id = $user->getId();
echo '.';
$this->_deleted++;
}
echo "\n";
$this->_cleanMemory();
}
}
public function deleted() {
return $this->_deleted;
}
protected function _whereClause($last_id=0) {
return implode(' and ',
['statut = 1',
'role_level = 2',
'id_user > ' . (int)$last_id,
'STR_TO_DATE(date_fin, \'%Y-%m-%d\') < "' . $this->_delay . '"']);
}
}
$options = getopt('d:', ['force']);
$usage = 'Usage : php scripts/user_delete_expired.php -d "2 years ago" [--force]
-d : delay, required, syntax as in http://php.net/manual/en/datetime.formats.relative.php
--force : really delete users, optional, if not passed will simulate
';
if (!isset($options['d']) || !$options['d']) {
echo $usage;
exit(255);
}
try {
$delay = (new DateTime($options['d']))->format('Y-m-d');
} catch(Exception $e) {
echo 'Invalid delay
';
echo $usage;
exit(255);
}
echo 'Will delete users expired since : ' . $delay . '
';
$deletion = new ExpiredUserDeletion($delay);
$count = $deletion->count();
echo $count . ' users expired since ' . $delay . ' in database
';
if (0 == $count) {
echo 'No user to delete
';
exit(255);
}
$for_real = array_key_exists('force', $options);
if ($for_real)
readline('CAUTION : you asked to run for real, users will be deleted !');
echo $for_real
? 'OH MY, RUNNING FOR REAL, GOOD LUCK'
: 'pro tip: use --force option to run for real, with great powers comes great responsibility
Simulation mode engaged...';
echo '
';
echo 'Started at ' . date('c') . "\n";
$deletion->run($for_real);
echo "\nEnded at " . date('c') . "\n";
echo $deletion->deleted() . ' users' . ($for_real ? '': ' would have been') . ' deleted
';
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment