UserMultipleCancelConfirm.php 6.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\user\Form\UserMultipleCancelConfirm.
 */

namespace Drupal\user\Form;

use Drupal\Component\Utility\String;
11
use Drupal\Core\Entity\EntityManagerInterface;
12
use Drupal\Core\Form\ConfirmFormBase;
13
use Drupal\Core\Form\FormStateInterface;
14
use Drupal\Core\Routing\UrlGeneratorInterface;
15
use Drupal\Core\Url;
16
use Drupal\user\TempStoreFactory;
17
use Drupal\user\UserStorageInterface;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Provides a confirmation form for cancelling multiple user accounts.
 */
class UserMultipleCancelConfirm extends ConfirmFormBase {

  /**
   * The temp store factory.
   *
   * @var \Drupal\user\TempStoreFactory
   */
  protected $tempStoreFactory;

  /**
34
   * The user storage.
35
   *
36
   * @var \Drupal\user\UserStorageInterface
37 38 39 40 41 42
   */
  protected $userStorage;

  /**
   * The entity manager.
   *
43
   * @var \Drupal\Core\Entity\EntityManagerInterface
44 45 46 47 48 49 50 51
   */
  protected $entityManager;

  /**
   * Constructs a new UserMultipleCancelConfirm.
   *
   * @param \Drupal\user\TempStoreFactory $temp_store_factory
   *   The temp store factory.
52 53
   * @param \Drupal\user\UserStorageInterface $user_storage
   *   The user storage.
54
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
55 56
   *   The entity manager.
   */
57
  public function __construct(TempStoreFactory $temp_store_factory, UserStorageInterface $user_storage, EntityManagerInterface $entity_manager) {
58 59 60 61 62 63 64 65 66 67 68
    $this->tempStoreFactory = $temp_store_factory;
    $this->userStorage = $user_storage;
    $this->entityManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('user.tempstore'),
69
      $container->get('entity.manager')->getStorage('user'),
70 71 72 73 74 75 76
      $container->get('entity.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
77
  public function getFormId() {
78 79 80 81 82 83 84 85 86 87 88 89 90
    return 'user_multiple_cancel_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to cancel these user accounts?');
  }

  /**
   * {@inheritdoc}
   */
91 92
  public function getCancelUrl() {
    return new Url('user.admin_account');
93 94 95 96 97 98 99 100 101 102 103 104
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Cancel accounts');
  }

  /**
   * {@inheritdoc}
   */
105
  public function buildForm(array $form, FormStateInterface $form_state) {
106 107 108 109 110
    // Retrieve the accounts to be canceled from the temp store.
    $accounts = $this->tempStoreFactory
      ->get('user_user_operations_cancel')
      ->get($this->currentUser()->id());
    if (!$accounts) {
111
      return new RedirectResponse($this->url('user.admin_account', [], ['absolute' => TRUE]));
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    }

    $form['accounts'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
    foreach ($accounts as $uid => $account) {
      // Prevent user 1 from being canceled.
      if ($uid <= 1) {
        continue;
      }
      $form['accounts'][$uid] = array(
        '#type' => 'hidden',
        '#value' => $uid,
        '#prefix' => '<li>',
        '#suffix' => String::checkPlain($account->label()) . "</li>\n",
      );
    }

    // Output a notice that user 1 cannot be canceled.
    if (isset($accounts[1])) {
      $redirect = (count($accounts) == 1);
      $message = $this->t('The user account %name cannot be canceled.', array('%name' => $accounts[1]->label()));
      drupal_set_message($message, $redirect ? 'error' : 'warning');
      // If only user 1 was selected, redirect to the overview.
      if ($redirect) {
135
        return new RedirectResponse($this->url('user.admin_account', [], ['absolute' => TRUE]));
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
      }
    }

    $form['operation'] = array('#type' => 'hidden', '#value' => 'cancel');

    $form['user_cancel_method'] = array(
      '#type' => 'radios',
      '#title' => $this->t('When cancelling these accounts'),
    );

    $form['user_cancel_method'] += user_cancel_methods();

    // Allow to send the account cancellation confirmation mail.
    $form['user_cancel_confirm'] = array(
      '#type' => 'checkbox',
151
      '#title' => $this->t('Require email confirmation to cancel account'),
152
      '#default_value' => FALSE,
153
      '#description' => $this->t('When enabled, the user must confirm the account cancellation via email.'),
154 155 156 157
    );
    // Also allow to send account canceled notification mail, if enabled.
    $form['user_cancel_notify'] = array(
      '#type' => 'checkbox',
158
      '#title' => $this->t('Notify user when account is canceled'),
159
      '#default_value' => FALSE,
160
      '#access' => $this->config('user.settings')->get('notify.status_canceled'),
161
      '#description' => $this->t('When enabled, the user will receive an email notification after the account has been canceled.'),
162 163 164 165 166 167 168 169 170 171
    );

    $form = parent::buildForm($form, $form_state);

    return $form;
  }

  /**
   * {@inheritdoc}
   */
172
  public function submitForm(array &$form, FormStateInterface $form_state) {
173 174 175 176
    $current_user_id = $this->currentUser()->id();

    // Clear out the accounts from the temp store.
    $this->tempStoreFactory->get('user_user_operations_cancel')->delete($current_user_id);
177 178
    if ($form_state->getValue('confirm')) {
      foreach ($form_state->getValue('accounts') as $uid => $value) {
179 180 181 182 183 184 185 186
        // Prevent programmatic form submissions from cancelling user 1.
        if ($uid <= 1) {
          continue;
        }
        // Prevent user administrators from deleting themselves without confirmation.
        if ($uid == $current_user_id) {
          $admin_form_mock = array();
          $admin_form_state = $form_state;
187
          $admin_form_state->unsetValue('user_cancel_confirm');
188 189 190
          // The $user global is not a complete user entity, so load the full
          // entity.
          $account = $this->userStorage->load($uid);
191
          $admin_form = $this->entityManager->getFormObject('user', 'cancel');
192 193
          $admin_form->setEntity($account);
          // Calling this directly required to init form object with $account.
194
          $admin_form->buildForm($admin_form_mock, $admin_form_state);
195
          $admin_form->submitForm($admin_form_mock, $admin_form_state);
196 197
        }
        else {
198
          user_cancel($form_state->getValues(), $uid, $form_state->getValue('user_cancel_method'));
199 200 201
        }
      }
    }
202
    $form_state->setRedirect('user.admin_account');
203 204 205
  }

}