diff --git a/public/main/auth/inscription.php b/public/main/auth/inscription.php index 033acd93677..2de8d966730 100644 --- a/public/main/auth/inscription.php +++ b/public/main/auth/inscription.php @@ -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( diff --git a/public/main/inc/lib/api.lib.php b/public/main/inc/lib/api.lib.php index 5fab676931c..1a46cf52e0f 100644 --- a/public/main/inc/lib/api.lib.php +++ b/public/main/inc/lib/api.lib.php @@ -7577,3 +7577,31 @@ function api_calculate_increment_percent(int $newValue, int $oldValue): string } return $result; } + +/** + * Checks whether the number of accounts using the given email has reached the configured limit. + * + * @param string $email The email address to check. + * + * @return bool True if limit has been reached, false otherwise. + */ +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; // No limit or invalid email + } + + $sql = "SELECT COUNT(*) FROM user WHERE email = '".Database::escape_string($email)."'"; + $result = Database::query($sql); + + if (!$result) { + return false; + } + + $count = (int) $result->fetchOne(); + + return $count >= $limit; +} + diff --git a/src/CoreBundle/DataFixtures/SettingsCurrentFixtures.php b/src/CoreBundle/DataFixtures/SettingsCurrentFixtures.php index 3608e829c8f..7ab2f4ff41d 100644 --- a/src/CoreBundle/DataFixtures/SettingsCurrentFixtures.php +++ b/src/CoreBundle/DataFixtures/SettingsCurrentFixtures.php @@ -2002,6 +2002,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', diff --git a/src/CoreBundle/Settings/PlatformSettingsSchema.php b/src/CoreBundle/Settings/PlatformSettingsSchema.php index fcb62f64d0a..a59c8fbfdd4 100644 --- a/src/CoreBundle/Settings/PlatformSettingsSchema.php +++ b/src/CoreBundle/Settings/PlatformSettingsSchema.php @@ -93,6 +93,7 @@ public function buildSettings(AbstractSettingsBuilder $builder): void 'redirect_index_to_url_for_logged_users' => '', 'default_menu_entry_for_course_or_session' => 'my_courses', 'notification_event' => 'false', + 'hosting_limit_identical_email' => '0', ] ) ->setTransformer( @@ -282,6 +283,14 @@ public function buildForm(FormBuilderInterface $builder): void ] ) ->add('notification_event', YesNoType::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);