Skip to content

Internal: Add setting to limit number of same e-mails - refs #5112 #6381

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

Merged
merged 3 commits into from
Jul 10, 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
9 changes: 9 additions & 0 deletions public/main/auth/inscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@

$form->addEmailRule('email');

$form->addRule(
'email',
get_lang('This e-mail address has already been used by the maximum number of allowed accounts. Please use another.'),
'callback',
function ($email) {
return !api_email_reached_registration_limit($email);
}
);

// USERNAME
if ('true' != api_get_setting('login_is_email')) {
$form->addText(
Expand Down
18 changes: 18 additions & 0 deletions public/main/inc/lib/api.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -7574,3 +7574,21 @@ function api_calculate_increment_percent(int $newValue, int $oldValue): string
}
return $result;
}

/**
* @todo Move to UserRegistrationHelper when migrating inscription.php to Symfony
*/
function api_email_reached_registration_limit(string $email): bool
{
$limit = (int) api_get_setting('platform.hosting_limit_identical_email');

if ($limit <= 0 || empty($email)) {
return false;
}

$repo = Container::getUserRepository();
$count = $repo->countUsersByEmail($email);

return $count >= $limit;
}

3 changes: 3 additions & 0 deletions public/main/inc/lib/internationalization.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ function api_get_timezone(): string
Session::write('system_timezone', $timezone);
}

// Replace backslashes by forward slashes in case of wrong timezone format
$timezone = str_replace('\\', '/', $timezone);

return $timezone;
}

Expand Down
5 changes: 5 additions & 0 deletions src/CoreBundle/DataFixtures/SettingsCurrentFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,11 @@ public static function getNewConfigurationSettings(): array
],
],
'platform' => [
[
'name' => 'hosting_limit_identical_email',
'title' => 'Limit identical email usage',
'comment' => 'Maximum number of accounts allowed to share the same e-mail address. Set to 0 to disable this limit.',
],
[
'name' => 'allow_double_validation_in_registration',
'title' => 'Double validation for registration process',
Expand Down
87 changes: 87 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20250709201100.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

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

declare(strict_types=1);

namespace Chamilo\CoreBundle\Migrations\Schema\V200;

use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;

final class Version20250709201100 extends AbstractMigrationChamilo
{
public function getDescription(): string
{
return 'Insert or update the platform setting for hosting_limit_identical_email.';
}

public function up(Schema $schema): void
{
$setting = [
'variable' => 'hosting_limit_identical_email',
'selected_value' => '0',
'title' => 'Limit identical email usage',
'comment' => 'Maximum number of accounts allowed to share the same e-mail address. Set to 0 to disable this limit.',
'category' => 'platform',
];

$sqlCheck = sprintf(
"SELECT COUNT(*) as count
FROM settings
WHERE variable = '%s'
AND subkey IS NULL
AND access_url = 1",
addslashes($setting['variable'])
);

$stmt = $this->connection->executeQuery($sqlCheck);
$result = $stmt->fetchAssociative();

if ($result && (int)$result['count'] > 0) {
// UPDATE existing setting
$this->addSql(sprintf(
"UPDATE settings
SET selected_value = '%s',
title = '%s',
comment = '%s',
category = '%s'
WHERE variable = '%s'
AND subkey IS NULL
AND access_url = 1",
addslashes($setting['selected_value']),
addslashes($setting['title']),
addslashes($setting['comment']),
addslashes($setting['category']),
addslashes($setting['variable'])
));
$this->write(sprintf("Updated setting: %s", $setting['variable']));
} else {
// INSERT new setting
$this->addSql(sprintf(
"INSERT INTO settings
(variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url)
VALUES
('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)",
addslashes($setting['variable']),
addslashes($setting['category']),
addslashes($setting['selected_value']),
addslashes($setting['title']),
addslashes($setting['comment'])
));
$this->write(sprintf("Inserted setting: %s", $setting['variable']));
}
}

public function down(Schema $schema): void
{
$this->addSql("
DELETE FROM settings
WHERE variable = 'hosting_limit_identical_email'
AND subkey IS NULL
AND access_url = 1
");

$this->write("Removed setting: hosting_limit_identical_email.");
}
}
17 changes: 17 additions & 0 deletions src/CoreBundle/Repository/Node/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -1270,4 +1270,21 @@ public function findUsersForSessionAdmin(

return $qb->getQuery()->getResult();
}

/**
* Returns the number of users registered with a given email.
*
* @param string $email
*
* @return int
*/
public function countUsersByEmail(string $email): int
{
return (int) $this->createQueryBuilder('u')
->select('COUNT(u.id)')
->where('u.email = :email')
->setParameter('email', $email)
->getQuery()
->getSingleScalarResult();
}
}
13 changes: 12 additions & 1 deletion src/CoreBundle/Settings/PlatformSettingsSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function buildSettings(AbstractSettingsBuilder $builder): void
'show_tabs_per_role' => '{}',
'session_admin_user_subscription_search_extra_field_to_search' => '',
'push_notification_settings' => '',
'hosting_limit_identical_email' => '0',
]
)
->setTransformer(
Expand Down Expand Up @@ -161,6 +162,7 @@ public function buildForm(FormBuilderInterface $builder): void
->add('use_custom_pages', YesNoType::class)
->add('pdf_logo_header')
->add('allow_my_files', YesNoType::class)
// old settings with no category
->add('chamilo_database_version')
->add(
'load_term_conditions_section',
Expand Down Expand Up @@ -247,7 +249,16 @@ public function buildForm(FormBuilderInterface $builder): void
'help' => 'User extra field key to use when searching and naming sessions from /admin-dashboard/register.',
]
)
->add('push_notification_settings', TextareaType::class);
->add('push_notification_settings', TextareaType::class)
->add(
'hosting_limit_identical_email',
TextType::class,
[
'label' => 'Limit identical emails',
'help' => 'Maximum number of accounts allowed with the same email. Set to 0 to disable limit.'
]
)
;

$this->updateFormFieldsFromSettingsInfo($builder);
}
Expand Down
Loading