Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth: Extend the user.auth_source field #6100

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion public/main/admin/course_import.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Copyright (c) 2005 Bart Mollet <[email protected]>.
*/

use Chamilo\CoreBundle\Entity\UserAuthSource;

/**
* Validates imported data.
*
Expand Down Expand Up @@ -167,7 +169,7 @@ function parse_csv_courses_data($file)
$this_section = SECTION_PLATFORM_ADMIN;
api_protect_admin_script();

$defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
$defined_auth_sources[] = UserAuthSource::PLATFORM;

if (isset($extAuthSource) && is_array($extAuthSource)) {
$defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
Expand Down
4 changes: 2 additions & 2 deletions public/main/admin/ldap_synchro.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
$password = $val[0];
// Pour faciliter la gestion on ajoute le code "etape-annee"
$official_code = $etape."-".$annee;
$auth_source = "ldap";
$auth_source = ["ldap"];
// Pas de date d'expiration d'etudiant (a recuperer par rapport au shadow expire LDAP)
$expiration_date = '';
$active = 1;
Expand Down Expand Up @@ -126,7 +126,7 @@
$lastname,
$username,
null,
null,
[],
$email,
$status,
$official_code,
Expand Down
21 changes: 16 additions & 5 deletions public/main/admin/user_add.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Entity\UserAuthSource;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
use Chamilo\CoreBundle\ServiceHelper\AuthenticationConfigHelper;

$cidReset = true;
require_once __DIR__.'/../inc/global.inc.php';
Expand All @@ -15,6 +19,12 @@
api_protect_admin_script(true);
api_protect_limit_for_session_admin();

/** @var AuthenticationConfigHelper $authenticationConfigHelper */
$authenticationConfigHelper = Container::$container->get(AuthenticationConfigHelper::class);

/** @var AccessUrl $accessUrl */
$accessUrl = Container::$container->get(AccessUrlHelper::class)->getCurrent();

$is_platform_admin = api_is_platform_admin() ? 1 : 0;

