The included subroutine takes a role id for a role which will be the model for a new role named, 1 $newrolename.
The new role gets created and the permissions for the role are copied from $rid.
(Not sure about the access of the role id at this point. It appears that role id's are autoincremented. And, so next_id is not being used. Is there a change for the new version having to do with the way the new id is captured?)
This code also reflects imagining that there is a built in taxonomy, 'userroles'. Of course, there should probably be a check for existence of some sort of taxonomy to do with managing the cloning path.
function user_clone_role($rid = 1,$newrolename) {
if ( is_numeric($rid) ) {
//////////////////
db_query("INSERT INTO {role} (name) VALUES ('%s')", $newrolename);
drupal_set_message(t('The role has been added.'));
////////////////
$result = db_query("SELECT rid FROM {role} where name = ('%s')", $newrolename);
$newrid = db_fetch_value($result);
$result = db_query('SELECT r.rid, r.name, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid WHERE r.rid = %d', $rid);
$role = db_fetch_object($result);
db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $newrid, $role->perm);
//////////////////
if ( function_exists('taxonomy_save_term') ) {
$vid = taxonomy_id_of_vocabulary('userroles');
if ( $vid != null ) {
$pid = taxonomy_id_of_term($vid,$role->name);
if ( $pid != null ) {
$edit = array('vid' => $vid, 'name' => $newrolename, 'parent' => $pid);
taxonomy_save_term($edit);
}
}
}
//////////////////
}
}
Comments
Comment #1
catchnot a patch, new features go in development version.
Comment #2
marcingy commentedBumping to d8
Comment #3
jhedstromThis should be pretty easy now that roles are proper entities.