Newer
Older

Angie Byron
committed
/**
* @file
* Install, update and uninstall functions for the user module.
*/
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;

Dries Buytaert
committed
* Implements hook_schema().
*/
function user_schema() {
$schema['users_data'] = [
'description' => 'Stores module data as key/value pairs per user.',
'fields' => [
'uid' => [
'description' => 'The {users}.uid this record affects.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'module' => [
'description' => 'The name of the module declaring the variable.',
'type' => 'varchar_ascii',
'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
'not null' => TRUE,
'default' => '',
],
'name' => [
'description' => 'The identifier of the data.',
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
'value' => [
'description' => 'The value.',
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
],
'serialized' => [
'description' => 'Whether value is serialized.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'default' => 0,
],
],
'primary key' => ['uid', 'module', 'name'],
'indexes' => [
'module' => ['module'],
'name' => ['name'],
],
'foreign keys' => [

catch
committed
'data_user' => [
'table' => 'users',
'columns' => [
'uid' => 'uid',
],
],
],
];
return $schema;
}

Dries Buytaert
committed
/**
* Implements hook_install().
*/
function user_install() {
$storage = \Drupal::entityTypeManager()->getStorage('user');

Dries Buytaert
committed
// Insert a row for the anonymous user.
$storage
->create([

Dries Buytaert
committed
'uid' => 0,
'status' => 0,
'name' => '',
])
->save();

Dries Buytaert
committed

Alex Pott
committed
// We need some placeholders here as name and mail are unique.

Angie Byron
committed
// This will be changed by the settings form in the installer.
$storage
->create([

Dries Buytaert
committed
'uid' => 1,
'name' => 'placeholder-for-uid-1',
'mail' => 'placeholder-for-uid-1',
'status' => TRUE,
])
->save();

Dries Buytaert
committed
}
/**
* Implements hook_requirements().
*/
function user_requirements($phase): array {
if ($phase !== 'runtime') {
return [];
}

Dave Long
committed
$return = [];
$result = (bool) \Drupal::entityQuery('user')
->accessCheck(FALSE)
->condition('uid', 0)
->range(0, 1)
->execute();
if ($result === FALSE) {

Dave Long
committed
$return['anonymous user'] = [
'title' => t('Anonymous user'),
'description' => t('The anonymous user does not exist. See the <a href=":url">restore the anonymous (user ID 0) user record</a> for more information', [
':url' => 'https://www.drupal.org/node/1029506',
]),
'severity' => REQUIREMENT_WARNING,
];
}
$query = \Drupal::database()->select('users_field_data');
$query->addExpression('LOWER(mail)', 'lower_mail');
$query->groupBy('lower_mail');
$query->having('COUNT(uid) > :matches', [':matches' => 1]);
$conflicts = $query->countQuery()->execute()->fetchField();
if ($conflicts > 0) {
$return['conflicting emails'] = [
'title' => t('Conflicting user emails'),
'description' => t('Some user accounts have email addresses that differ only by case. For example, one account might have alice@example.com and another might have Alice@Example.com. See <a href=":url">Conflicting User Emails</a> for more information.', [
':url' => 'https://www.drupal.org/node/3486109',
]),
'severity' => REQUIREMENT_WARNING,
];
}

Dave Long
committed
return $return;
}
/**
* Implements hook_update_last_removed().
*/
function user_update_last_removed() {
return 9301;
}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Remove non-existent permissions created by migrations.
*/
function user_update_10000() {
$cleaned_roles = [];
$existing_permissions = array_keys(\Drupal::service('user.permissions')
->getPermissions());
$config_factory = \Drupal::configFactory();
$role_ids = $config_factory->listAll('user.role.');
foreach ($role_ids as $role_id) {
$role_config = $config_factory->getEditable($role_id);
$removed_permissions = array_diff($role_config->get('permissions'), $existing_permissions);
if (!empty($removed_permissions)) {
$cleaned_roles[] = $role_config->get('label');
\Drupal::logger('update')->notice(
'The role %role has had the following non-existent permission(s) removed: %permissions.',
[
'%role' => $role_config->get('label'),
'%permissions' => implode(', ', $removed_permissions),
]
);
$permissions = array_intersect($role_config->get('permissions'), $existing_permissions);
$role_config->set('permissions', $permissions);
$role_config->save();
}
}
if (!empty($cleaned_roles)) {
return new PluralTranslatableMarkup(
count($cleaned_roles),
'The role %role_list has had non-existent permissions removed. Check the logs for details.',
'The roles %role_list have had non-existent permissions removed. Check the logs for details.',
['%role_list' => implode(', ', $cleaned_roles)]
);
}
}