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