$message = null;
Expand Down Expand Up @@ -175,23 +185,24 @@ function updateStatus(){

// Password
$group = [];
$extAuthSource = $authenticationConfigHelper->getAuthSourceAuthentications($accessUrl);
$auth_sources = 0; //make available wider as we need it in case of form reset (see below)
$nb_ext_auth_source_added = 0;
if (isset($extAuthSource) && count($extAuthSource) > 0) {
if (count($extAuthSource) > 0) {
$auth_sources = [];
foreach ($extAuthSource as $key => $info) {
foreach ($extAuthSource as $key) {
// @todo : make uniform external authentification configuration (ex : cas and external_login ldap)
// Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
// extAuthSource always on for CAS even if not activated
// same action for file user_edit.php
if ((CAS_AUTH_SOURCE == $key && 'true' === api_get_setting('cas_activate')) || (CAS_AUTH_SOURCE != $key)) {
if ((UserAuthSource::CAS == $key && 'true' === api_get_setting('cas_activate')) || (UserAuthSource::CAS != $key)) {
$auth_sources[$key] = $key;
$nb_ext_auth_source_added++;
}
}
if ($nb_ext_auth_source_added > 0) {
$group[] = $form->createElement('radio', 'password_auto', null, get_lang('External authentification').' ', 2);
$group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
$group[] = $form->createElement('select', 'auth_source', null, $auth_sources, ['multiple' => 'multiple']);
$group[] = $form->createElement('static', '', '', '<br />');
}
}
Expand Down Expand Up @@ -366,7 +377,7 @@ function updateStatus(){
$auth_source = $user['password']['auth_source'];
$password = 'PLACEHOLDER';
} else {
$auth_source = PLATFORM_AUTH_SOURCE;
$auth_source = [UserAuthSource::PLATFORM];
$password = '1' === $user['password']['password_auto'] ? api_generate_password() : $user['password']['password'];
}

Expand Down
36 changes: 26 additions & 10 deletions public/main/admin/user_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Entity\UserAuthSource;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
use Chamilo\CoreBundle\ServiceHelper\AuthenticationConfigHelper;
use ChamiloSession as Session;
use Chamilo\CoreBundle\Component\Utils\ActionIcon;

Expand All @@ -21,6 +25,12 @@
$userObj = api_get_user_entity($user_id);
$illustrationRepo = Container::getIllustrationRepository();

/** @var AuthenticationConfigHelper $authenticationConfigHelper */
$authenticationConfigHelper = Container::$container->get(AuthenticationConfigHelper::class);

/** @var AccessUrl $accessUrl */
$accessUrl = Container::$container->get(AccessUrlHelper::class)->getCurrent();

$htmlHeadXtra[] = '
<script>
var is_platform_id = "'.$is_platform_admin.'";
Expand Down Expand Up @@ -192,33 +202,37 @@ function confirmation(name) {
$form->addRule('username', get_lang('This login is already in use'), 'username_available', $user_data['username']);
}

if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
$form->addLabel(
get_lang('External authentification'),
$userInfo['auth_source']
);
$extAuthSource = $authenticationConfigHelper->getAuthSourceAuthentications($accessUrl);

if (!empty($extAuthSource) && count($extAuthSource) > 0) {
foreach ($userInfo['auth_sources'] as $userAuthSource) {
$form->addLabel(
get_lang('External authentification'),
$userAuthSource
);
}
}

// Password
$form->addElement('radio', 'reset_password', get_lang('Password'), get_lang('Don\'t reset password'), 0);
$nb_ext_auth_source_added = 0;
if (isset($extAuthSource) && !empty($extAuthSource) && count($extAuthSource) > 0) {
if (!empty($extAuthSource) && count($extAuthSource) > 0) {
$auth_sources = [];
foreach ($extAuthSource as $key => $info) {
foreach ($extAuthSource as $key) {
// @todo : make uniform external authentication configuration (ex : cas and external_login ldap)
// Special case for CAS. CAS is activated from Chamilo > Administration > Configuration > CAS
// extAuthSource always on for CAS even if not activated
// same action for file user_add.php
if ((CAS_AUTH_SOURCE == $key && 'true' === api_get_setting('cas_activate')) || (CAS_AUTH_SOURCE != $key)) {
if ((UserAuthSource::CAS == $key && 'true' === api_get_setting('cas_activate')) || (UserAuthSource::CAS != $key)) {
$auth_sources[$key] = $key;
$nb_ext_auth_source_added++;
}
}
if ($nb_ext_auth_source_added > 0) {
// @todo check the radio button for external authentification and select the external authentication in the menu
$group[] = $form->createElement('radio', 'reset_password', null, get_lang('External authentification').' ', 3);
$group[] = $form->createElement('select', 'auth_source', null, $auth_sources);
$group[] = $form->createElement('static', '', '', '<br />');
$group[] = $form->createElement('select', 'auth_source', null, $auth_sources, ['multiple' => 'multiple']);
$group[] = $form->createElement('static', '', '', '<br />', []);
$form->addGroup($group, 'password', null, null, false);
}
}
Expand Down Expand Up @@ -380,6 +394,8 @@ function confirmation(name) {

// Set default values
$user_data['reset_password'] = 0;
$user_data['auth_source'] = $userInfo['auth_sources'];

if (!$hideFields) {
$expiration_date = $user_data['expiration_date'];
if (empty($expiration_date)) {
Expand Down
17 changes: 13 additions & 4 deletions public/main/admin/user_export.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;

$cidReset = true;

require_once __DIR__.'/../inc/global.inc.php';
Expand All @@ -18,6 +22,9 @@
$tool_name = get_lang('Export users list');
$interbreadcrumb[] = ["url" => 'index.php', "name" => get_lang('Administration')];

/** @var AccessUrl $accessUrl */
$accessUrl = Container::$container->get(AccessUrlHelper::class)->getCurrent();

set_time_limit(0);
$coursesSessions = [];
$coursesSessions[''] = '--';
Expand Down Expand Up @@ -87,12 +94,8 @@

$sql = "SELECT
u.id AS UserId,
u.lastname AS LastName,
u.firstname AS FirstName,
u.email AS Email,
u.username AS UserName,
".(('none' != api_get_configuration_value('password_encryption')) ? " " : "u.password AS Password, ")."
u.auth_source AS AuthSource,
u.status AS Status,
u.official_code AS OfficialCode,
u.phone AS Phone,
Expand Down Expand Up @@ -171,6 +174,12 @@

$res = Database::query($sql);
while ($user = Database::fetch_assoc($res)) {
$userEntity = api_get_user_entity($user['UserId']);
$user['LastName'] = $userEntity->getLastname();
$user['FirstName'] = $userEntity->getFirstname();
$user['UserName'] = $userEntity->getUsername();
$user['AuthSource'] = implode(', ', $userEntity->getAuthSourcesAuthentications($accessUrl));

$student_data = UserManager:: get_extra_user_data(
$user['UserId'],
true,
Expand Down
7 changes: 4 additions & 3 deletions public/main/admin/user_import.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
use Chamilo\CoreBundle\Entity\UserAuthSource;
use ChamiloSession as Session;

/**
Expand Down Expand Up @@ -163,7 +164,7 @@ function complete_missing_data($user)
}
// 4. Set authsource if not allready set.
if (empty($user['AuthSource'])) {
$user['AuthSource'] = PLATFORM_AUTH_SOURCE;
$user['AuthSource'] = UserAuthSource::PLATFORM;
}

if (empty($user['ExpiryDate'])) {
Expand Down Expand Up @@ -233,7 +234,7 @@ function save_data($users, $sendMail = false)
$user['language'],
$user['PhoneNumber'],
'',
$user['AuthSource'],
[$user['AuthSource']],
$user['ExpiryDate'],
1,
0,
Expand Down Expand Up @@ -494,7 +495,7 @@ function processUsers(&$users, $sendMail)
}

$this_section = SECTION_PLATFORM_ADMIN;
$defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
$defined_auth_sources[] = UserAuthSource::PLATFORM;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable "defined_auth_sources" is not in valid camel caps format

if (isset($extAuthSource) && is_array($extAuthSource)) {
$defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
}
Expand Down
9 changes: 5 additions & 4 deletions public/main/admin/user_update_import.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* This tool allows platform admins to add users by uploading a CSV or XML file.
*/

use Chamilo\CoreBundle\Entity\UserAuthSource;
use Symfony\Component\DomCrawler\Crawler;

$cidReset = true;
Expand Down Expand Up @@ -112,7 +113,7 @@ function updateUsers(
$userName = isset($user['NewUserName']) ? $user['NewUserName'] : $userInfo['username'];
$changePassMethod = 0;
$password = null;
$authSource = $userInfo['auth_source'];
$authSource = $userInfo['auth_sources'];

if ($resetPassword) {
$changePassMethod = 1;
Expand All @@ -122,8 +123,8 @@ function updateUsers(
$password = $user['Password'];
}

if (isset($user['AuthSource']) && $user['AuthSource'] != $authSource) {
$authSource = $user['AuthSource'];
if (isset($user['AuthSource']) && !in_array($user['AuthSource'], $authSource)) {
$authSource = [$user['AuthSource']];
$changePassMethod = 3;
}
}
Expand Down Expand Up @@ -267,7 +268,7 @@ function parse_xml_data($file)
$this_section = SECTION_PLATFORM_ADMIN;
api_protect_admin_script(true, null);

$defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
$defined_auth_sources[] = UserAuthSource::PLATFORM;
if (isset($extAuthSource) && is_array($extAuthSource)) {
$defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
}
Expand Down
3 changes: 2 additions & 1 deletion public/main/auth/inscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Entity\UserAuthSource;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\ServiceHelper\ContainerHelper;
use ChamiloSession as Session;
Expand Down Expand Up @@ -928,7 +929,7 @@
$values['language'],
$phone,
null,
PLATFORM_AUTH_SOURCE,
[UserAuthSource::PLATFORM],
null,
1,
0,
Expand Down
5 changes: 4 additions & 1 deletion public/main/auth/lostPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*
* @todo refactor, move relevant functions to code libraries
*/

use Chamilo\CoreBundle\Entity\UserAuthSource;

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

// Custom pages
Expand Down Expand Up @@ -118,7 +121,7 @@
exit;
}

if ('extldap' === $user['auth_source']) {
if (in_array(UserAuthSource::CAS, $user['auth_sources'])) {
Display::addFlash(
Display::return_message(get_lang('Could not reset password, contact your helpdesk.'), 'info', false)
);
Expand Down
5 changes: 3 additions & 2 deletions public/main/auth/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Entity\UserAuthSource;
use ChamiloSession as Session;
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
use Chamilo\CoreBundle\Component\Utils\ToolIcon;
Expand Down Expand Up @@ -299,7 +300,7 @@ function show_image(image,width,height) {
}

// PASSWORD, if auth_source is platform
if (PLATFORM_AUTH_SOURCE == $user_data['auth_source'] &&
if (in_array(UserAuthSource::PLATFORM, $user_data['auth_sources']) &&
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable "user_data" is not in valid camel caps format

in_array('password', $profileList)
) {
$form->addElement('password', 'password0', [get_lang('Pass'), get_lang('Enter2passToChange')], ['size' => 40]);
Expand Down Expand Up @@ -402,7 +403,7 @@ function show_image(image,width,height) {
}

$allow_users_to_change_email_with_no_password = true;
if (isset($user_data['auth_source']) && PLATFORM_AUTH_SOURCE == $user_data['auth_source'] &&
if (isset($user_data['auth_sources']) && in_array(UserAuthSource::PLATFORM, $user_data['auth_sources']) &&
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable "user_data" is not in valid camel caps format

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable "user_data" is not in valid camel caps format

'false' === api_get_setting('allow_users_to_change_email_with_no_password')
) {
$allow_users_to_change_email_with_no_password = false;
Expand Down
Loading
Loading