15 use Symfony\Component\Mime\Address;
59 $error = $this->translator->translate(
'registration:noname');
69 }
catch (RegistrationException $ex) {
77 }
catch (RegistrationException $ex) {
85 }
catch (RegistrationException $ex) {
143 $constructor = \ElggUser::class;
152 $user =
new $constructor();
163 if (!
$user->save()) {
164 throw new RegistrationException($this->translator->translate(
'registerbad'));
171 $user->setNotificationSetting(
'email',
true);
174 $user->setValidationStatus(
true,
'on_create');
193 $msg = $this->translator->translate(
'registration:usernametooshort', [$this->config->minusername]);
199 $msg = $this->translator->translate(
'registration:usernametoolong', [128]);
208 if (preg_match_all(
'/[^\p{L}\p{M}\p{Nd}._-]+/iu',
$username, $invalid_chars)) {
209 throw new RegistrationException($this->translator->translate(
'registration:invalidchars:route', [implode(
',', $invalid_chars[0])]));
214 $blacklist2 =
'\'/\\
"*& ?#%^(){}[]~?<>;|¬`@+=,:';
216 $blacklist2 = $this->events->triggerResults(
217 'username:character_blacklist',
219 ['blacklist' => $blacklist2],
223 for ($n = 0; $n < elgg_strlen($blacklist2); $n++) {
224 if (elgg_strpos($username, $blacklist2[$n]) !== false) {
225 $msg = $this->translator->translate('registration:invalidchars', [$blacklist2[$n], $blacklist2]);
226 $msg = htmlspecialchars($msg, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
227 throw new RegistrationException($msg);
231 $result = $this->events->triggerResults(
232 'registeruser:validate:username',
234 ['username' => $username],
239 throw new RegistrationException($this->translator->translate('registration:usernamenotvalid'));
242 if ($assert_unregistered) {
243 $exists = elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_SHOW_DELETED_ENTITIES, function () use ($username) {
244 return elgg_get_user_by_username($username);
247 if ($exists instanceof \ElggUser) {
248 throw new RegistrationException($this->translator->translate('registration:userexists'));
262 public function assertValidPassword(string|array $password): void {
263 if (is_array($password)) {
264 list($password, $password2) = $password;
266 if (empty($password) || empty($password2)) {
267 throw new RegistrationException(elgg_echo('RegistrationException:EmptyPassword'));
270 if (strcmp($password, $password2) != 0) {
271 throw new RegistrationException(elgg_echo('RegistrationException:PasswordMismatch'));
275 $result = $this->events->triggerResults(
276 'registeruser:validate:password',
278 ['password' => $password],
283 throw new RegistrationException($this->translator->translate('registration:passwordnotvalid'));
296 public function assertCurrentPassword(\ElggUser $user, string $password): void {
297 if (!$this->passwords->verify($password, $user->password_hash)) {
298 throw new RegistrationException($this->translator->translate('LoginException:PasswordFailure'));
311 public function assertValidEmail(string $address, bool $assert_unregistered = false): void {
312 if (!$this->isValidEmail($address)) {
313 throw new RegistrationException($this->translator->translate('registration:notemail'));
316 $result = $this->events->triggerResults(
317 'registeruser:validate:email',
319 ['email' => $address],
324 throw new RegistrationException($this->translator->translate('registration:emailnotvalid'));
327 if ($assert_unregistered) {
328 $exists = elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_SHOW_DELETED_ENTITIES, function () use ($address) {
329 return elgg_get_user_by_email($address);
332 if ($exists instanceof \ElggUser) {
333 throw new RegistrationException($this->translator->translate('registration:dupeemail'));
345 public function isValidEmail(string $address): bool {
346 return filter_var($address, FILTER_VALIDATE_EMAIL) === $address;
358 public function requestNewEmailValidation(\ElggUser $user, string $email): bool {
359 if (!$this->isValidEmail($email)) {
360 throw new InvalidArgumentException($this->translator->translate('registration:notemail'));
363 $site = elgg_get_site_entity();
365 $user->new_email = $email;
367 $url = elgg_generate_url('account:email:confirm', [
368 'guid' => $user->guid,
370 $url = elgg_http_get_signed_url($url, '+1 hour');
372 $notification = Email::factory([
374 'to' => new Address($email, $user->getDisplayName()),
375 'subject' => $this->translator->translate('email:request:email:subject', [], $user->getLanguage()),
376 'body' => $this->translator->translate('email:request:email:body', [
377 $site->getDisplayName(),
379 ], $user->getLanguage()),
382 return $this->email->send($notification);
393 public function registerAuthenticationFailure(\ElggUser $user): void {
394 $fails = (int) $user->authentication_failures;
397 $user->authentication_failures = $fails;
398 $user->{"authentication_failure_{$fails}
"} = time();
409 public function resetAuthenticationFailures(\ElggUser $user): void {
410 $fails = (int) $user->authentication_failures;
415 for ($n = 1; $n <= $fails; $n++) {
416 unset($user->{"authentication_failure_{$n}
"});
419 unset($user->authentication_failures);
432 public function isAuthenticationFailureLimitReached(\ElggUser $user, ?int $limit = null, ?int $lifetime = null): bool {
433 $limit = $limit ?? $this->config->authentication_failures_limit;
434 $lifetime = $lifetime ?? $this->config->authentication_failures_lifetime;
436 $fails = (int) $user->authentication_failures;
437 if (empty($fails) || $fails < $limit) {
442 $min_time = time() - $lifetime;
443 for ($n = $fails; $n > 0; $n--) {
444 $failure_timestamp = $user->{"authentication_failure_{$n}
"};
445 if ($failure_timestamp > $min_time) {
449 if ($failure_count === $limit) {
467 public function changePassword(\ElggUser $user, string $password): void {
468 $user->setPassword($password);
469 $this->persistentLogin->handlePasswordChange($user);
if(! $user||! $user->canDelete()) $name
$params
Saves global plugin settings.
$user
Elgg user account settings.
Could not register a new user for whatever reason.
Exception thrown if an argument is not of the expected type.
\Elgg\PersistentLoginService
assertValidUsername(string $username, bool $assert_unregistered=false)
Simple function which ensures that a username contains only valid characters.
validateAccountData(string $username, string|array $password, string $name, string $email, bool $allow_multiple_emails=false)
Validate registration details to ensure they can be used to register a new user account.
assertValidPassword(string|array $password)
Simple validation of a password.
assertValidAccountData(string $username, string|array $password, string $name, string $email, bool $allow_multiple_emails=false)
Assert that given registration details are valid and can be used to register the user.
__construct(protected Config $config, protected Translator $translator, protected PasswordService $passwords, protected EventsService $events, protected EmailService $email, protected PersistentLoginService $persistentLogin,)
Constructor.
assertValidEmail(string $address, bool $assert_unregistered=false)
Simple validation of an email.
Represents a set of validated parameters.
$config
Advanced site settings, debugging section.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
elgg_get_entity_class(string $type, string $subtype)
Return the class name registered as a constructor for an entity of a given type and subtype.
elgg_strlen()
Wrapper function for mb_strlen().
if(! $user||! $user->canEdit()) $password
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.