Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
To completed the CRUD functions for roles, and for a use case I had:
Index: modules/permissions_api/permissions_api.drush.inc
===================================================================
--- modules/permissions_api/permissions_api.drush.inc (revision 18809)
+++ modules/permissions_api/permissions_api.drush.inc (working copy)
@@ -25,6 +25,8 @@
return dt("List permissions assigned to a role.");
case 'drush:perm-create':
return dt("Create a role.");
+ case 'drush:perm-delete':
+ return dt("Delete a role.");
}
}
@@ -98,6 +100,17 @@
),
);
+ $items['perm-delete'] = array(
+ 'callback' => 'drush_permissions_api_perm_delete',
+ 'description' => dt('Delete a role.'),
+ 'arguments' => array(
+ 'role' => dt('The name of the role you are deleting.'),
+ ),
+ 'examples' => array(
+ 'drush perm-delete Admin' => dt('Delete Admin role.'),
+ ),
+ );
+
$items['perm-list'] = array(
'callback' => 'drush_permissions_api_perm_list',
'description' => dt('List permissions for a role.'),
@@ -175,3 +188,11 @@
drush_print(dt('Role !role created.', array('!role' => $role)));
}
}
+
+function drush_permissions_api_perm_delete() {
+ $args = func_get_args();
+ foreach($args as $role) {
+ permissions_delete_role($role);
+ drush_print(dt('Role !role delete.', array('!role' => $role)));
+ }
+}
\ No newline at end of file
Index: modules/permissions_api/permissions_api.module
===================================================================
--- modules/permissions_api/permissions_api.module (revision 18809)
+++ modules/permissions_api/permissions_api.module (working copy)
@@ -329,6 +329,29 @@
}
/**
+ * Helper function to create a new role in the database
+ *
+ * @param
+ * String representing the name of the role to be created. Ex: 'site administrator'
+ * @return
+ * Returns either an object representing the newly created (or previously existing) role, or FALSE if there was an error
+ */
+function permissions_delete_role($role_name) {
+ // Look up the role to see if it exists already
+ $role = permissions_get_role($role_name);
+ if($role) {
+ db_query('DELETE FROM {role} WHERE rid = %d', $role->rid);
+ db_query('DELETE FROM {permission} WHERE rid = %d', $role->rid);
+ // Update the users who have this role set:
+ db_query('DELETE FROM {users_roles} WHERE rid = %d', $role->rid);
+ watchdog('permissions_api', 'permissions_delete_role: role %rolename deleted', array('%rolename' => $role->name), WATCHDOG_ERROR);
+ return $role;
+ }
+ watchdog('permissions_api', 'permissions_delete_role: role %rolename not found', array('%rolename' => $role_name), WATCHDOG_WARNING);
+ return FALSE;
+}
+
+/**
* This function allows a role to inherit either all the permissions of another role
*
* @param
Comment | File | Size | Author |
---|---|---|---|
#4 | permissions_api_role_delete_824538.patch | 3.49 KB | langworthy |
#3 | permissions_api_role_delete_824538.patch | 3.49 KB | jonathan_hunt |
Comments
Comment #1
ebeyrent CreditAttribution: ebeyrent commentedHi there!
Can you please attach this as a patch file to make it easier for people to review and download?
In looking at this, I see a few things that should be changed. Some of the comments are inconsistent with the function; for example, the comments for the permissions_delete_role() function don't match. Also, why does the function return the $role object? It should return a true or false, depending on whether the delete was successful.
Function drush_permissions_api_perm_delete() is confusing, because it looks like the function deletes a permission, when it actually deletes a role. I realize that drush_permissions_api_perm_create() is just as confusing (my bad) - let's fix that one too to clear up any ambiguity.
Other than that, the patch looks reasonable, so let's clean it up a little, and I'll be happy to commit it.
Thanks for the contribution!
Comment #2
ebeyrent CreditAttribution: ebeyrent commentedComment #3
jonathan_hunt CreditAttribution: jonathan_hunt commentedPlease try the attached patch. The perm-create is changed to role-create, and role-delete is added.
Comment #4
langworthy CreditAttribution: langworthy commentedLooks good. I tested using both drush and the api call in an update_N().
I changed the drush_print() text Role !role delete. to Role !role deleted. on line 66 of the patch and cleaned up a coding standard issue at line 64. (Control statements should have one space between the control keyword and opening parenthesis). This module has many such issues but I figure there's no use introducing more.
updated patch attached.
Comment #5
ebeyrent CreditAttribution: ebeyrent commentedNice work -see commit #401114. Released in v6.x-2.10.
Thanks for the contribution!!
Comment #6
ebeyrent CreditAttribution: ebeyrent commented