comparison core/modules/user/src/AccountSettingsForm.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\user;
4
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Render\Element;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13 * Configure user settings for this site.
14 *
15 * @internal
16 */
17 class AccountSettingsForm extends ConfigFormBase {
18
19 /**
20 * The module handler.
21 *
22 * @var \Drupal\Core\Extension\ModuleHandlerInterface
23 */
24 protected $moduleHandler;
25
26 /**
27 * The role storage used when changing the admin role.
28 *
29 * @var \Drupal\user\RoleStorageInterface
30 */
31 protected $roleStorage;
32
33 /**
34 * Constructs a \Drupal\user\AccountSettingsForm object.
35 *
36 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
37 * The factory for configuration objects.
38 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
39 * The module handler.
40 * @param \Drupal\user\RoleStorageInterface $role_storage
41 * The role storage.
42 */
43 public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, RoleStorageInterface $role_storage) {
44 parent::__construct($config_factory);
45 $this->moduleHandler = $module_handler;
46 $this->roleStorage = $role_storage;
47 }
48
49 /**
50 * {@inheritdoc}
51 */
52 public static function create(ContainerInterface $container) {
53 return new static(
54 $container->get('config.factory'),
55 $container->get('module_handler'),
56 $container->get('entity.manager')->getStorage('user_role')
57 );
58 }
59
60 /**
61 * {@inheritdoc}
62 */
63 public function getFormId() {
64 return 'user_admin_settings';
65 }
66
67 /**
68 * {@inheritdoc}
69 */
70 protected function getEditableConfigNames() {
71 return [
72 'system.site',
73 'user.mail',
74 'user.settings',
75 ];
76 }
77
78 /**
79 * {@inheritdoc}
80 */
81 public function buildForm(array $form, FormStateInterface $form_state) {
82 $form = parent::buildForm($form, $form_state);
83 $config = $this->config('user.settings');
84 $mail_config = $this->config('user.mail');
85 $site_config = $this->config('system.site');
86
87 $form['#attached']['library'][] = 'user/drupal.user.admin';
88
89 // Settings for anonymous users.
90 $form['anonymous_settings'] = [
91 '#type' => 'details',
92 '#title' => $this->t('Anonymous users'),
93 '#open' => TRUE,
94 ];
95 $form['anonymous_settings']['anonymous'] = [
96 '#type' => 'textfield',
97 '#title' => $this->t('Name'),
98 '#default_value' => $config->get('anonymous'),
99 '#description' => $this->t('The name used to indicate anonymous users.'),
100 '#required' => TRUE,
101 ];
102
103 // Administrative role option.
104 $form['admin_role'] = [
105 '#type' => 'details',
106 '#title' => $this->t('Administrator role'),
107 '#open' => TRUE,
108 ];
109 // Do not allow users to set the anonymous or authenticated user roles as the
110 // administrator role.
111 $roles = user_role_names(TRUE);
112 unset($roles[RoleInterface::AUTHENTICATED_ID]);
113
114 $admin_roles = $this->roleStorage->getQuery()
115 ->condition('is_admin', TRUE)
116 ->execute();
117 $default_value = reset($admin_roles);
118
119 $form['admin_role']['user_admin_role'] = [
120 '#type' => 'select',
121 '#title' => $this->t('Administrator role'),
122 '#empty_value' => '',
123 '#default_value' => $default_value,
124 '#options' => $roles,
125 '#description' => $this->t('This role will be automatically assigned new permissions whenever a module is enabled. Changing this setting will not affect existing permissions.'),
126 // Don't allow to select a single admin role in case multiple roles got
127 // marked as admin role already.
128 '#access' => count($admin_roles) <= 1,
129 ];
130
131 // @todo Remove this check once language settings are generalized.
132 if ($this->moduleHandler->moduleExists('content_translation')) {
133 $form['language'] = [
134 '#type' => 'details',
135 '#title' => $this->t('Language settings'),
136 '#open' => TRUE,
137 '#tree' => TRUE,
138 ];
139 $form_state->set(['content_translation', 'key'], 'language');
140 $form['language'] += content_translation_enable_widget('user', 'user', $form, $form_state);
141 }
142
143 // User registration settings.
144 $form['registration_cancellation'] = [
145 '#type' => 'details',
146 '#title' => $this->t('Registration and cancellation'),
147 '#open' => TRUE,
148 ];
149 $form['registration_cancellation']['user_register'] = [
150 '#type' => 'radios',
151 '#title' => $this->t('Who can register accounts?'),
152 '#default_value' => $config->get('register'),
153 '#options' => [
154 USER_REGISTER_ADMINISTRATORS_ONLY => $this->t('Administrators only'),
155 USER_REGISTER_VISITORS => $this->t('Visitors'),
156 USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => $this->t('Visitors, but administrator approval is required'),
157 ]
158 ];
159 $form['registration_cancellation']['user_email_verification'] = [
160 '#type' => 'checkbox',
161 '#title' => $this->t('Require email verification when a visitor creates an account'),
162 '#default_value' => $config->get('verify_mail'),
163 '#description' => $this->t('New users will be required to validate their email address prior to logging into the site, and will be assigned a system-generated password. With this setting disabled, users will be logged in immediately upon registering, and may select their own passwords during registration.')
164 ];
165 $form['registration_cancellation']['user_password_strength'] = [
166 '#type' => 'checkbox',
167 '#title' => $this->t('Enable password strength indicator'),
168 '#default_value' => $config->get('password_strength'),
169 ];
170 $form['registration_cancellation']['user_cancel_method'] = [
171 '#type' => 'radios',
172 '#title' => $this->t('When cancelling a user account'),
173 '#default_value' => $config->get('cancel_method'),
174 '#description' => $this->t('Users with the %select-cancel-method or %administer-users <a href=":permissions-url">permissions</a> can override this default method.', ['%select-cancel-method' => $this->t('Select method for cancelling account'), '%administer-users' => $this->t('Administer users'), ':permissions-url' => $this->url('user.admin_permissions')]),
175 ];
176 $form['registration_cancellation']['user_cancel_method'] += user_cancel_methods();
177 foreach (Element::children($form['registration_cancellation']['user_cancel_method']) as $key) {
178 // All account cancellation methods that specify #access cannot be
179 // configured as default method.
180 // @see hook_user_cancel_methods_alter()
181 if (isset($form['registration_cancellation']['user_cancel_method'][$key]['#access'])) {
182 $form['registration_cancellation']['user_cancel_method'][$key]['#access'] = FALSE;
183 }
184 }
185
186 // Default notifications address.
187 $form['mail_notification_address'] = [
188 '#type' => 'email',
189 '#title' => $this->t('Notification email address'),
190 '#default_value' => $site_config->get('mail_notification'),
191 '#description' => $this->t("The email address to be used as the 'from' address for all account notifications listed below. If <em>'Visitors, but administrator approval is required'</em> is selected above, a notification email will also be sent to this address for any new registrations. Leave empty to use the default system email address <em>(%site-email).</em>", ['%site-email' => $site_config->get('mail')]),
192 '#maxlength' => 180,
193 ];
194
195 $form['email'] = [
196 '#type' => 'vertical_tabs',
197 '#title' => $this->t('Emails'),
198 ];
199 // These email tokens are shared for all settings, so just define
200 // the list once to help ensure they stay in sync.
201 $email_token_help = $this->t('Available variables are: [site:name], [site:url], [user:display-name], [user:account-name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');
202
203 $form['email_admin_created'] = [
204 '#type' => 'details',
205 '#title' => $this->t('Welcome (new user created by administrator)'),
206 '#open' => $config->get('register') == USER_REGISTER_ADMINISTRATORS_ONLY,
207 '#description' => $this->t('Edit the welcome email messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,
208 '#group' => 'email',
209 ];
210 $form['email_admin_created']['user_mail_register_admin_created_subject'] = [
211 '#type' => 'textfield',
212 '#title' => $this->t('Subject'),
213 '#default_value' => $mail_config->get('register_admin_created.subject'),
214 '#maxlength' => 180,
215 ];
216 $form['email_admin_created']['user_mail_register_admin_created_body'] = [
217 '#type' => 'textarea',
218 '#title' => $this->t('Body'),
219 '#default_value' => $mail_config->get('register_admin_created.body'),
220 '#rows' => 15,
221 ];
222
223 $form['email_pending_approval'] = [
224 '#type' => 'details',
225 '#title' => $this->t('Welcome (awaiting approval)'),
226 '#open' => $config->get('register') == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
227 '#description' => $this->t('Edit the welcome email messages sent to new members upon registering, when administrative approval is required.') . ' ' . $email_token_help,
228 '#group' => 'email',
229 ];
230 $form['email_pending_approval']['user_mail_register_pending_approval_subject'] = [
231 '#type' => 'textfield',
232 '#title' => $this->t('Subject'),
233 '#default_value' => $mail_config->get('register_pending_approval.subject'),
234 '#maxlength' => 180,
235 ];
236 $form['email_pending_approval']['user_mail_register_pending_approval_body'] = [
237 '#type' => 'textarea',
238 '#title' => $this->t('Body'),
239 '#default_value' => $mail_config->get('register_pending_approval.body'),
240 '#rows' => 8,
241 ];
242
243 $form['email_pending_approval_admin'] = [
244 '#type' => 'details',
245 '#title' => $this->t('Admin (user awaiting approval)'),
246 '#open' => $config->get('register') == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
247 '#description' => $this->t('Edit the email notifying the site administrator that there are new members awaiting administrative approval.') . ' ' . $email_token_help,
248 '#group' => 'email',
249 ];
250 $form['email_pending_approval_admin']['register_pending_approval_admin_subject'] = [
251 '#type' => 'textfield',
252 '#title' => $this->t('Subject'),
253 '#default_value' => $mail_config->get('register_pending_approval_admin.subject'),
254 '#maxlength' => 180,
255 ];
256 $form['email_pending_approval_admin']['register_pending_approval_admin_body'] = [
257 '#type' => 'textarea',
258 '#title' => $this->t('Body'),
259 '#default_value' => $mail_config->get('register_pending_approval_admin.body'),
260 '#rows' => 8,
261 ];
262
263 $form['email_no_approval_required'] = [
264 '#type' => 'details',
265 '#title' => $this->t('Welcome (no approval required)'),
266 '#open' => $config->get('register') == USER_REGISTER_VISITORS,
267 '#description' => $this->t('Edit the welcome email messages sent to new members upon registering, when no administrator approval is required.') . ' ' . $email_token_help,
268 '#group' => 'email',
269 ];
270 $form['email_no_approval_required']['user_mail_register_no_approval_required_subject'] = [
271 '#type' => 'textfield',
272 '#title' => $this->t('Subject'),
273 '#default_value' => $mail_config->get('register_no_approval_required.subject'),
274 '#maxlength' => 180,
275 ];
276 $form['email_no_approval_required']['user_mail_register_no_approval_required_body'] = [
277 '#type' => 'textarea',
278 '#title' => $this->t('Body'),
279 '#default_value' => $mail_config->get('register_no_approval_required.body'),
280 '#rows' => 15,
281 ];
282
283 $form['email_password_reset'] = [
284 '#type' => 'details',
285 '#title' => $this->t('Password recovery'),
286 '#description' => $this->t('Edit the email messages sent to users who request a new password.') . ' ' . $email_token_help,
287 '#group' => 'email',
288 '#weight' => 10,
289 ];
290 $form['email_password_reset']['user_mail_password_reset_subject'] = [
291 '#type' => 'textfield',
292 '#title' => $this->t('Subject'),
293 '#default_value' => $mail_config->get('password_reset.subject'),
294 '#maxlength' => 180,
295 ];
296 $form['email_password_reset']['user_mail_password_reset_body'] = [
297 '#type' => 'textarea',
298 '#title' => $this->t('Body'),
299 '#default_value' => $mail_config->get('password_reset.body'),
300 '#rows' => 12,
301 ];
302
303 $form['email_activated'] = [
304 '#type' => 'details',
305 '#title' => $this->t('Account activation'),
306 '#description' => $this->t('Enable and edit email messages sent to users upon account activation (when an administrator activates an account of a user who has already registered, on a site where administrative approval is required).') . ' ' . $email_token_help,
307 '#group' => 'email',
308 ];
309 $form['email_activated']['user_mail_status_activated_notify'] = [
310 '#type' => 'checkbox',
311 '#title' => $this->t('Notify user when account is activated'),
312 '#default_value' => $config->get('notify.status_activated'),
313 ];
314 $form['email_activated']['settings'] = [
315 '#type' => 'container',
316 '#states' => [
317 // Hide the additional settings when this email is disabled.
318 'invisible' => [
319 'input[name="user_mail_status_activated_notify"]' => ['checked' => FALSE],
320 ],
321 ],
322 ];
323 $form['email_activated']['settings']['user_mail_status_activated_subject'] = [
324 '#type' => 'textfield',
325 '#title' => $this->t('Subject'),
326 '#default_value' => $mail_config->get('status_activated.subject'),
327 '#maxlength' => 180,
328 ];
329 $form['email_activated']['settings']['user_mail_status_activated_body'] = [
330 '#type' => 'textarea',
331 '#title' => $this->t('Body'),
332 '#default_value' => $mail_config->get('status_activated.body'),
333 '#rows' => 15,
334 ];
335
336 $form['email_blocked'] = [
337 '#type' => 'details',
338 '#title' => $this->t('Account blocked'),
339 '#description' => $this->t('Enable and edit email messages sent to users when their accounts are blocked.') . ' ' . $email_token_help,
340 '#group' => 'email',
341 ];
342 $form['email_blocked']['user_mail_status_blocked_notify'] = [
343 '#type' => 'checkbox',
344 '#title' => $this->t('Notify user when account is blocked'),
345 '#default_value' => $config->get('notify.status_blocked'),
346 ];
347 $form['email_blocked']['settings'] = [
348 '#type' => 'container',
349 '#states' => [
350 // Hide the additional settings when the blocked email is disabled.
351 'invisible' => [
352 'input[name="user_mail_status_blocked_notify"]' => ['checked' => FALSE],
353 ],
354 ],
355 ];
356 $form['email_blocked']['settings']['user_mail_status_blocked_subject'] = [
357 '#type' => 'textfield',
358 '#title' => $this->t('Subject'),
359 '#default_value' => $mail_config->get('status_blocked.subject'),
360 '#maxlength' => 180,
361 ];
362 $form['email_blocked']['settings']['user_mail_status_blocked_body'] = [
363 '#type' => 'textarea',
364 '#title' => $this->t('Body'),
365 '#default_value' => $mail_config->get('status_blocked.body'),
366 '#rows' => 3,
367 ];
368
369 $form['email_cancel_confirm'] = [
370 '#type' => 'details',
371 '#title' => $this->t('Account cancellation confirmation'),
372 '#description' => $this->t('Edit the email messages sent to users when they attempt to cancel their accounts.') . ' ' . $email_token_help,
373 '#group' => 'email',
374 ];
375 $form['email_cancel_confirm']['user_mail_cancel_confirm_subject'] = [
376 '#type' => 'textfield',
377 '#title' => $this->t('Subject'),
378 '#default_value' => $mail_config->get('cancel_confirm.subject'),
379 '#maxlength' => 180,
380 ];
381 $form['email_cancel_confirm']['user_mail_cancel_confirm_body'] = [
382 '#type' => 'textarea',
383 '#title' => $this->t('Body'),
384 '#default_value' => $mail_config->get('cancel_confirm.body'),
385 '#rows' => 3,
386 ];
387
388 $form['email_canceled'] = [
389 '#type' => 'details',
390 '#title' => $this->t('Account canceled'),
391 '#description' => $this->t('Enable and edit email messages sent to users when their accounts are canceled.') . ' ' . $email_token_help,
392 '#group' => 'email',
393 ];
394 $form['email_canceled']['user_mail_status_canceled_notify'] = [
395 '#type' => 'checkbox',
396 '#title' => $this->t('Notify user when account is canceled'),
397 '#default_value' => $config->get('notify.status_canceled'),
398 ];
399 $form['email_canceled']['settings'] = [
400 '#type' => 'container',
401 '#states' => [
402 // Hide the settings when the cancel notify checkbox is disabled.
403 'invisible' => [
404 'input[name="user_mail_status_canceled_notify"]' => ['checked' => FALSE],
405 ],
406 ],
407 ];
408 $form['email_canceled']['settings']['user_mail_status_canceled_subject'] = [
409 '#type' => 'textfield',
410 '#title' => $this->t('Subject'),
411 '#default_value' => $mail_config->get('status_canceled.subject'),
412 '#maxlength' => 180,
413 ];
414 $form['email_canceled']['settings']['user_mail_status_canceled_body'] = [
415 '#type' => 'textarea',
416 '#title' => $this->t('Body'),
417 '#default_value' => $mail_config->get('status_canceled.body'),
418 '#rows' => 3,
419 ];
420
421 return $form;
422 }
423
424 /**
425 * {@inheritdoc}
426 */
427 public function submitForm(array &$form, FormStateInterface $form_state) {
428 parent::submitForm($form, $form_state);
429
430 $this->config('user.settings')
431 ->set('anonymous', $form_state->getValue('anonymous'))
432 ->set('register', $form_state->getValue('user_register'))
433 ->set('password_strength', $form_state->getValue('user_password_strength'))
434 ->set('verify_mail', $form_state->getValue('user_email_verification'))
435 ->set('cancel_method', $form_state->getValue('user_cancel_method'))
436 ->set('notify.status_activated', $form_state->getValue('user_mail_status_activated_notify'))
437 ->set('notify.status_blocked', $form_state->getValue('user_mail_status_blocked_notify'))
438 ->set('notify.status_canceled', $form_state->getValue('user_mail_status_canceled_notify'))
439 ->save();
440 $this->config('user.mail')
441 ->set('cancel_confirm.body', $form_state->getValue('user_mail_cancel_confirm_body'))
442 ->set('cancel_confirm.subject', $form_state->getValue('user_mail_cancel_confirm_subject'))
443 ->set('password_reset.body', $form_state->getValue('user_mail_password_reset_body'))
444 ->set('password_reset.subject', $form_state->getValue('user_mail_password_reset_subject'))
445 ->set('register_admin_created.body', $form_state->getValue('user_mail_register_admin_created_body'))
446 ->set('register_admin_created.subject', $form_state->getValue('user_mail_register_admin_created_subject'))
447 ->set('register_no_approval_required.body', $form_state->getValue('user_mail_register_no_approval_required_body'))
448 ->set('register_no_approval_required.subject', $form_state->getValue('user_mail_register_no_approval_required_subject'))
449 ->set('register_pending_approval.body', $form_state->getValue('user_mail_register_pending_approval_body'))
450 ->set('register_pending_approval.subject', $form_state->getValue('user_mail_register_pending_approval_subject'))
451 ->set('register_pending_approval_admin.body', $form_state->getValue('register_pending_approval_admin_body'))
452 ->set('register_pending_approval_admin.subject', $form_state->getValue('register_pending_approval_admin_subject'))
453 ->set('status_activated.body', $form_state->getValue('user_mail_status_activated_body'))
454 ->set('status_activated.subject', $form_state->getValue('user_mail_status_activated_subject'))
455 ->set('status_blocked.body', $form_state->getValue('user_mail_status_blocked_body'))
456 ->set('status_blocked.subject', $form_state->getValue('user_mail_status_blocked_subject'))
457 ->set('status_canceled.body', $form_state->getValue('user_mail_status_canceled_body'))
458 ->set('status_canceled.subject', $form_state->getValue('user_mail_status_canceled_subject'))
459 ->save();
460 $this->config('system.site')
461 ->set('mail_notification', $form_state->getValue('mail_notification_address'))
462 ->save();
463
464 // Change the admin role.
465 if ($form_state->hasValue('user_admin_role')) {
466 $admin_roles = $this->roleStorage->getQuery()
467 ->condition('is_admin', TRUE)
468 ->execute();
469
470 foreach ($admin_roles as $rid) {
471 $this->roleStorage->load($rid)->setIsAdmin(FALSE)->save();
472 }
473
474 $new_admin_role = $form_state->getValue('user_admin_role');
475 if ($new_admin_role) {
476 $this->roleStorage->load($new_admin_role)->setIsAdmin(TRUE)->save();
477 }
478 }
479 }
480
481 }