Commit 8d348bb8 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3274874 by AdamPS: Perform account switching

parent 92ea30cb
Loading
Loading
Loading
Loading
+63 −1
Changes for src/Email.php: 63 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -9,7 +9,10 @@ use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\symfony_mailer\Processor\EmailProcessorInterface;
use Drupal\user\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mime\Email as SymfonyEmail;

@@ -48,6 +51,19 @@ class Email implements InternalEmailInterface {
   */
  protected $themeManager;

  /**
   * Account switcher.
   *
   * @var \Drupal\Core\Session\AccountSwitcherInterface
   */
  protected $accountSwitcher;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * @var string
@@ -111,6 +127,13 @@ class Email implements InternalEmailInterface {
   */
  protected $transportDsn = '';

  /**
   * True if must switch account for rendering.
   *
   * @var bool
   */
  protected $mustSwitchAccount = FALSE;

  /**
   * Constructs the Email object.
   *
@@ -122,6 +145,10 @@ class Email implements InternalEmailInterface {
   *   The entity type manager.
   * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
   *   The theme manager.
   * @param \Drupal\Core\Session\AccountSwitcherInterface $account_switcher
   *   The account switcher service.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
   * @param string $type
   *   Type. @see \Drupal\symfony_mailer\BaseEmailInterface::getType()
   * @param string $sub_type
@@ -129,11 +156,13 @@ class Email implements InternalEmailInterface {
   * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
   *   (optional) Entity. @see \Drupal\symfony_mailer\BaseEmailInterface::getEntity()
   */
  public function __construct(MailerInterface $mailer, RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager, ThemeManagerInterface $theme_manager, string $type, string $sub_type, ?ConfigEntityInterface $entity) {
  public function __construct(MailerInterface $mailer, RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager, ThemeManagerInterface $theme_manager, AccountSwitcherInterface $account_switcher, AccountInterface $account, string $type, string $sub_type, ?ConfigEntityInterface $entity) {
    $this->mailer = $mailer;
    $this->renderer = $renderer;
    $this->entityTypeManager = $entity_type_manager;
    $this->themeManager = $theme_manager;
    $this->accountSwitcher = $account_switcher;
    $this->account = $account;
    $this->type = $type;
    $this->subType = $sub_type;
    $this->entity = $entity;
@@ -163,6 +192,8 @@ class Email implements InternalEmailInterface {
      $container->get('renderer'),
      $container->get('entity_type.manager'),
      $container->get('theme.manager'),
      $container->get('account_switcher'),
      $container->get('current_user'),
      $type,
      $sub_type,
      $entity
@@ -267,6 +298,7 @@ class Email implements InternalEmailInterface {
      ->view($entity, $view_mode);

    $this->appendBody($build);
    $this->mustSwitchAccount = TRUE;
    return $this;
  }

@@ -422,6 +454,13 @@ class Email implements InternalEmailInterface {
  public function render() {
    $this->valid('preRender');

    $user = $this->mustSwitchAccount ? $this->getUser() : NULL;
    $must_switch = $user && $user->id() != $this->account->id();

    if ($must_switch) {
      $this->accountSwitcher->switchTo($user);
    }

    // Render subject.
    if ($this->subject instanceof MarkupInterface) {
      $this->subject = PlainTextOutput::renderFromHtml($this->subject);
@@ -434,6 +473,11 @@ class Email implements InternalEmailInterface {
    $this->setHtmlBody($html);
    $this->body = [];

    if ($must_switch) {
      $this->accountSwitcher->switchBack();
    }


    return $this;
  }

@@ -476,4 +520,22 @@ class Email implements InternalEmailInterface {
    return $this;
  }

  /**
   * Gets a user account for the recipient of this email.
   *
   * If there is a single to address, searcch for a matching user account.
   *
   * @return
   */
  protected function getUser() {
    $to = $this->getTo();
    if (count($to) == 1) {
      $user = user_load_by_mail($to[0]);
    }
    if (empty($user)) {
      $user = User::getAnonymousUser();
    }
    return $user;
  }

}