Commit 64d05d0e authored by Harsh Panchal's avatar Harsh Panchal Committed by Shaun Dychko
Browse files

Issue #3292381 by Harsh panchal, ShaunDychko: User::load calls should be...

Issue #3292381 by Harsh panchal, ShaunDychko: User::load calls should be avoided in classes, use dependency injection instead
parent 853c867e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ services:
      - { name: event_subscriber }
  decoupled_cookie_auth.modified_resource_response_subscriber:
    class: Drupal\decoupled_cookie_auth\EventSubscriber\ModifiedResourceResponseSubscriber
    arguments: ['@current_route_match', '@current_user', '@serialization.json', '@config.factory']
    arguments: ['@current_route_match', '@current_user', '@serialization.json', '@config.factory', '@entity_type.manager']
    tags:
      - { name: event_subscriber }
  decoupled_cookie_auth.request_subscriber:
+12 −3
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ namespace Drupal\decoupled_cookie_auth\EventSubscriber;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\user\Entity\User;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Session\AccountProxyInterface;
@@ -44,14 +44,22 @@ class ModifiedResourceResponseSubscriber implements EventSubscriberInterface {
   */
  protected $configFactory;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new ModifiedResourceResponseSubscriber object.
   */
  public function __construct(CurrentRouteMatch $current_route_match, AccountProxyInterface $current_user, Json $json_serializer, ConfigFactoryInterface $config_factory) {
  public function __construct(CurrentRouteMatch $current_route_match, AccountProxyInterface $current_user, Json $json_serializer, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->currentRouteMatch = $current_route_match;
    $this->currentUser = $current_user;
    $this->jsonSerializer = $json_serializer;
    $this->configFactory = $config_factory;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
@@ -76,7 +84,8 @@ class ModifiedResourceResponseSubscriber implements EventSubscriberInterface {
    $is_route = $this->currentRouteMatch->getRouteName() === 'rest.user_registration.POST';
    if ($is_route && $this->currentUser->isAnonymous()) {
      $data = $this->jsonSerializer->decode($response->getContent());
      $account = User::load($data['uid'][0]['value']);
      /** @var \Drupal\user\UserInterface $account */
      $account = $this->entityTypeManager->getStorage('user')->load($data);
      $config = $this->configFactory->get('user.settings');
      if (!$config->get('verify_mail') && !empty($account) && $account->isActive()) {
        user_login_finalize($account);