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