Index: modules/user/user.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.admin.inc,v retrieving revision 1.72 diff -u -r1.72 user.admin.inc --- modules/user/user.admin.inc 22 Aug 2009 14:34:23 -0000 1.72 +++ modules/user/user.admin.inc 22 Aug 2009 14:54:11 -0000 @@ -610,7 +610,7 @@ $role_names = array($rid => $role_names[$rid]); } // Fetch permissions for all roles or the one selected role. - $role_permissions = user_role_permissions($role_names); + $role_permissions = user_role_get_permissions($role_names); // Store $role_names for use when saving the data. $form['role_names'] = array( @@ -663,19 +663,8 @@ */ function user_admin_permissions_submit($form, &$form_state) { foreach ($form_state['values']['role_names'] as $rid => $name) { - $checked = array_filter($form_state['values'][$rid]); - // Delete existing permissions for the role. This handles "unchecking" checkboxes. - db_delete('role_permission') - ->condition('rid', $rid) - ->execute(); - $query = db_insert('role_permission')->fields(array('rid', 'permission')); - foreach ($checked as $permission) { - $query->values(array( - 'rid' => $rid, - 'permission' => $permission, - )); - } - $query->execute(); + $permissions = array_filter($form_state['values'][$rid]); + user_role_set_permissions($rid, $permissions); } drupal_set_message(t('The changes have been saved.')); @@ -776,13 +765,13 @@ function user_admin_role_validate($form, &$form_state) { if ($form_state['values']['name']) { if ($form_state['values']['op'] == t('Save role')) { - $existing_role = (bool) db_query_range("SELECT 1 FROM {role} WHERE name = :name AND rid <> :rid", array(':name' => $form_state['values']['name'], ':rid' => $form_state['values']['rid']), 0, 1)->fetchField(); - if ($existing_role) { + $rid = user_role_get_id($form_state['values']['name']); + if ($rid && $rid != $form_state['values']['rid']) { form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name']))); } } elseif ($form_state['values']['op'] == t('Add role')) { - if ((bool) db_query_range('SELECT 1 FROM {role} WHERE name = :name', array(':name' => $form_state['values']['name']), 0, 1)->fetchField()) { + if (user_role_get_id($form_state['values']['name'])) { form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name']))); } } @@ -793,31 +782,17 @@ } function user_admin_role_submit($form, &$form_state) { + $role = (object)$form_state['values']; if ($form_state['values']['op'] == t('Save role')) { - db_update('role') - ->fields(array('name' => $form_state['values']['name'])) - ->condition('rid', $form_state['values']['rid']) - ->execute(); + user_role_save($role); drupal_set_message(t('The role has been renamed.')); } elseif ($form_state['values']['op'] == t('Delete role')) { - db_delete('role') - ->condition('rid', $form_state['values']['rid']) - ->execute(); - db_delete('role_permission') - ->condition('rid', $form_state['values']['rid']) - ->execute(); - // Update the users who have this role set: - db_delete('users_roles') - ->condition('rid', $form_state['values']['rid']) - ->execute(); - + user_role_delete($form_state['values']['rid']); drupal_set_message(t('The role has been deleted.')); } elseif ($form_state['values']['op'] == t('Add role')) { - db_insert('role') - ->fields(array('name' => $form_state['values']['name'])) - ->execute(); + user_role_save($role); drupal_set_message(t('The role has been added.')); } $form_state['redirect'] = 'admin/config/people/roles'; Index: modules/user/user.module =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.module,v retrieving revision 1.1026 diff -u -r1.1026 user.module --- modules/user/user.module 22 Aug 2009 14:34:23 -0000 1.1026 +++ modules/user/user.module 22 Aug 2009 14:54:33 -0000 @@ -679,15 +679,14 @@ * Determine the permissions for one or more roles. * * @param $roles - * An array whose keys are the role IDs of interest, such as $user->roles. + * An array of role IDs. * @param $reset * Optional parameter - if TRUE data in the static variable is rebuilt. - * * @return * An array indexed by role ID. Each value is an array whose keys are the * permission strings for the given role ID. */ -function user_role_permissions($roles = array(), $reset = FALSE) { +function user_role_get_permissions(array $roles = array(), $reset = FALSE) { static $stored_permissions = array(); if ($reset) { @@ -698,7 +697,7 @@ $role_permissions = $fetch = array(); if ($roles) { - foreach ($roles as $rid => $name) { + foreach ($roles as $rid) { if (isset($stored_permissions[$rid])) { $role_permissions[$rid] = $stored_permissions[$rid]; } @@ -767,7 +766,7 @@ // To reduce the number of SQL queries, we cache the user's permissions // in a static variable. if (!isset($perm[$account->uid])) { - $role_permissions = user_role_permissions($account->roles, $reset); + $role_permissions = user_role_get_permissions(array_keys($account->roles), $reset); $perms = array(); foreach ($role_permissions as $one_role) { @@ -2256,6 +2255,131 @@ } /** + * Get the role ID of a role name. + * + * @param $role_name + * The name of the role. + * @return + * An integer of the role's ID, or FALSE if the role was not found. + */ +function user_role_get_id($role_name) { + return db_select('role', 'r') + ->fields('r', array('rid')) + ->condition('name', $role_name) + ->execute() + ->fetchField(); +} + +/** + * Fetch a user role from database. + * + * @param $role + * A string with the role name, or an integer with the role ID. + */ +function user_role_load($role) { + $rid = is_numeric($role) ? $role : user_role_get_id($role); + return db_select('role', 'r') + ->fields('r') + ->condition('rid', $rid) + ->execute() + ->fetchObject(); +} +/** + * Save a user role to the database. + * + * @param $role + * A role object to modify or add. If $role->rid is not specified, a new + * role will be created. + * @param $permissions + * An optional array of permissions to assign to the role. + * @return + * Status constant indicating if role was created or updated. + */ +function user_role_save($role, $permissions) { + if ($role->name) { + // Prevent leading and trailing spaces in role names. + $role->name = trim($role->name); + } + if (!empty($role->rid) && $role->name) { + $status = drupal_write_record('role', $role, 'rid'); + module_invoke_all('user_role_update', $role); + } + else { + $status = drupal_write_record('role', $role); + module_invoke_all('user_role_insert', $role); + } + + if (isset($permissions)) { + // Assign the permissions to the role. + user_role_set_permissions($role, $permissions); + } + return $status; +} + +/** + * Delete a user role from database. + * + * @param $role + * A string with the role name, or an integer with the role ID. + */ +function user_role_delete($role) { + $rid = is_numeric($role) ? $role : user_role_get_id($role); + $role = user_role_load($rid); + + db_delete('role') + ->condition('rid', $rid) + ->execute(); + db_delete('role_permission') + ->condition('rid', $rid) + ->execute(); + // Update the users who have this role set: + db_delete('users_roles') + ->condition('rid', $rid) + ->execute(); + + // Clear the user access cache. + user_access(NULL, NULL, TRUE); + + module_invoke_all('user_role_delete', $role); +} + +/** + * Assign permissions to a user role. + * + * @param $role + * A string with the role name, or an integer with the role ID. + * @param $permissions + * An array of permissions strings. + * @param $merge + * A boolean indicating whether to add permissions or to merge + * with all existing permissions. + */ +function user_role_set_permissions($role, array $permissions = array(), $merge = FALSE) { + $rid = is_numeric($role) ? $role : user_role_get_id($role); + if (!$merge) { + // Delete existing permissions for the role. + db_delete('role_permission') + ->condition('rid', $rid) + ->execute(); + } + + // Assign the new permissions for the role. + foreach ($permissions as $permission_string) { + db_merge('role_permission') + ->key(array( + 'rid' => $rid, + 'permission' => $permission_string, + )) + ->execute(); + } + + // Clear the user access cache. + user_access(NULL, NULL, TRUE); + + return TRUE; +} + +/** * Implement hook_user_operations(). */ function user_user_operations($form_state = array()) { Index: modules/user/user.api.php =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.api.php,v retrieving revision 1.11 diff -u -r1.11 user.api.php --- modules/user/user.api.php 22 Aug 2009 14:34:23 -0000 1.11 +++ modules/user/user.api.php 22 Aug 2009 14:54:12 -0000 @@ -423,5 +423,45 @@ } /** + * Act on user roles when inserted. + * + * Modules implementing this hook can act on the user role object when saved to + * the database. + * + * @param $role + * A user role object. + */ +function hook_user_role_insert($role) { +} + +/** + * Act on user roles when updated. + * + * Modules implementing this hook can act on the user role object when updated. + * + * @param $role + * A user role object. + */ +function hook_user_role_update($role) { +} + +/** + * Inform other modules that a user role has been deleted. + * + * This hook allows you act when a user role has been deleted. + * It is recommended that you implement this hook if your module + * stores references to roles. + * + * @param $role + * The $role object being deleted. + */ +function hook_user_role_delete($role) { + // Delete existing instances of the deleted role. + db_delete('my_module_table') + ->condition('rid', $role->rid) + ->execute(); +} + +/** * @} End of "addtogroup hooks". */ Index: profiles/default/default.install =================================================================== RCS file: /cvs/drupal/drupal/profiles/default/default.install,v retrieving revision 1.1 diff -u -r1.1 default.install --- profiles/default/default.install 21 Aug 2009 07:50:08 -0000 1.1 +++ profiles/default/default.install 22 Aug 2009 14:54:38 -0000 @@ -183,20 +183,16 @@ ))->execute(); db_insert('taxonomy_vocabulary_node_type')->fields(array('vid' => $vid, 'type' => 'article'))->execute(); - // Create a default role for site administrators. - $rid = db_insert('role')->fields(array('name' => 'administrator'))->execute(); + // Enable default permissions for system roles. + user_role_set_permissions(DRUPAL_ANONYMOUS_RID, array('access content')); + user_role_set_permissions(DRUPAL_AUTHENTICATED_RID, array('access content', 'access comments', 'post comments', 'post comments without approval')); + // Create a default role for site administrators, with all available permissions assigned. + $admin_role = new stdClass(); + $admin_role->name = 'administrator'; + user_role_save($admin_role, array_keys(module_invoke_all('permission'))); // Set this as the administrator role. - variable_set('user_admin_role', $rid); - - // Assign all available permissions to this role. - foreach (module_invoke_all('permission') as $key => $value) { - db_insert('role_permission') - ->fields(array( - 'rid' => $rid, - 'permission' => $key, - ))->execute(); - } + variable_set('user_admin_role', $admin_role->rid); // Update the menu router information. menu_rebuild(); Index: modules/block/block.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/block/block.admin.inc,v retrieving revision 1.49 diff -u -r1.49 block.admin.inc --- modules/block/block.admin.inc 22 Aug 2009 14:34:18 -0000 1.49 +++ modules/block/block.admin.inc 22 Aug 2009 14:53:20 -0000 @@ -534,3 +534,12 @@ $variables['form_submit'] = drupal_render_children($variables['form']); } + +/** + * Implement hook_user_role_delete(). + */ +function block_user_role_delete($role) { + db_delete('block_role') + ->condition('rid', $role->rid) + ->execute(); +} \ No newline at end of file Index: modules/field/modules/text/text.module =================================================================== RCS file: /cvs/drupal/drupal/modules/field/modules/text/text.module,v retrieving revision 1.22 diff -u -r1.22 text.module --- modules/field/modules/text/text.module 22 Aug 2009 14:34:20 -0000 1.22 +++ modules/field/modules/text/text.module 22 Aug 2009 14:53:25 -0000 @@ -308,6 +308,22 @@ ); } +function text_filter_format_delete($format) { + //@TODO: implement. + $conditions = array(array('format', $format)); + // Load info for all text_long field types. + $fields = field_read_fields(array('type' => 'text_long')) + + field_read_fields(array('type' => 'text_with_summary'));; + foreach ($fields as $field_name => $field) { + $results = field_attach_query($field['id'], $conditions); + } + + foreach ($fields as $field_name => $field) { + + } + +} + /** * Theme function for 'default' text field formatter. */ Index: modules/system/system.install =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.install,v retrieving revision 1.374 diff -u -r1.374 system.install --- modules/system/system.install 21 Aug 2009 17:28:27 -0000 1.374 +++ modules/system/system.install 22 Aug 2009 14:54:03 -0000 @@ -360,47 +360,31 @@ db_query("UPDATE {users} SET uid = 1 WHERE name = '%s'", 'placeholder-for-uid-1'); // Built-in roles. - $rid_anonymous = db_insert('role') - ->fields(array('name' => 'anonymous user')) - ->execute(); - - $rid_authenticated = db_insert('role') - ->fields(array('name' => 'authenticated user')) - ->execute(); + $anonymous_user_role = new stdClass(); + $anonymous_user_role->name = 'anonymous user'; + user_role_save($anonymous_user_role); + + $authenticated_user_role = new stdClass(); + $authenticated_user_role->name = 'authenticated user'; + user_role_save($authenticated_user_role); // Sanity check to ensure the anonymous and authenticated role IDs are the // same as the drupal defined constants. In certain situations, this will // not be true. - if ($rid_anonymous != DRUPAL_ANONYMOUS_RID) { + if ($anonymous_user_role->rid != DRUPAL_ANONYMOUS_RID) { db_update('role') ->fields(array('rid' => DRUPAL_ANONYMOUS_RID)) - ->condition('rid', $rid_anonymous) + ->condition('rid', $anonymous_user_role->rid) ->execute(); } - if ($rid_authenticated != DRUPAL_AUTHENTICATED_RID) { + if ($authenticated_user_role->rid != DRUPAL_AUTHENTICATED_RID) { db_update('role') ->fields(array('rid' => DRUPAL_AUTHENTICATED_RID)) - ->condition('rid', $rid_authenticated) + ->condition('rid', $authenticated_user_role->rid) ->execute(); } - $query = db_insert('role_permission')->fields(array('rid', 'permission')); - // Anonymous role permissions. - $query->values(array( - 'rid' => DRUPAL_ANONYMOUS_RID, - 'permission' => 'access content', - )); - - // Authenticated role permissions. - foreach (array('access comments', 'access content', 'post comments', 'post comments without approval', 'view own unpublished content') as $permission) { - $query->values(array( - 'rid' => DRUPAL_AUTHENTICATED_RID, - 'permission' => $permission, - )); - } - $query->execute(); - variable_set('theme_default', 'garland'); db_update('system') Index: modules/simpletest/drupal_web_test_case.php =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v retrieving revision 1.141 diff -u -r1.141 drupal_web_test_case.php --- modules/simpletest/drupal_web_test_case.php 22 Aug 2009 00:58:54 -0000 1.141 +++ modules/simpletest/drupal_web_test_case.php 22 Aug 2009 14:53:50 -0000 @@ -892,26 +892,18 @@ $name = $this->randomName(); } + // Check the all the permissions strings are valid. if (!$this->checkPermissions($permissions)) { return FALSE; } // Create new role. - db_insert('role') - ->fields(array('name' => $name)) - ->execute(); - $role = db_query('SELECT * FROM {role} WHERE name = :name', array(':name' => $name))->fetchObject(); - $this->assertTrue($role, t('Created role of name: @name, id: @rid', array('@name' => $name, '@rid' => (isset($role->rid) ? $role->rid : t('-n/a-')))), t('Role')); + $role = new stdClass(); + $role->name = $name; + user_role_save($role, $permissions); + + $this->assertTrue(user_role_get_id($name), t('Created role of name: @name, id: @rid', array('@name' => $name, '@rid' => (isset($role->rid) ? $role->rid : t('-n/a-')))), t('Role')); if ($role && !empty($role->rid)) { - // Assign permissions to role and mark it for clean-up. - $query = db_insert('role_permission')->fields(array('rid', 'permission')); - foreach ($permissions as $permission_string) { - $query->values(array( - 'rid' => $role->rid, - 'permission' => $permission_string, - )); - } - $query->execute(); $count = db_query('SELECT COUNT(*) FROM {role_permission} WHERE rid = :rid', array(':rid' => $role->rid))->fetchField(); $this->assertTrue($count == count($permissions), t('Created permissions: @perms', array('@perms' => implode(', ', $permissions))), t('Role')); return $role->rid; Index: modules/filter/filter.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/filter/filter.admin.inc,v retrieving revision 1.34 diff -u -r1.34 filter.admin.inc --- modules/filter/filter.admin.inc 22 Aug 2009 14:34:20 -0000 1.34 +++ modules/filter/filter.admin.inc 22 Aug 2009 14:53:29 -0000 @@ -452,3 +452,14 @@ cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE); } + +/** + * Implement hook_user_role_delete(). + */ + +function filter_user_role_delete($role) { + db_update('filter_format') + ->expression('roles', 'REPLACE(roles, :rid, :repl)', array(':rid' => ',' . $role->rid, ':repl' => '')) + ->condition('roles', '%,' . $role->rid . '%', 'LIKE') + ->execute(); +} \ No newline at end of file