View source
<?php
function duplicate_role_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/user/duplirole":
$output = '<p>' . t("Duplicate an existing role with the same permissions for modules and taxonomies as the original ones") . '</p>';
break;
}
return $output;
}
function duplicate_role_perm() {
return array(
'access duplicate role',
);
}
function duplicate_role_menu() {
$items = array();
$items['admin/user/duplirole'] = array(
'title' => t('Duplicate Role'),
'description' => t('A module that duplicates an existing role and its permissions.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'duplicate_role_form',
),
'access arguments' => array(
'access duplicate role',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function duplicate_role_form_submit($form, &$form_state) {
$rolnovo_nome = $form_state['values']['nome'];
$rolvello_id = $form_state['values']['varid'];
if (db_table_exists('role')) {
$sql = "INSERT INTO {role} (name) VALUES ('%s')";
$result = db_query($sql, $rolnovo_nome);
$sql = "SELECT r.rid FROM {role} r WHERE name='%s'";
$result = db_query($sql, $rolnovo_nome);
while ($row = db_fetch_object($result)) {
$rolnovo_id = $row->rid;
}
}
if (db_table_exists('permission')) {
$sql = "SELECT p.perm, p.tid FROM {permission} p WHERE rid='%d'";
$result = db_query($sql, $rolvello_id);
while ($row = db_fetch_object($result)) {
$rolvello_perm = $row->perm;
$rolvello_tid = $row->tid;
}
$sql = "INSERT INTO {permission} (rid,perm,tid) VALUES ('%d','%s','%d')";
$result = db_query($sql, $rolnovo_id, $rolvello_perm, $rolvello_tid);
}
if (db_table_exists('term_access')) {
$sql = "INSERT INTO {term_access} \n\t\t(tid,rid,grant_view,grant_update,grant_delete,grant_create,grant_list) \n\t\tSELECT tid, '%d',grant_view,grant_update,grant_delete,grant_create,grant_list\n\t\tFROM term_access where rid='%d'";
$result = db_query($sql, $rolnovo_id, $rolvello_id);
}
if (db_table_exists('term_access_defaults')) {
$sql = "INSERT INTO {term_access_defaults} \n\t\t(vid,rid,grant_view,grant_update,grant_delete,grant_create,grant_list) \n\t\tSELECT vid, '%d',grant_view,grant_update,grant_delete,grant_create,grant_list\n\t\tFROM {term_access_defaults} where rid='%d'";
$result = db_query($sql, $rolnovo_id, $rolvello_id);
}
drupal_set_message(t('New role added successfully.'));
}
function duplicate_role_form() {
$form = array();
$u_roles = user_roles();
asort($u_roles);
$options = array();
$options[] = t('-- Please Select One Role --');
foreach ($u_roles as $key => $value) {
$options[$key] = $value;
}
$form['varid'] = array(
'#type' => 'select',
'#title' => t('Choose role to duplicate'),
'#default_value' => variable_get('copiarol_varid', 0),
'#description' => t("Select role to duplicate"),
'#options' => $options,
);
$form['nome'] = array(
'#type' => 'textfield',
'#title' => t('New role'),
'#default_value' => variable_get('copiarol_nome', ''),
'#required' => true,
'#size' => 40,
'#maxlength' => 40,
'#description' => t("New role name"),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create new role'),
);
return $form;
}