new: [cli] Add command to trigger password change on next login for users with old pw

pull/9255/head
Jeroen Pinoy 2023-08-17 12:50:27 +02:00
parent 828ee92e33
commit e4deb7914d
No known key found for this signature in database
GPG Key ID: DF33A50B8E4EE081
1 changed files with 37 additions and 0 deletions

View File

@ -104,6 +104,14 @@ class UserShell extends AppShell
],
],
]);
$parser->addSubcommand('require_password_change_for_old_passwords', [
'help' => __('Trigger forced password change on next login for users with an old (older than x days) password.'),
'parser' => [
'arguments' => [
'days' => ['help' => __('Amount of days after which a password is considered "old" and needs to be changed.'), 'required' => true]
],
]
]);
return $parser;
}
@ -431,6 +439,35 @@ class UserShell extends AppShell
}
}
public function require_password_change_for_old_passwords(){
list($days) = $this->args;
if(!is_numeric($days)){
$this->error("The amount of days after which a password change is required (the argument) should be numeric.");
}
$interval = 'P' . $days . 'D';
$current_time = new DateTime();
$time_before_change_required = $current_time->sub(new DateInterval($interval))->getTimestamp();
$users = $this->User->find('all', [
'conditions' => [
'OR' => [
'last_pw_change <' => $time_before_change_required
]
],
'fields' => ['id'],
'recursive' => 0
]);
foreach ($users as $user) {
$user['User']['change_pw'] = true;
$userId = $user['User']['id'];
if (!$this->User->save($user['User'], true, ["change_pw"])) {
$this->out("Could not update user $userId.");
$this->out($this->json($this->User->validationErrors));
$this->_stop(self::CODE_ERROR);
}
}
}
/**
* @param string|int $userId
* @return array