View source
<?php
function subuser_perm() {
return array(
'administer subuser',
'manage subuser',
);
}
function subuser_menu($may_cache) {
$items = array();
$items[] = array(
'path' => 'admin/settings/subuser',
'title' => t('Admin settings - Sub User'),
'description' => t('Admin settings - Sub User'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'subuser_admin_settings',
),
'access' => user_access('administer subuser'),
);
$items[] = array(
'path' => 'subuser/list',
'title' => t('My Subusers'),
'description' => t('My Subusers'),
'callback' => 'subuser_listall',
'access' => user_access('manage subuser'),
);
$items[] = array(
'path' => 'subuser/add',
'title' => t('Add New Sub-User'),
'description' => t('Add New Sub-User'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'subuser_add',
),
'access' => user_access('manage subuser'),
);
$items[] = array(
'path' => 'subuser/' . arg(1) . '/add',
'title' => t('Edit Subuser'),
'description' => t('Edit Subuser'),
'callback' => 'subuser_edit',
'callback arguments' => arg(1),
'access' => user_access('manage subuser'),
);
$items[] = array(
'path' => 'subuser/' . arg(1) . '/delete',
'title' => t('Edit Subuser'),
'description' => t('Edit Subuser'),
'callback' => 'subuser_delete',
'callback arguments' => arg(1),
'access' => user_access('manage subuser'),
);
return $items;
}
function subuser_delete($uid) {
return drupal_get_form('subuser_confirm_delete', $uid, $uid);
}
function subuser_confirm_delete($name, $uid) {
$user = user_load(array(
"uid" => $uid,
));
$form['uid'] = array(
'#type' => 'value',
'#value' => $uid,
);
return confirm_form($form, t('Are you sure you want to delete the account %name?', array(
'%name' => $user->name,
)), 'user/' . $uid, t('All submissions made by this user will be attributed to the anonymous account. This action cannot be undone.'), t('Delete'), t('Cancel'));
}
function subuser_confirm_delete_submit($form_id, $form_values) {
$account = user_load(array(
'uid' => $form_values['uid'],
));
user_delete((array) $account, $form_values['uid']);
return 'subuser/list';
}
function subuser_listall() {
global $user;
$header = array(
"Username",
"Edit",
"Delete",
);
$myUID = $user->uid;
$result = db_query("SELECT u.uid,u.name FROM {users} u, {subuser} su WHERE u.uid=su.uid AND refuid='%s'", $myUID);
while ($account = db_fetch_object($result)) {
$rows[] = array(
$account->name,
l("Edit", "subuser/{$account->uid}/edit"),
l("Delete", "subuser/{$account->uid}/delete"),
);
}
return theme('table', $header, $rows);
}
function subuser_user($type, &$edit, &$account, $category = NULL) {
global $user;
$refuid = $edit["mainuid"];
if ($user->uid != 1) {
$refuid = $user->uid;
}
if ($type == 'insert') {
db_query("INSERT INTO {subuser} SET uid=%d,refuid='%d'", $account->uid, $refuid);
}
if ($type == 'delete') {
db_query("DELETE FROM {subuser} SET uid=%d,refuid='%d'", $account->uid, $refuid);
}
}
function subuser_add() {
global $user;
$form['name'] = array(
"#type" => "textfield",
"#title" => "Username",
);
$form['mail'] = array(
"#type" => "textfield",
"#title" => "E-mail Address",
);
$form['pass'] = array(
"#type" => "password",
"#title" => "Password",
);
if ($user->uid == 1) {
$result = db_query("SELECT uid,name FROM users ORDER BY name");
while ($uu = db_fetch_object($result)) {
if ($uu->uid == 0 || $uu->uid == 1) {
continue;
}
$options[$uu->uid] = $uu->name;
}
$form['mainuid'] = array(
"#type" => "select",
"#options" => $options,
"#title" => "Main Account",
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add Sub User'),
'#weight' => 30,
);
return $form;
}
function subuser_add_validate($form_id, $form_values) {
$edit = $form_values;
if ($error = user_validate_name($edit['name'])) {
form_set_error('name', $error);
}
else {
if (is_object(user_load(array(
'name' => $edit['name'],
)))) {
form_set_error('name', t('The name %name is already taken.', array(
'%name' => $edit['name'],
)));
}
else {
if (drupal_is_denied('user', $edit['name'])) {
form_set_error('name', t('The name %name has been denied access.', array(
'%name' => $edit['name'],
)));
}
}
}
if ($error = user_validate_mail($edit['mail'])) {
form_set_error('mail', $error);
}
else {
if (is_object(user_load(array(
'mail' => $edit['mail'],
)))) {
form_set_error('mail', t('The e-mail address %email is already registered. ', array(
'%email' => $edit['mail'],
'@password' => url('user/password'),
)));
}
else {
if (drupal_is_denied('mail', $edit['mail'])) {
form_set_error('mail', t('The e-mail address %email has been denied access.', array(
'%email' => $edit['mail'],
)));
}
}
}
}
function subuser_add_submit($form_id, $form_values) {
$roleID = variable_get("subuser_roleid", '');
$roles["{$roleID}"] = $roleID;
$mail = $form_values["mail"];
$pass = $form_values["pass"];
$merge_data = array(
'pass' => $pass,
'init' => $mail,
'roles' => $roles,
'status' => 1,
);
$account = user_save('', array_merge($form_values, $merge_data));
if ($account->uid) {
drupal_set_message(t("New subuser added"));
}
else {
drupal_set_message(t("Error adding subuser"));
}
return 'subuser/list';
}
function subuser_admin_settings() {
$form['subuser_num'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Number of sub-users a member can create'),
'#default_value' => variable_get('subuser_num', "5"),
'#description' => t('Number of sub-users a member can create'),
);
$form['subuser_roleid'] = array(
'#type' => 'select',
'#options' => user_roles(),
'#title' => t('Subuser Role'),
'#default_value' => variable_get('subuser_roleid', ''),
'#description' => t('New roles can be added from ') . l("admin/user/roles", "admin/user/roles"),
);
return system_settings_form($form);
}