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