UserRegistrationResource.php in Drupal 10
File
core/modules/user/src/Plugin/rest/resource/UserRegistrationResource.php
View source
<?php
namespace Drupal\user\Plugin\rest\resource;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Session\AccountInterface;
use Drupal\rest\ModifiedResourceResponse;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\Plugin\rest\resource\EntityResourceAccessTrait;
use Drupal\rest\Plugin\rest\resource\EntityResourceValidationTrait;
use Drupal\user\UserInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class UserRegistrationResource extends ResourceBase {
use EntityResourceValidationTrait;
use EntityResourceAccessTrait;
protected $userSettings;
protected $currentUser;
public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, ImmutableConfig $user_settings, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->userSettings = $user_settings;
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->getParameter('serializer.formats'), $container
->get('logger.factory')
->get('rest'), $container
->get('config.factory')
->get('user.settings'), $container
->get('current_user'));
}
public function post(UserInterface $account = NULL) {
$this
->ensureAccountCanRegister($account);
if ($this->userSettings
->get('register') == UserInterface::REGISTER_VISITORS && !$this->userSettings
->get('verify_mail')) {
$account
->activate();
}
else {
$account
->block();
}
$this
->checkEditFieldAccess($account);
$this
->validate($account);
$account
->save();
$this
->sendEmailNotifications($account);
return new ModifiedResourceResponse($account, 200);
}
protected function ensureAccountCanRegister(UserInterface $account = NULL) {
if ($account === NULL) {
throw new BadRequestHttpException('No user account data for registration received.');
}
if (!$account
->isNew()) {
throw new BadRequestHttpException('An ID has been set and only new user accounts can be registered.');
}
if (!$this->currentUser
->isAnonymous()) {
throw new AccessDeniedHttpException('Only anonymous users can register a user.');
}
if ($this->userSettings
->get('register') == UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
throw new AccessDeniedHttpException('You cannot register a new user account.');
}
if (!$this->userSettings
->get('verify_mail')) {
if (empty($account
->getPassword())) {
throw new UnprocessableEntityHttpException('No password provided.');
}
}
else {
if (!empty($account
->getPassword())) {
throw new UnprocessableEntityHttpException('A Password cannot be specified. It will be generated on login.');
}
}
}
protected function sendEmailNotifications(UserInterface $account) {
$approval_settings = $this->userSettings
->get('register');
if ($approval_settings == UserInterface::REGISTER_VISITORS) {
if ($this->userSettings
->get('verify_mail')) {
_user_mail_notify('register_no_approval_required', $account);
}
}
elseif ($approval_settings == UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) {
_user_mail_notify('register_pending_approval', $account);
}
}
}