Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / user / user.api.php
1 <?php
2
3 /**
4  * @file
5  * Hooks provided by the User module.
6  */
7
8 /**
9  * @addtogroup hooks
10  * @{
11  */
12
13 /**
14  * Act on user account cancellations.
15  *
16  * This hook is invoked from user_cancel() before a user account is canceled.
17  * Depending on the account cancellation method, the module should either do
18  * nothing, unpublish content, or anonymize content. See user_cancel_methods()
19  * for the list of default account cancellation methods provided by User module.
20  * Modules may add further methods via hook_user_cancel_methods_alter().
21  *
22  * This hook is NOT invoked for the 'user_cancel_delete' account cancellation
23  * method. To react to that method, implement hook_ENTITY_TYPE_predelete() or
24  * hook_ENTITY_TYPE_delete() for user entities instead.
25  *
26  * Expensive operations should be added to the global account cancellation batch
27  * by using batch_set().
28  *
29  * @param array $edit
30  *   The array of form values submitted by the user.
31  * @param \Drupal\Core\Session\AccountInterface $account
32  *   The user object on which the operation is being performed.
33  * @param string $method
34  *   The account cancellation method.
35  *
36  * @see user_cancel_methods()
37  * @see hook_user_cancel_methods_alter()
38  */
39 function hook_user_cancel($edit, $account, $method) {
40   switch ($method) {
41     case 'user_cancel_block_unpublish':
42       // Unpublish nodes (current revisions).
43       module_load_include('inc', 'node', 'node.admin');
44       $nodes = \Drupal::entityQuery('node')
45         ->condition('uid', $account->id())
46         ->execute();
47       node_mass_update($nodes, ['status' => 0], NULL, TRUE);
48       break;
49
50     case 'user_cancel_reassign':
51       // Anonymize nodes (current revisions).
52       module_load_include('inc', 'node', 'node.admin');
53       $nodes = \Drupal::entityQuery('node')
54         ->condition('uid', $account->id())
55         ->execute();
56       node_mass_update($nodes, ['uid' => 0], NULL, TRUE);
57       // Anonymize old revisions.
58       db_update('node_field_revision')
59         ->fields(['uid' => 0])
60         ->condition('uid', $account->id())
61         ->execute();
62       break;
63   }
64 }
65
66 /**
67  * Modify account cancellation methods.
68  *
69  * By implementing this hook, modules are able to add, customize, or remove
70  * account cancellation methods. All defined methods are turned into radio
71  * button form elements by user_cancel_methods() after this hook is invoked.
72  * The following properties can be defined for each method:
73  * - title: The radio button's title.
74  * - description: (optional) A description to display on the confirmation form
75  *   if the user is not allowed to select the account cancellation method. The
76  *   description is NOT used for the radio button, but instead should provide
77  *   additional explanation to the user seeking to cancel their account.
78  * - access: (optional) A boolean value indicating whether the user can access
79  *   a method. If 'access' is defined, the method cannot be configured as
80  *   default method.
81  *
82  * @param array $methods
83  *   An array containing user account cancellation methods, keyed by method id.
84  *
85  * @see user_cancel_methods()
86  * @see \Drupal\user\Form\UserCancelForm
87  */
88 function hook_user_cancel_methods_alter(&$methods) {
89   $account = \Drupal::currentUser();
90   // Limit access to disable account and unpublish content method.
91   $methods['user_cancel_block_unpublish']['access'] = $account->hasPermission('administer site configuration');
92
93   // Remove the content re-assigning method.
94   unset($methods['user_cancel_reassign']);
95
96   // Add a custom zero-out method.
97   $methods['mymodule_zero_out'] = [
98     'title' => t('Delete the account and remove all content.'),
99     'description' => t('All your content will be replaced by empty strings.'),
100     // access should be used for administrative methods only.
101     'access' => $account->hasPermission('access zero-out account cancellation method'),
102   ];
103 }
104
105 /**
106  * Alter the username that is displayed for a user.
107  *
108  * Called by $account->getDisplayName() to allow modules to alter the username
109  * that is displayed. Can be used to ensure user privacy in situations where
110  * $account->getDisplayName() is too revealing.
111  *
112  * @param string|Drupal\Component\Render\MarkupInterface $name
113  *   The username that is displayed for a user. If a hook implementation changes
114  *   this to an object implementing MarkupInterface it is the responsibility of
115  *   the implementation to ensure the user's name is escaped properly. String
116  *   values will be autoescaped.
117  * @param \Drupal\Core\Session\AccountInterface $account
118  *   The user object on which the operation is being performed.
119  *
120  * @see \Drupal\Core\Session\AccountInterface::getDisplayName()
121  * @see sanitization
122  */
123 function hook_user_format_name_alter(&$name, $account) {
124   // Display the user's uid instead of name.
125   if ($account->id()) {
126     $name = t('User @uid', ['@uid' => $account->id()]);
127   }
128 }
129
130 /**
131  * The user just logged in.
132  *
133  * @param object $account
134  *   The user object on which the operation was just performed.
135  */
136 function hook_user_login($account) {
137   $config = \Drupal::config('system.date');
138   // If the user has a NULL time zone, notify them to set a time zone.
139   if (!$account->getTimezone() && $config->get('timezone.user.configurable') && $config->get('timezone.user.warn')) {
140     drupal_set_message(t('Configure your <a href=":user-edit">account time zone setting</a>.', [':user-edit' => $account->url('edit-form', ['query' => \Drupal::destination()->getAsArray(), 'fragment' => 'edit-timezone'])]));
141   }
142 }
143
144 /**
145  * The user just logged out.
146  *
147  * @param $account
148  *   The user object on which the operation was just performed.
149  */
150 function hook_user_logout($account) {
151   db_insert('logouts')
152     ->fields([
153       'uid' => $account->id(),
154       'time' => time(),
155     ])
156     ->execute();
157 }
158
159 /**
160  * @} End of "addtogroup hooks".
161  */