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

catch’s picture

Version: 6.x-dev » 7.x-dev
Status: Needs review » Active

not a patch, new features go in development version.

marcingy’s picture

Version: 7.x-dev » 8.x-dev

Bumping to d8

jhedstrom’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)

This should be pretty easy now that roles are proper entities.