Merge permissions of Drupal roles
Merge permissions from multiple Drupal roles
First specity the new role which will be the source and destination for the merge
$source_role_names = array('blogger', 'editor') $dest_role_name = 'merged';
Fetch the rid from the names
foreach ($source_role_names as $name) { $rid = user_role_load_by_name($name)->rid; $source_role_ids[$rid] = $rid; }
Create the role if necessary.
$role = user_role_load_by_name($dest_role_name); if (!$role) { $role = new stdClass(); $role->name = $dest_role_name; user_role_save($role); }
Get all the permissions from all source roles
$result = db_query('SELECT permission FROM {role_permission} WHERE rid IN (:rid)', array(':rid' => $source_role_ids))->fetchAll(); foreach ($result as $record) { $role_permissions[] = $record->permission; }
Grant permissions for the new role (merged)
user_role_grant_permissions($role->rid, $role_permissions);
Category: