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