1.20.x user.install | user_update_1007() |
Convert user roles to configuration files.
Related topics
File
- modules/
user/ user.install, line 378 - Install, update and uninstall functions for the user module.
Code
function user_update_1007() {
// Update role ID to be a varchar instead of an integer.
if (db_field_exists('users_roles', 'rid')) {
db_drop_primary_key('users_roles');
db_drop_index('users_roles', 'rid');
// This column is added as part of the initial Backdrop update fixing
// initial compatibility. See update_prepare_bootstrap().
if (db_field_exists('users_roles', 'role')) {
db_drop_field('users_roles', 'role');
}
$role_column = array(
'type' => 'varchar',
'length' => 64,
'description' => 'Primary Key: The name of the role.',
'not null' => TRUE,
'default' => '',
);
db_change_field('users_roles', 'rid', 'role', $role_column);
db_add_primary_key('users_roles', array('uid', 'role'));
db_add_index('users_roles', 'role', array('role'));
}
$admin_role_id = config_get('system.core', 'user_admin_role');
$role_result = db_query("SELECT * FROM {role}");
foreach ($role_result as $row) {
$role_data = array(
'name' => $row->rid,
'label' => backdrop_ucfirst($row->name),
'weight' => $row->weight,
);
// Ensure default role names are correct.
if ($row->rid == 1) {
$role_data['name'] = 'anonymous';
$role_data['label'] = 'Anonymous';
}
if ($row->rid == 2) {
$role_data['name'] = 'authenticated';
$role_data['label'] = 'Authenticated';
}
// Build the list of permissions.
$role_data['permissions'] = array();
$permission_result = db_query("SELECT * FROM {role_permission} WHERE rid = :rid", array(':rid' => $row->rid));
foreach ($permission_result as $permission_row) {
$role_data['permissions'][] = $permission_row->permission;
}
// Save the new user role.
$config = config('user.role.' . $role_data['name']);
$config->setData($role_data);
$config->save();
// Update the users table to match the new role names.
db_update('users_roles')
->fields(array(
'role' => $role_data['name'],
))
->condition('role', $row->rid)
->execute();
// Update the admin role.
if ($admin_role_id == $row->rid) {
config_set('system.core', 'user_admin_role', $role_data['name']);
}
}
db_drop_table('role');
db_drop_table('role_permission');
db_drop_field('users_roles', 'rid');
}