Skip to content

Commit f06ca10

Browse files
committed
Add admin config force_renew_password_at_first_login BT#18811
1 parent 3ee3598 commit f06ca10

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

main/auth/reset.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
2+
23
/* For license terms, see /license.txt */
34

45
require_once __DIR__.'/../inc/global.inc.php';
56

6-
$token = isset($_GET['token']) ? $_GET['token'] : '';
7+
$token = $_GET['token'] ?? '';
78

89
if (!ctype_alnum($token)) {
910
$token = '';
@@ -37,6 +38,7 @@
3738

3839
/** @var \Chamilo\UserBundle\Entity\User $user */
3940
$user = UserManager::getManager()->findUserByConfirmationToken($token);
41+
4042
if ($user) {
4143
if (!$user->isPasswordRequestNonExpired($ttl)) {
4244
Display::addFlash(Display::return_message(get_lang('LinkExpired')), 'warning');
@@ -54,6 +56,14 @@
5456
Database::getManager()->persist($user);
5557
Database::getManager()->flush();
5658

59+
if (api_get_configuration_value('force_renew_password_at_first_login')) {
60+
$extraFieldValue = new ExtraFieldValue('user');
61+
$value = $extraFieldValue->get_values_by_handler_and_field_variable($user->getId(), 'ask_new_password');
62+
if (!empty($value) && isset($value['value']) && 1 === (int) $value['value']) {
63+
$extraFieldValue->delete($value['id']);
64+
}
65+
}
66+
5767
Display::addFlash(Display::return_message(get_lang('Updated')));
5868
header('Location: '.api_get_path(WEB_PATH));
5969
exit;

main/inc/lib/usermanager.lib.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7319,4 +7319,32 @@ private static function getGravatar(
73197319

73207320
return $url;
73217321
}
7322+
7323+
public static function redirectToResetPassword($userId)
7324+
{
7325+
if (!api_get_configuration_value('force_renew_password_at_first_login')) {
7326+
return;
7327+
}
7328+
7329+
$askPassword = self::get_extra_user_data_by_field(
7330+
$userId,
7331+
'ask_new_password'
7332+
);
7333+
7334+
if (!empty($askPassword) && isset($askPassword['ask_new_password']) &&
7335+
1 === (int) $askPassword['ask_new_password']
7336+
) {
7337+
$uniqueId = api_get_unique_id();
7338+
$userObj = api_get_user_entity($userId);
7339+
7340+
$userObj->setConfirmationToken($uniqueId);
7341+
$userObj->setPasswordRequestedAt(new \DateTime());
7342+
7343+
Database::getManager()->persist($userObj);
7344+
Database::getManager()->flush();
7345+
7346+
$url = api_get_path(WEB_CODE_PATH).'auth/reset.php?token='.$uniqueId;
7347+
api_location($url);
7348+
}
7349+
}
73227350
}

main/inc/local.inc.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@
516516
if (is_array($my_url_list) &&
517517
in_array($current_access_url_id, $my_url_list)
518518
) {
519+
UserManager::redirectToResetPassword($uData['user_id']);
519520
ConditionalLogin::check_conditions($uData);
520521

521522
$_user['user_id'] = $uData['user_id'];
@@ -536,9 +537,9 @@
536537
exit;
537538
}
538539
} else {
539-
//Only admins of the "main" (first) Chamilo portal can login wherever they want
540+
// Only admins of the "main" (first) Chamilo portal can login wherever they want
540541
if (in_array(1, $my_url_list)) {
541-
//Check if this admin have the access_url_id = 1 which means the principal
542+
// Check if this admin have the access_url_id = 1 which means the principal
542543
ConditionalLogin::check_conditions($uData);
543544
$_user['user_id'] = $uData['user_id'];
544545
$_user['status'] = $uData['status'];
@@ -548,6 +549,7 @@
548549
} else {
549550
//This means a secondary admin wants to login so we check as he's a normal user
550551
if (in_array($current_access_url_id, $my_url_list)) {
552+
UserManager::redirectToResetPassword($uData['user_id']);
551553
$_user['user_id'] = $uData['user_id'];
552554
$_user['status'] = $uData['status'];
553555
Session::write('_user', $_user);
@@ -566,6 +568,7 @@
566568
}
567569
}
568570
} else {
571+
UserManager::redirectToResetPassword($uData['user_id']);
569572
ConditionalLogin::check_conditions($uData);
570573
$_user['user_id'] = $uData['user_id'];
571574
$_user['status'] = $uData['status'];

main/install/configuration.dist.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,10 @@
19451945
// Disable webservices.
19461946
//$_configuration['disable_webservices'] = true;
19471947

1948+
// Ask user to renew password at first login.
1949+
// Requires a user checkbox extra field called "ask_new_password".
1950+
//$_configuration['force_renew_password_at_first_login'] = true;
1951+
19481952
// KEEP THIS AT THE END
19491953
// -------- Custom DB changes
19501954
// Add user activation by confirmation email

0 commit comments

Comments
 (0)