From ab8510e28f68c652b23d600dd2063c6c187de46a Mon Sep 17 00:00:00 2001 From: David Snopek Date: Thu, 11 Jan 2018 08:54:57 -0600 Subject: [PATCH] CRM-21652: Fix numerous problems with anonymous users registering themselves --- CRM/Utils/System/Drupal8.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/CRM/Utils/System/Drupal8.php b/CRM/Utils/System/Drupal8.php index 6589b2e41d..95b317b74f 100644 --- a/CRM/Utils/System/Drupal8.php +++ b/CRM/Utils/System/Drupal8.php @@ -49,6 +49,7 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { return FALSE; } + /** @var \Drupal\user\Entity\User $account */ $account = entity_create('user'); $account->setUsername($params['cms_name'])->setEmail($params[$mail]); @@ -64,6 +65,9 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { if ($user_register_conf != 'visitors' && !$user->hasPermission('administer users')) { $account->block(); } + elseif (!$verify_mail_conf) { + $account->activate(); + } // Validate the user object $violations = $account->validate(); @@ -71,12 +75,19 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { return FALSE; } + // Let the Drupal module know we're already in CiviCRM. + $config = CRM_Core_Config::singleton(); + $config->inCiviCRM = TRUE; + try { $account->save(); } catch (\Drupal\Core\Entity\EntityStorageException $e) { return FALSE; } + finally { + $config->inCiviCRM = TRUE; + } // Send off any emails as required. // Possible values for $op: @@ -100,6 +111,11 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { break; } + // If this is a user creating their own account, login them in! + if ($account->isActive() && $user->isAnonymous()) { + \user_login_finalize($account); + } + return $account->id(); } @@ -139,10 +155,10 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { $violations = iterator_to_array($user->validate()); // We only care about violations on the username field; discard the rest. $violations = array_filter($violations, function ($v) { - return $v->getPropertyPath() == 'name.0.value'; + return $v->getPropertyPath() == 'name'; }); if (count($violations) > 0) { - $errors['cms_name'] = $violations[0]->getMessage(); + $errors['cms_name'] = (string) $violations[0]->getMessage(); } } @@ -157,10 +173,10 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { $violations = iterator_to_array($user->validate()); // We only care about violations on the email field; discard the rest. $violations = array_filter($violations, function ($v) { - return $v->getPropertyPath() == 'mail.0.value'; + return $v->getPropertyPath() == 'mail'; }); if (count($violations) > 0) { - $errors[$emailName] = $violations[0]->getMessage(); + $errors[$emailName] = (string) $violations[0]->getMessage(); } } } -- 2.25.1