From b1dc4a8f23e10324f8dc460123581f0698c41211 Mon Sep 17 00:00:00 2001 From: Hugo Wetterberg Date: Fri, 24 Aug 2012 15:24:43 +0200 Subject: [PATCH] Initial work on porting roles to cmi --- core/includes/session.inc | 8 +- core/modules/user/config/user.roles.yml | 6 ++ .../user/lib/Drupal/user/UserStorageController.php | 5 +- core/modules/user/user.admin.inc | 26 +++---- core/modules/user/user.install | 60 +++++--------- core/modules/user/user.module | 91 ++++++++++++++-------- 6 files changed, 106 insertions(+), 90 deletions(-) create mode 100644 core/modules/user/config/user.roles.yml diff --git a/core/includes/session.inc b/core/includes/session.inc index 5761dc9..25b4b4a 100644 --- a/core/includes/session.inc +++ b/core/includes/session.inc @@ -111,7 +111,13 @@ function _drupal_session_read($sid) { // Add roles element to $user. $user->roles = array(); $user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user'; - $user->roles += db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = :uid", array(':uid' => $user->uid))->fetchAllKeyed(0, 1); + + $role_config = config('user.roles'); + $rids = db_query("SELECT ur.rid FROM {users_roles} ur WHERE ur.uid = :uid", array(':uid' => $user->uid))->fetchCol(); + foreach ($rids as $rid) { + $role = $role_config->get($rid); + $user->roles[$rid] = $role['name']; + } } elseif ($user) { // The user is anonymous or blocked. Only preserve two fields from the diff --git a/core/modules/user/config/user.roles.yml b/core/modules/user/config/user.roles.yml new file mode 100644 index 0000000..189ef36 --- /dev/null +++ b/core/modules/user/config/user.roles.yml @@ -0,0 +1,6 @@ +anonymous: + name: Anonymous user + weight: 0 +authenticated: + name: Authenticated user + weight: 1 diff --git a/core/modules/user/lib/Drupal/user/UserStorageController.php b/core/modules/user/lib/Drupal/user/UserStorageController.php index 3e2ac68..92d2b82 100644 --- a/core/modules/user/lib/Drupal/user/UserStorageController.php +++ b/core/modules/user/lib/Drupal/user/UserStorageController.php @@ -40,9 +40,10 @@ class UserStorageController extends DatabaseStorageController { } // Add any additional roles from the database. - $result = db_query('SELECT r.rid, r.name, ur.uid FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid IN (:uids)', array(':uids' => array_keys($queried_users))); + $result = db_query('SELECT ur.rid, ur.uid FROM {users_roles} ur WHERE ur.uid IN (:uids)', array(':uids' => array_keys($queried_users))); foreach ($result as $record) { - $queried_users[$record->uid]->roles[$record->rid] = $record->name; + $role = user_role_load($record->rid); + $queried_users[$record->uid]->roles[$record->rid] = $role->name; } // Add the full file objects for user pictures if enabled. diff --git a/core/modules/user/user.admin.inc b/core/modules/user/user.admin.inc index fac9723..77ed8d0 100644 --- a/core/modules/user/user.admin.inc +++ b/core/modules/user/user.admin.inc @@ -823,25 +823,21 @@ function theme_user_permission_description($variables) { * @see theme_user_admin_roles() */ function user_admin_roles($form, $form_state) { - $roles = db_select('role', 'r') - ->addTag('translatable') - ->fields('r') - ->orderBy('weight') - ->orderBy('name') - ->execute(); + $roles = config('user.roles')->get(); $form['roles'] = array( '#tree' => TRUE, ); $max_weight = 0; - foreach ($roles as $role) { + foreach ($roles as $rid => $role) { + $role = (object)$role; $max_weight = max($max_weight, $role->weight); - $form['roles'][$role->rid]['#role'] = $role; - $form['roles'][$role->rid]['#weight'] = $role->weight; - $form['roles'][$role->rid]['name'] = array( + $form['roles'][$rid]['#role'] = $role; + $form['roles'][$rid]['#weight'] = $role->weight; + $form['roles'][$rid]['name'] = array( '#markup' => check_plain($role->name), ); - $form['roles'][$role->rid]['weight'] = array( + $form['roles'][$rid]['weight'] = array( '#type' => 'textfield', '#title' => t('Weight for @title', array('@title' => $role->name)), '#title_display' => 'invisible', @@ -849,15 +845,15 @@ function user_admin_roles($form, $form_state) { '#default_value' => $role->weight, '#attributes' => array('class' => array('role-weight')), ); - $form['roles'][$role->rid]['edit'] = array( + $form['roles'][$rid]['edit'] = array( '#type' => 'link', '#title' => t('edit role'), - '#href' => 'admin/people/roles/edit/' . $role->rid, + '#href' => 'admin/people/roles/edit/' . $rid, ); - $form['roles'][$role->rid]['permissions'] = array( + $form['roles'][$rid]['permissions'] = array( '#type' => 'link', '#title' => t('edit permissions'), - '#href' => 'admin/people/permissions/' . $role->rid, + '#href' => 'admin/people/permissions/' . $rid, ); } diff --git a/core/modules/user/user.install b/core/modules/user/user.install index abb985b..322812f 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -87,39 +87,6 @@ function user_schema() { ), ); - $schema['role'] = array( - 'description' => 'Stores user roles.', - 'fields' => array( - 'rid' => array( - 'type' => 'varchar', - // The role ID is often used as part of a compound index; at least MySQL - // has a maximum index length of 1000 characters (333 on utf8), so we - // limit the maximum length. - 'length' => 64, - 'not null' => TRUE, - 'description' => 'Primary Key: Unique role ID.', - ), - 'name' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'default' => '', - 'description' => 'Role label.', - 'translatable' => TRUE, - ), - 'weight' => array( - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - 'description' => 'The weight of this role in listings and the user interface.', - ), - ), - 'primary key' => array('rid'), - 'indexes' => array( - 'name_weight' => array('name', 'weight'), - ), - ); - // The table name here is plural, despite Drupal table naming standards, // because "user" is a reserved word in many databases. $schema['users'] = array( @@ -326,13 +293,6 @@ function user_install() { 'data' => NULL, )) ->execute(); - - // Insert built-in roles. - db_insert('role') - ->fields(array('rid', 'name', 'weight')) - ->values(array(DRUPAL_ANONYMOUS_RID, 'Anonymous user', 0)) - ->values(array(DRUPAL_AUTHENTICATED_RID, 'Authenticated user', 1)) - ->execute(); } /** @@ -475,5 +435,25 @@ function user_update_8003() { } /** + * Replace the role table with configuration system as a beckend for storing roles. + */ +function user_update_8004() { + $config = config('user.roles'); + + $roles = db_select('role', 'r')->fields('r') + ->execute()->fetchAll(); + + foreach ($roles as $role) { + $config->set($role->rid, array( + 'name' => $role->name, + 'weight' => $role->weight, + )); + } + + $config->save(); + db_drop_table('role'); +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". */ diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 44cb266..87b31f4 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -2356,26 +2356,40 @@ function user_roles($membersonly = FALSE, $permission = NULL) { } } - $query = db_select('role', 'r'); - $query->addTag('translatable'); - $query->fields('r', array('rid', 'name')); - $query->orderBy('weight'); - $query->orderBy('name'); + $config = config('user.roles'); + $roles = $config->get(); + + if ($membersonly) { + unset($roles[DRUPAL_ANONYMOUS_RID]); + } + if (!empty($permission)) { - $query->innerJoin('role_permission', 'p', 'r.rid = p.rid'); + $filtered = array(); + $query = db_select('role_permission', 'p'); + $query->fields('p', array('rid')); $query->condition('p.permission', $permission); + $rids = $query->execute()->fetchCol(); + + foreach ($rids as $rid) { + $filtered[$rid] = $roles[$rid]; + } + $roles = $filtered; } - if ($membersonly) { - $query->condition('r.rid', DRUPAL_ANONYMOUS_RID, '!='); + + uksort($roles, function($a, $b) { + return $a['weight'] - $b['weight']; + }); + + $result = array(); + foreach ($roles as $rid => $role) { + $result[$rid] = $role['name']; } - $roles = $query->execute()->fetchAllKeyed(); if (empty($permission)) { - $user_roles[$cid] = $roles; - return $user_roles[$cid]; + $user_roles[$cid] = $result; } - return $roles; + return $result; } /** @@ -2389,11 +2403,18 @@ function user_roles($membersonly = FALSE, $permission = NULL) { * otherwise. */ function user_role_load($rid) { - return db_select('role', 'r') - ->fields('r') - ->condition('rid', $rid) - ->execute() - ->fetchObject(); + $config = config('user.roles'); + $role = $config->get($rid); + + if ($role) { + $role = (object)$role; + $role->rid = $rid; + } + else { + $role = FALSE; + } + + return $role; } /** @@ -2409,34 +2430,39 @@ function user_role_load($rid) { * performed. */ function user_role_save($role) { + $config = config('user.roles'); + $roles = $config->get(); + if ($role->name) { // Prevent leading and trailing spaces in role names. $role->name = trim($role->name); } if (!isset($role->weight)) { // Set a role weight to make this new role last. - $query = db_select('role'); - $query->addExpression('MAX(weight)'); - $role->weight = $query->execute()->fetchField() + 1; + $max = array_reduce($roles, function($max, $role) { + return $max > $role['weight'] ? $max : $role['weight']; + }); + $role->weight = $max + 1; } // Let modules modify the user role before it is saved to the database. module_invoke_all('user_role_presave', $role); - $exists = db_select('role', 'r') - ->fields('r', array('rid')) - ->condition('rid', $role->rid) - ->execute() - ->fetchAll(); + $exists = isset($roles[$role->rid]); + $config->set($role->rid, array( + 'name' => $role->name, + 'weight' => $role->weight, + )); - if (empty($exists)) { - $status = drupal_write_record('role', $role); + if (empty($existing)) { + $status = SAVED_NEW; module_invoke_all('user_role_insert', $role); } else { - $status = drupal_write_record('role', $role, 'rid'); + $status = SAVED_UPDATED; module_invoke_all('user_role_update', $role); } + $config->save(); // Clear the user access cache. drupal_static_reset('user_access'); @@ -2454,9 +2480,10 @@ function user_role_save($role) { function user_role_delete($role) { $role = user_role_load($role); - db_delete('role') - ->condition('rid', $role->rid) - ->execute(); + $config = config('user.roles'); + $config->clear($role->rid); + $config->save(); + db_delete('role_permission') ->condition('rid', $role->rid) ->execute(); @@ -2701,7 +2728,7 @@ function user_user_operations_block($accounts) { * Callback function for admin mass adding/deleting a user role. */ function user_multiple_role_edit($accounts, $operation, $rid) { - $role_name = db_query('SELECT name FROM {role} WHERE rid = :rid', array(':rid' => $rid))->fetchField(); + $role_name = user_role_load($rid)->name; switch ($operation) { case 'add_role': -- 1.7.11.1