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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions public/main/inc/lib/api.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

La nueva función debería ir ChamiloHelper (quizás) para dejar de usar código legacy 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sì, es usado por ahora en inscription.php con formvalidator como una regla para el correo, por eso esta como api en legacy, se podria agregar luego con symfony para usarlo en un FormType cuando se cambie la pagina de inscripcion a modo vue/symfony , quedaria como referencia.

{
$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();
Comment on lines +7596 to +7603
Copy link
Member

Choose a reason for hiding this comment

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

Debería estar como una función en UserRepository para no usar SQL directamente


return $count >= $limit;
}

5 changes: 5 additions & 0 deletions src/CoreBundle/DataFixtures/SettingsCurrentFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 9 additions & 0 deletions src/CoreBundle/Settings/PlatformSettingsSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
Loading