function UserRegistrationResource::post

Same name and namespace in other branches
  1. 9 core/modules/user/src/Plugin/rest/resource/UserRegistrationResource.php \Drupal\user\Plugin\rest\resource\UserRegistrationResource::post()
  2. 8.9.x core/modules/user/src/Plugin/rest/resource/UserRegistrationResource.php \Drupal\user\Plugin\rest\resource\UserRegistrationResource::post()
  3. 11.x core/modules/user/src/Plugin/rest/resource/UserRegistrationResource.php \Drupal\user\Plugin\rest\resource\UserRegistrationResource::post()

Responds to user registration POST request.

Parameters

\Drupal\user\UserInterface $account: The user account entity.

Return value

\Drupal\rest\ModifiedResourceResponse The HTTP response object.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException

File

core/modules/user/src/Plugin/rest/resource/UserRegistrationResource.php, line 119

Class

UserRegistrationResource
Represents user registration as a resource.

Namespace

Drupal\user\Plugin\rest\resource

Code

public function post(?UserInterface $account = NULL) {
  $this->ensureAccountCanRegister($account);
  // Only activate new users if visitors are allowed to register.
  if ($this->userSettings
    ->get('register') == UserInterface::REGISTER_VISITORS) {
    $account->activate();
  }
  else {
    $account->block();
  }
  // Generate password if email verification required.
  if ($this->userSettings
    ->get('verify_mail')) {
    $account->setPassword($this->passwordGenerator
      ->generate());
  }
  $this->checkEditFieldAccess($account);
  // Make sure that the user entity is valid (email and name are valid).
  $this->validate($account);
  // Create the account.
  $account->save();
  $this->sendEmailNotifications($account);
  return new ModifiedResourceResponse($account, 200);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.