annotate core/modules/user/src/Form/UserMultipleCancelConfirm.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\user\Form;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 6 use Drupal\Core\Form\ConfirmFormBase;
Chris@0 7 use Drupal\Core\Form\FormStateInterface;
Chris@17 8 use Drupal\Core\Messenger\MessengerInterface;
Chris@0 9 use Drupal\Core\Url;
Chris@14 10 use Drupal\Core\TempStore\PrivateTempStoreFactory;
Chris@0 11 use Drupal\user\UserStorageInterface;
Chris@0 12 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Provides a confirmation form for cancelling multiple user accounts.
Chris@14 16 *
Chris@14 17 * @internal
Chris@0 18 */
Chris@0 19 class UserMultipleCancelConfirm extends ConfirmFormBase {
Chris@0 20
Chris@0 21 /**
Chris@0 22 * The temp store factory.
Chris@0 23 *
Chris@14 24 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
Chris@0 25 */
Chris@0 26 protected $tempStoreFactory;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * The user storage.
Chris@0 30 *
Chris@0 31 * @var \Drupal\user\UserStorageInterface
Chris@0 32 */
Chris@0 33 protected $userStorage;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * The entity manager.
Chris@0 37 *
Chris@0 38 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 39 */
Chris@0 40 protected $entityManager;
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Constructs a new UserMultipleCancelConfirm.
Chris@0 44 *
Chris@14 45 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
Chris@0 46 * The temp store factory.
Chris@0 47 * @param \Drupal\user\UserStorageInterface $user_storage
Chris@0 48 * The user storage.
Chris@0 49 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 50 * The entity manager.
Chris@0 51 */
Chris@0 52 public function __construct(PrivateTempStoreFactory $temp_store_factory, UserStorageInterface $user_storage, EntityManagerInterface $entity_manager) {
Chris@0 53 $this->tempStoreFactory = $temp_store_factory;
Chris@0 54 $this->userStorage = $user_storage;
Chris@0 55 $this->entityManager = $entity_manager;
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * {@inheritdoc}
Chris@0 60 */
Chris@0 61 public static function create(ContainerInterface $container) {
Chris@0 62 return new static(
Chris@14 63 $container->get('tempstore.private'),
Chris@0 64 $container->get('entity.manager')->getStorage('user'),
Chris@0 65 $container->get('entity.manager')
Chris@0 66 );
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * {@inheritdoc}
Chris@0 71 */
Chris@0 72 public function getFormId() {
Chris@0 73 return 'user_multiple_cancel_confirm';
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * {@inheritdoc}
Chris@0 78 */
Chris@0 79 public function getQuestion() {
Chris@0 80 return $this->t('Are you sure you want to cancel these user accounts?');
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * {@inheritdoc}
Chris@0 85 */
Chris@0 86 public function getCancelUrl() {
Chris@0 87 return new Url('entity.user.collection');
Chris@0 88 }
Chris@0 89
Chris@0 90 /**
Chris@0 91 * {@inheritdoc}
Chris@0 92 */
Chris@0 93 public function getConfirmText() {
Chris@0 94 return $this->t('Cancel accounts');
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * {@inheritdoc}
Chris@0 99 */
Chris@0 100 public function buildForm(array $form, FormStateInterface $form_state) {
Chris@0 101 // Retrieve the accounts to be canceled from the temp store.
Chris@0 102 /* @var \Drupal\user\Entity\User[] $accounts */
Chris@0 103 $accounts = $this->tempStoreFactory
Chris@0 104 ->get('user_user_operations_cancel')
Chris@0 105 ->get($this->currentUser()->id());
Chris@0 106 if (!$accounts) {
Chris@0 107 return $this->redirect('entity.user.collection');
Chris@0 108 }
Chris@0 109
Chris@0 110 $root = NULL;
Chris@0 111 $names = [];
Chris@0 112 $form['accounts'] = ['#tree' => TRUE];
Chris@0 113 foreach ($accounts as $account) {
Chris@0 114 $uid = $account->id();
Chris@0 115 $names[$uid] = $account->label();
Chris@0 116 // Prevent user 1 from being canceled.
Chris@0 117 if ($uid <= 1) {
Chris@0 118 $root = intval($uid) === 1 ? $account : $root;
Chris@0 119 continue;
Chris@0 120 }
Chris@0 121 $form['accounts'][$uid] = [
Chris@0 122 '#type' => 'hidden',
Chris@0 123 '#value' => $uid,
Chris@0 124 ];
Chris@0 125 }
Chris@0 126
Chris@0 127 $form['account']['names'] = [
Chris@0 128 '#theme' => 'item_list',
Chris@0 129 '#items' => $names,
Chris@0 130 ];
Chris@0 131
Chris@0 132 // Output a notice that user 1 cannot be canceled.
Chris@0 133 if (isset($root)) {
Chris@0 134 $redirect = (count($accounts) == 1);
Chris@0 135 $message = $this->t('The user account %name cannot be canceled.', ['%name' => $root->label()]);
Chris@17 136 $this->messenger()->addMessage($message, $redirect ? MessengerInterface::TYPE_ERROR : MessengerInterface::TYPE_WARNING);
Chris@0 137 // If only user 1 was selected, redirect to the overview.
Chris@0 138 if ($redirect) {
Chris@0 139 return $this->redirect('entity.user.collection');
Chris@0 140 }
Chris@0 141 }
Chris@0 142
Chris@0 143 $form['operation'] = ['#type' => 'hidden', '#value' => 'cancel'];
Chris@0 144
Chris@18 145 // Display account cancellation method selection, if allowed.
Chris@18 146 $user = $this->currentUser();
Chris@18 147 $selectCancel = $user->hasPermission('administer users') || $user->hasPermission('select account cancellation method');
Chris@18 148
Chris@0 149 $form['user_cancel_method'] = [
Chris@0 150 '#type' => 'radios',
Chris@0 151 '#title' => $this->t('When cancelling these accounts'),
Chris@18 152 '#access' => $selectCancel,
Chris@0 153 ];
Chris@0 154
Chris@0 155 $form['user_cancel_method'] += user_cancel_methods();
Chris@0 156
Chris@18 157 if (!$selectCancel) {
Chris@18 158 // Display an item to inform the user of the setting.
Chris@18 159 $default_method = $form['user_cancel_method']['#default_value'];
Chris@18 160 $form['user_cancel_method_show'] = [
Chris@18 161 '#type' => 'item',
Chris@18 162 '#title' => $this->t('When cancelling these accounts'),
Chris@18 163 '#plain_text' => $form['user_cancel_method']['#options'][$default_method],
Chris@18 164 ];
Chris@18 165 }
Chris@18 166
Chris@0 167 // Allow to send the account cancellation confirmation mail.
Chris@0 168 $form['user_cancel_confirm'] = [
Chris@0 169 '#type' => 'checkbox',
Chris@0 170 '#title' => $this->t('Require email confirmation to cancel account'),
Chris@0 171 '#default_value' => FALSE,
Chris@0 172 '#description' => $this->t('When enabled, the user must confirm the account cancellation via email.'),
Chris@0 173 ];
Chris@0 174 // Also allow to send account canceled notification mail, if enabled.
Chris@0 175 $form['user_cancel_notify'] = [
Chris@0 176 '#type' => 'checkbox',
Chris@0 177 '#title' => $this->t('Notify user when account is canceled'),
Chris@0 178 '#default_value' => FALSE,
Chris@0 179 '#access' => $this->config('user.settings')->get('notify.status_canceled'),
Chris@0 180 '#description' => $this->t('When enabled, the user will receive an email notification after the account has been canceled.'),
Chris@0 181 ];
Chris@0 182
Chris@0 183 $form = parent::buildForm($form, $form_state);
Chris@0 184
Chris@0 185 return $form;
Chris@0 186 }
Chris@0 187
Chris@0 188 /**
Chris@0 189 * {@inheritdoc}
Chris@0 190 */
Chris@0 191 public function submitForm(array &$form, FormStateInterface $form_state) {
Chris@0 192 $current_user_id = $this->currentUser()->id();
Chris@0 193
Chris@0 194 // Clear out the accounts from the temp store.
Chris@0 195 $this->tempStoreFactory->get('user_user_operations_cancel')->delete($current_user_id);
Chris@0 196 if ($form_state->getValue('confirm')) {
Chris@0 197 foreach ($form_state->getValue('accounts') as $uid => $value) {
Chris@0 198 // Prevent programmatic form submissions from cancelling user 1.
Chris@0 199 if ($uid <= 1) {
Chris@0 200 continue;
Chris@0 201 }
Chris@0 202 // Prevent user administrators from deleting themselves without confirmation.
Chris@0 203 if ($uid == $current_user_id) {
Chris@0 204 $admin_form_mock = [];
Chris@0 205 $admin_form_state = $form_state;
Chris@0 206 $admin_form_state->unsetValue('user_cancel_confirm');
Chris@0 207 // The $user global is not a complete user entity, so load the full
Chris@0 208 // entity.
Chris@0 209 $account = $this->userStorage->load($uid);
Chris@0 210 $admin_form = $this->entityManager->getFormObject('user', 'cancel');
Chris@0 211 $admin_form->setEntity($account);
Chris@0 212 // Calling this directly required to init form object with $account.
Chris@0 213 $admin_form->buildForm($admin_form_mock, $admin_form_state);
Chris@0 214 $admin_form->submitForm($admin_form_mock, $admin_form_state);
Chris@0 215 }
Chris@0 216 else {
Chris@0 217 user_cancel($form_state->getValues(), $uid, $form_state->getValue('user_cancel_method'));
Chris@0 218 }
Chris@0 219 }
Chris@0 220 }
Chris@0 221 $form_state->setRedirect('entity.user.collection');
Chris@0 222 }
Chris@0 223
Chris@0 224 }