diff --git a/front/helpdesk.public.php b/front/helpdesk.public.php index 5b0ee9544b5..9d06573cbbb 100644 --- a/front/helpdesk.public.php +++ b/front/helpdesk.public.php @@ -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 = ""; @@ -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, diff --git a/src/Central.php b/src/Central.php index 33db54c3c8f..14c23895231 100644 --- a/src/Central.php +++ b/src/Central.php @@ -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 . ' ' . '' diff --git a/src/User.php b/src/User.php index 685ee0c2b7e..53097613d21 100644 --- a/src/User.php +++ b/src/User.php @@ -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']) @@ -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(); diff --git a/tests/functional/User.php b/tests/functional/User.php index 73f35b2a0cc..99124b98980 100644 --- a/tests/functional/User.php +++ b/tests/functional/User.php @@ -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, @@ -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, @@ -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, @@ -976,6 +979,7 @@ 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, @@ -983,6 +987,24 @@ protected function passwordExpirationMethodsProvider() '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, + ], ]; } @@ -990,7 +1012,8 @@ protected function passwordExpirationMethodsProvider() * @dataProvider passwordExpirationMethodsProvider */ public function testPasswordExpirationMethods( - string $last_update, + string $creation_date, + ?string $last_update, int $expiration_delay, int $expiration_notice, $expected_expiration_time, @@ -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);