Merge pull request #12926 from annando/issue-11535

Issue 11535: Automatically open and close the registration
This commit is contained in:
Hypolite Petovan 2023-03-22 09:08:01 -04:00 committed by GitHub
commit 9a5e4a93fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 334 additions and 282 deletions

View File

@ -28,8 +28,8 @@ use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\GServer; use Friendica\Model\GServer;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Model\User;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Delivery;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings; use Friendica\Util\Strings;
@ -65,6 +65,9 @@ class Cron
// Directly deliver or requeue posts to other systems // Directly deliver or requeue posts to other systems
self::deliverPosts(); self::deliverPosts();
// Automatically open/close the registration based on the user count
User::setRegisterMethodByUserCount();
} }
/** /**

View File

@ -35,6 +35,7 @@ use Friendica\Core\System;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Module\Register;
use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Security\TwoFactor\Model\AppSpecificPassword; use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
@ -1266,6 +1267,8 @@ class User
Hook::callAll('register_account', $uid); Hook::callAll('register_account', $uid);
self::setRegisterMethodByUserCount();
$return['user'] = $user; $return['user'] = $user;
return $return; return $return;
} }
@ -1610,6 +1613,7 @@ class User
// Remove the user relevant data // Remove the user relevant data
Worker::add(Worker::PRIORITY_NEGLIGIBLE, 'RemoveUser', $uid); Worker::add(Worker::PRIORITY_NEGLIGIBLE, 'RemoveUser', $uid);
self::setRegisterMethodByUserCount();
return true; return true;
} }
@ -1877,4 +1881,29 @@ class User
return true; return true;
}); });
} }
public static function setRegisterMethodByUserCount()
{
$max_registered_users = DI::config()->get('config', 'max_registered_users');
if ($max_registered_users <= 0) {
return;
}
$register_policy = DI::config()->get('config', 'register_policy');
if (!in_array($register_policy, [Register::OPEN, Register::CLOSED])) {
Logger::debug('Unsupported register policy.', ['policy' => $register_policy]);
return;
}
$users = DBA::count('user', ['blocked' => false, 'account_removed' => false, 'account_expired' => false]);
if (($users >= $max_registered_users) && ($register_policy == Register::OPEN)) {
DI::config()->set('config', 'register_policy', Register::CLOSED);
Logger::notice('Max users reached, registration is closed.', ['users' => $users, 'max' => $max_registered_users]);
} elseif (($users < $max_registered_users) && ($register_policy == Register::CLOSED)) {
DI::config()->set('config', 'register_policy', Register::OPEN);
Logger::notice('Below maximum users, registration is opened.', ['users' => $users, 'max' => $max_registered_users]);
} else {
Logger::debug('Unchanged register policy', ['policy' => $register_policy, 'users' => $users, 'max' => $max_registered_users]);
}
}
} }

View File

@ -72,6 +72,7 @@ class Site extends BaseAdmin
$jpegimagequality = (!empty($_POST['jpegimagequality']) ? intval(trim($_POST['jpegimagequality'])) : 100); $jpegimagequality = (!empty($_POST['jpegimagequality']) ? intval(trim($_POST['jpegimagequality'])) : 100);
$register_policy = (!empty($_POST['register_policy']) ? intval(trim($_POST['register_policy'])) : 0); $register_policy = (!empty($_POST['register_policy']) ? intval(trim($_POST['register_policy'])) : 0);
$max_registered_users = (!empty($_POST['max_registered_users']) ? intval(trim($_POST['max_registered_users'])) : 0);
$daily_registrations = (!empty($_POST['max_daily_registrations']) ? intval(trim($_POST['max_daily_registrations'])) : 0); $daily_registrations = (!empty($_POST['max_daily_registrations']) ? intval(trim($_POST['max_daily_registrations'])) : 0);
$abandon_days = (!empty($_POST['abandon_days']) ? intval(trim($_POST['abandon_days'])) : 0); $abandon_days = (!empty($_POST['abandon_days']) ? intval(trim($_POST['abandon_days'])) : 0);
@ -210,7 +211,11 @@ class Site extends BaseAdmin
$transactionConfig->set('system', 'jpeg_quality' , $jpegimagequality); $transactionConfig->set('system', 'jpeg_quality' , $jpegimagequality);
$transactionConfig->set('config', 'register_policy' , $register_policy); $transactionConfig->set('config', 'register_policy' , $register_policy);
$transactionConfig->set('config', 'max_registered_users' , $max_registered_users);
$transactionConfig->set('system', 'max_daily_registrations', $daily_registrations); $transactionConfig->set('system', 'max_daily_registrations', $daily_registrations);
User::setRegisterMethodByUserCount();
$transactionConfig->set('system', 'account_abandon_days' , $abandon_days); $transactionConfig->set('system', 'account_abandon_days' , $abandon_days);
$transactionConfig->set('config', 'register_text' , $register_text); $transactionConfig->set('config', 'register_text' , $register_text);
$transactionConfig->set('system', 'allowed_sites' , $allowed_sites); $transactionConfig->set('system', 'allowed_sites' , $allowed_sites);
@ -431,6 +436,7 @@ class Site extends BaseAdmin
'$jpegimagequality' => ['jpegimagequality', DI::l10n()->t('JPEG image quality'), DI::config()->get('system', 'jpeg_quality'), DI::l10n()->t('Uploaded JPEGS will be saved at this quality setting [0-100]. Default is 100, which is full quality.')], '$jpegimagequality' => ['jpegimagequality', DI::l10n()->t('JPEG image quality'), DI::config()->get('system', 'jpeg_quality'), DI::l10n()->t('Uploaded JPEGS will be saved at this quality setting [0-100]. Default is 100, which is full quality.')],
'$register_policy' => ['register_policy', DI::l10n()->t('Register policy'), DI::config()->get('config', 'register_policy'), '', $register_choices], '$register_policy' => ['register_policy', DI::l10n()->t('Register policy'), DI::config()->get('config', 'register_policy'), '', $register_choices],
'$max_registered_users' => ['max_registered_users', DI::l10n()->t('Maximum Users'), DI::config()->get('config', 'max_registered_users'), DI::l10n()->t('If defined, the register policy is automatically closed when the given number of users is reached and reopens the registry when the number drops below the limit. It only works when the policy is set to open or close, but not when the policy is set to approval.')],
'$daily_registrations' => ['max_daily_registrations', DI::l10n()->t('Maximum Daily Registrations'), DI::config()->get('system', 'max_daily_registrations'), DI::l10n()->t('If registration is permitted above, this sets the maximum number of new user registrations to accept per day. If register is set to closed, this setting has no effect.')], '$daily_registrations' => ['max_daily_registrations', DI::l10n()->t('Maximum Daily Registrations'), DI::config()->get('system', 'max_daily_registrations'), DI::l10n()->t('If registration is permitted above, this sets the maximum number of new user registrations to accept per day. If register is set to closed, this setting has no effect.')],
'$register_text' => ['register_text', DI::l10n()->t('Register text'), DI::config()->get('config', 'register_text'), DI::l10n()->t('Will be displayed prominently on the registration page. You can use BBCode here.')], '$register_text' => ['register_text', DI::l10n()->t('Register text'), DI::config()->get('config', 'register_text'), DI::l10n()->t('Will be displayed prominently on the registration page. You can use BBCode here.')],
'$forbidden_nicknames' => ['forbidden_nicknames', DI::l10n()->t('Forbidden Nicknames'), DI::config()->get('system', 'forbidden_nicknames'), DI::l10n()->t('Comma separated list of nicknames that are forbidden from registration. Preset is a list of role names according RFC 2142.')], '$forbidden_nicknames' => ['forbidden_nicknames', DI::l10n()->t('Forbidden Nicknames'), DI::config()->get('system', 'forbidden_nicknames'), DI::l10n()->t('Comma separated list of nicknames that are forbidden from registration. Preset is a list of role names according RFC 2142.')],

File diff suppressed because it is too large Load Diff

View File

@ -32,6 +32,7 @@
<h2>{{$registration}}</h2> <h2>{{$registration}}</h2>
{{include file="field_textarea.tpl" field=$register_text}} {{include file="field_textarea.tpl" field=$register_text}}
{{include file="field_select.tpl" field=$register_policy}} {{include file="field_select.tpl" field=$register_policy}}
{{include file="field_input.tpl" field=$max_registered_users}}
{{include file="field_input.tpl" field=$daily_registrations}} {{include file="field_input.tpl" field=$daily_registrations}}
{{include file="field_checkbox.tpl" field=$enable_multi_reg}} {{include file="field_checkbox.tpl" field=$enable_multi_reg}}
{{include file="field_checkbox.tpl" field=$enable_openid}} {{include file="field_checkbox.tpl" field=$enable_openid}}

View File

@ -71,6 +71,7 @@
<div class="panel-body"> <div class="panel-body">
{{include file="field_textarea.tpl" field=$register_text}} {{include file="field_textarea.tpl" field=$register_text}}
{{include file="field_select.tpl" field=$register_policy}} {{include file="field_select.tpl" field=$register_policy}}
{{include file="field_input.tpl" field=$max_registered_users}}
{{include file="field_input.tpl" field=$daily_registrations}} {{include file="field_input.tpl" field=$daily_registrations}}
{{include file="field_checkbox.tpl" field=$enable_multi_reg}} {{include file="field_checkbox.tpl" field=$enable_multi_reg}}
{{include file="field_checkbox.tpl" field=$enable_openid}} {{include file="field_checkbox.tpl" field=$enable_openid}}