Skip to content

Commit

Permalink
enhance password expiration message
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 authored and cedric-anne committed Jan 11, 2024
1 parent aa750fb commit db2db6a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
8 changes: 1 addition & 7 deletions front/helpdesk.public.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@
$password_alert = "";
$user = new User();
$user->getFromDB(Session::getLoginUserID());
if ($user->fields['authtype'] == Auth::DB_GLPI && $user->shouldChangePassword()) {
$password_alert = sprintf(
__('Your password will expire on %s.'),
Html::convDateTime(date('Y-m-d H:i:s', $user->getPasswordExpirationTime()))
);
}

$ticket_summary = "";
$survey_list = "";
Expand Down Expand Up @@ -140,7 +134,7 @@

Html::requireJs('masonry');
TemplateRenderer::getInstance()->display('pages/self-service/home.html.twig', [
'password_alert' => $password_alert,
'password_alert' => $user->getPasswordExpirationMessage(),
'ticket_summary' => $ticket_summary,
'survey_list' => $survey_list,
'reminder_list' => $reminder_list,
Expand Down
7 changes: 2 additions & 5 deletions src/Central.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,8 @@ private static function getMessages(): array

$user = new User();
$user->getFromDB(Session::getLoginUserID());
if ($user->fields['authtype'] == Auth::DB_GLPI && $user->shouldChangePassword()) {
$expiration_msg = sprintf(
__('Your password will expire on %s.'),
Html::convDateTime(date('Y-m-d H:i:s', $user->getPasswordExpirationTime()))
);
$expiration_msg = $user->getPasswordExpirationMessage();
if ($expiration_msg !== null) {
$messages['warnings'][] = $expiration_msg
. ' '
. '<a href="' . $CFG_GLPI['root_doc'] . '/front/updatepassword.php">'
Expand Down
27 changes: 27 additions & 0 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6365,6 +6365,13 @@ public function getPasswordExpirationTime()
return null;
}

if (null === $this->fields['password_last_update']) {
// password never updated
return strtotime(
'+ ' . $expiration_delay . ' days',
strtotime($this->fields['date_creation'])
);
}
return strtotime(
'+ ' . $expiration_delay . ' days',
strtotime($this->fields['password_last_update'])
Expand Down Expand Up @@ -6416,6 +6423,26 @@ public function hasPasswordExpired()
return $expiration_time < time();
}

public function getPasswordExpirationMessage(): ?string
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;
$expiration_msg = null;
if ($this->fields['authtype'] == Auth::DB_GLPI && $this->shouldChangePassword()) {
$expire_time = $this->getPasswordExpirationTime();
$expire_has_passed = $expire_time < time();
if ($expire_has_passed) {
$expiration_msg = __('Your password has expired.');
} else {
$expiration_msg = sprintf(
__('Your password will expire on %s.'),
Html::convDateTime(date('Y-m-d H:i:s', $expire_time))
);
}
}
return $expiration_msg;
}

public static function getFriendlyNameSearchCriteria(string $filter): array
{
$table = self::getTable();
Expand Down
32 changes: 28 additions & 4 deletions tests/functional/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ protected function passwordExpirationMethodsProvider()

return [
[
'creation_date' => $_SESSION['glpi_currenttime'],
'last_update' => date('Y-m-d H:i:s', strtotime('-10 years', $time)),
'expiration_delay' => -1,
'expiration_notice' => -1,
Expand All @@ -960,6 +961,7 @@ protected function passwordExpirationMethodsProvider()
'expected_has_password_expire' => false,
],
[
'creation_date' => $_SESSION['glpi_currenttime'],
'last_update' => date('Y-m-d H:i:s', strtotime('-10 days', $time)),
'expiration_delay' => 15,
'expiration_notice' => -1,
Expand All @@ -968,6 +970,7 @@ protected function passwordExpirationMethodsProvider()
'expected_has_password_expire' => false,
],
[
'creation_date' => $_SESSION['glpi_currenttime'],
'last_update' => date('Y-m-d H:i:s', strtotime('-10 days', $time)),
'expiration_delay' => 15,
'expiration_notice' => 10,
Expand All @@ -976,21 +979,41 @@ protected function passwordExpirationMethodsProvider()
'expected_has_password_expire' => false,
],
[
'creation_date' => $_SESSION['glpi_currenttime'],
'last_update' => date('Y-m-d H:i:s', strtotime('-20 days', $time)),
'expiration_delay' => 15,
'expiration_notice' => -1,
'expected_expiration_time' => strtotime('-5 days', $time),
'expected_should_change_password' => true,
'expected_has_password_expire' => true,
],
[
'creation_date' => $_SESSION['glpi_currenttime'],
'last_update' => null,
'expiration_delay' => 15,
'expiration_notice' => -1,
'expected_expiration_time' => strtotime('+15 days', strtotime($_SESSION['glpi_currenttime'])),
'expected_should_change_password' => false,
'expected_has_password_expire' => false,
],
[
'creation_date' => '2021-12-03 17:54:32',
'last_update' => null,
'expiration_delay' => 15,
'expiration_notice' => -1,
'expected_expiration_time' => strtotime('2021-12-18 17:54:32'),
'expected_should_change_password' => true,
'expected_has_password_expire' => true,
],
];
}

/**
* @dataProvider passwordExpirationMethodsProvider
*/
public function testPasswordExpirationMethods(
string $last_update,
string $creation_date,
?string $last_update,
int $expiration_delay,
int $expiration_notice,
$expected_expiration_time,
Expand All @@ -1003,9 +1026,10 @@ public function testPasswordExpirationMethods(
$username = 'prepare_for_update_' . mt_rand();
$user_id = $user->add(
[
'name' => $username,
'password' => 'pass',
'password2' => 'pass'
'date_creation' => $creation_date,
'name' => $username,
'password' => 'pass',
'password2' => 'pass'
]
);
$this->integer($user_id)->isGreaterThan(0);
Expand Down

0 comments on commit db2db6a

Please sign in to comment.