欢迎各位兄弟 发布技术文章
这里的技术是共享的
<?php //定义权限 仅仅指明什么权限对这个模块可用 function shipingzhongcustomfour_perm() { return array('custom create user');; } //老师角色 function shipingzhongcustomfour_laoshirole() { global $user; //教师角色 或超级管理员角色 if (in_array('教师',$user->roles) || $user->uid==1 ) { return true; }else{ return false; } } function shipingzhongcustomfour_menu() { $items['shipingzhongcustomfour/createuser'] = array( 'title' => '创建用户', 'page callback' => 'drupal_get_form', 'page arguments' => array('shipingzhongcustomfour_createuser_form'), 'access arguments' => array('custom create user'), ); $items['shipingzhongcustomfour/batchcreateuser'] = array( 'title' => '批量创建用户', 'page callback' => 'drupal_get_form', 'page arguments' => array('shipingzhongcustomfour_batchcreateuser_form'), 'access arguments' => array('custom create user'), ); $items['shipingzhongcustomfour/newuserpermission'] = array( 'title' => '新创建的40位用户授权', 'page callback' => 'drupal_get_form', 'page arguments' => array('shipingzhongcustomfour_newuserpermission_form'), 'access callback' => 'shipingzhongcustomfour_laoshirole' ); return $items; } function shipingzhongcustomfour_createuser_form() { $form['name'] = array( '#type' => 'textfield', '#title' => '用户名', '#size' => 60, ); $form['mail'] = array( '#type' => 'textfield', '#title' => '电子邮件', '#size' => 60, ); $form['pass'] = array( '#type' => 'textfield', '#title' => '密码', '#size' => 60, ); // $form['profile_xingming'] = array( // '#type' => 'textfield', // '#title' => '姓名', // ); // // $form['profile_gerenjianjie'] = array( // '#type' => 'textarea', // '#title' => '个人简价', // ); $form['submit'] = array('#type' => 'submit', '#value' => '创建'); $form['link'] = array('#type' => 'markup', '#value' => ' <a href="/user"><font color="blue">返回用户中心</font></a>'); return $form; } function shipingzhongcustomfour_createuser_form_validate($form, &$form_state) { $name = ($form_state['values']['name']); if (empty($name)) { // form_set_error('name', '请输入用户名!'); } if (!empty($name)) { // $sql = "select uid from {users} where name='%s'"; $uid = db_result(db_query($sql,$name)); if(!empty($uid)){ form_set_error('name', '用户名'.$name.'已存在!'); } } $mail = trim($form_state['values']['mail']); if (empty($mail)) { // form_set_error('mail', '请输入邮件!'); } if (!empty($mail)) { // if(!valid_email_address($mail)){ form_set_error('mail', '邮件'.$mail.'格式不合法!'); } } if (!empty($mail)) { // // form_set_error('name', '请输入整数!'); $sql = "select uid from {users} where mail='%s'"; $uid = db_result(db_query($sql,$mail)); if(!empty($uid)){ form_set_error('mail', '邮件'.$mail.'已存在!'); } } $pass = trim($form_state['values']['pass']); if (empty($pass)) { // form_set_error('pass', '密码不能为空!'); } // $profile_xingming = trim($form_state['values']['profile_xingming']); // if (empty($profile_xingming)) { // // form_set_error('profile_xingming', '请输入姓名!'); // } // $profile_gerenjianjie = trim($form_state['values']['profile_gerenjianjie']); // if (empty($profile_gerenjianjie)) { // // form_set_error('profile_gerenjianjie', '请输入个人简介!'); // } } function shipingzhongcustomfour_createuser_form_submit($form, &$form_state) { $fields = array(); $fields['name'] = trim($form_state['values']['name']); $fields['mail'] = trim($form_state['values']['mail']); $fields['pass'] = trim($form_state['values']['pass']);//密码不用md5 $currentTime = time(); $fields['created'] = $currentTime; $fields['access'] = $currentTime; $fields['login'] = 0; $fields['status'] = 1; $fields['timezone'] = '28800'; $fields['timezone_name'] = 'Asia/Chongqing'; $fields['language'] = 'zh-hans'; $fields['roleids'] = array(2 => 'authenticated user'); $fields['profile_xingming'] = $fields['name']; $fields['profile_gerenjianjie'] = $fields['name']; $account = user_save('', $fields); if ($account->uid) { drupal_set_message('创建用户成功!用户名是:' . $fields['name']); } else { drupal_set_message('创建用户失败', 'error'); } } function shipingzhongcustomfour_batchcreateuser_form() { for ($i = 1; $i <= 20; $i++) { if($i<10){ $space = $i.' '; }else{ $space = $i.' '; } $form['order'.$i]['name_'.$i] = array( '#type' => 'textfield', '#title' => $space.'用户名', '#size' => 20 ); $form['order'.$i]['mail_'.$i] = array( '#type' => 'textfield', '#title' => '电子邮件', '#size' => 20 ); $form['order'.$i]['pass_'.$i] = array( '#type' => 'textfield', '#title' => '密码', '#size' => 20 ); } $form['#attributes'] = array('class' => 'batchcreateuserform'); // $form['profile_xingming'] = array( // '#type' => 'textfield', // '#title' => '姓名', // ); // // $form['profile_gerenjianjie'] = array( // '#type' => 'textarea', // '#title' => '个人简价', // ); $form['submit'] = array('#type' => 'submit', '#value' => '创建','#attributes'=>array('class' => 'form-item')); $form['link'] = array('#type' => 'markup', '#value' => ' <a href="/user" class="gobackceterlink"><font color="blue">返回用户中心</font></a>'); $form['#suffix'] = '<div class="clear"></div>'; return $form; } function shipingzhongcustomfour_batchcreateuser_form_validate($form, &$form_state) { for($i=1;$i<=20;$i++) { $name = trim($form_state['values']['name_'.$i]); $mail = trim($form_state['values']['mail_'.$i]); $pass = trim($form_state['values']['pass_'.$i]); if(!empty($name)){ if(empty($mail) || empty($pass)){ form_set_error('', '用户名,邮箱,密码必须全部填好!'); } } if(!empty($mail)){ if(empty($name) || empty($pass)){ form_set_error('', '用户名,邮箱,密码必须全部填好!'); } } if(!empty($pass)){ if(empty($name) || empty($mail)){ form_set_error('', '用户名,邮箱,密码必须全部填好!'); } } if(!empty($name) && !empty($mail) && !empty($pass)) { $sql = "select uid from {users} where name='%s'"; $uid = db_result(db_query($sql,$name)); if(!empty($uid)){ form_set_error('name_', '用户名'.$name.'已存在!'); } if(!valid_email_address($mail)){ form_set_error('mail_', '邮件'.$mail.'格式不合法!'); } $sql = "select uid from {users} where mail='%s'"; $uid = db_result(db_query($sql,$mail)); if(!empty($uid)){ form_set_error('pass_', '邮件'.$mail.'已存在!'); } } } // $profile_xingming = trim($form_state['values']['profile_xingming']); // if (empty($profile_xingming)) { // // form_set_error('profile_xingming', '请输入姓名!'); // } // $profile_gerenjianjie = trim($form_state['values']['profile_gerenjianjie']); // if (empty($profile_gerenjianjie)) { // // form_set_error('profile_gerenjianjie', '请输入个人简介!'); // } } function shipingzhongcustomfour_batchcreateuser_form_submit($form, &$form_state) { for($i=1;$i<=20;$i++) { $fields = array(); $name = trim($form_state['values']['name_'.$i]); $mail = trim($form_state['values']['mail_'.$i]); $pass = trim($form_state['values']['pass_'.$i]);//密码不用md5 $fields['name'] = $name; $fields['mail'] = $mail; $fields['pass'] = $pass; if(empty($name) && empty($mail) && empty($pass)){ continue; } $currentTime = time(); $fields['created'] = $currentTime; $fields['access'] = $currentTime; $fields['login'] = 0; $fields['status'] = 1; $fields['timezone'] = '28800'; $fields['timezone_name'] = 'Asia/Chongqing'; $fields['language'] = 'zh-hans'; $fields['roleids'] = array(2 => 'authenticated user'); $fields['profile_xingming'] = $fields['name']; $fields['profile_gerenjianjie'] = $fields['name']; $account = user_save('', $fields); if ($account->uid) { drupal_set_message('创建用户成功!用户名是:' . $fields['name']); } else { drupal_set_message('创建用户失败,用户名是:' . $fields['name'], 'error'); } } } function shipingzhongcustomfour_newuserpermission_form() { global $user; $sql = "select uid,name from users order by created desc limit 0,40"; $result = db_query($sql); while($rowObj = db_fetch_object($result)) { $users[] = $rowObj; } $options = array('all-shipingzhong'=>'<font color="blue">全部</font>'); foreach($users as $key=>$value){ $options[$value->uid] = $value->name; } $form['userids'] = array( '#type' => 'checkboxes', '#title' => '最近40位用户用户', '#options' =>$options, '#description' =>'<div class="clear">打勾选中用户</div>', ); $options = array(); foreach($user->roles as $key=>$value) { if(strstr($value,'分配_') == $value){ $rolename = substr($value,stripos($value,'_')+1).'_角色'; $rid = db_result(db_query("select rid from role where name='%s'",$rolename)); $options[$rid] = substr($value,stripos($value,'_')+1).'_角色'; } } $form['roleids'] = array( '#type' => 'checkboxes', '#title' => '角色', '#description' =>'<div class="clear">角色打勾</div>', '#options' => $options ); $form['#attributes'] = array('class' => 'newuserpermission'); $form['submit'] = array('#type' => 'submit', '#value' => '更新权限'); $form['link'] = array('#type' => 'markup', '#value' => ' <a href="/user"><font color="blue">返回用户中心</font></a>'); return $form; } function shipingzhongcustomfour_newuserpermission_form_validate($form, &$form_state) { $selectUser = false; //表单中得到的全部是 option的数组键 foreach($form_state['values']['userids'] as $uid) { if(!empty($uid) && is_numeric($uid)){ $selectUser = true; break; } } if(empty($selectUser)){ form_set_error('userids', '没有选中任何用户!'); } $selectRole = false; foreach($form_state['values']['roleids'] as $rid) { if(!empty($rid)){ $selectRole = true; } } if(empty($selectRole)){ form_set_error('roleids', '没有选中任何角色!'); } } function shipingzhongcustomfour_newuserpermission_form_submit($form, &$form_state) { $userids = array(); //表单中得到的全部是 option的数组键 foreach($form_state['values']['userids'] as $uid) { if(!empty($uid) && is_numeric($uid)){ $userids[] = $uid; } } $sql = "select rid,name from role"; $result = db_query($sql); while($rowObj = db_fetch_object($result)) { //数据库中得到所有的角色 id 与 角色名组成的数组 $allRoles[$rowObj->rid] = $rowObj->name; } //这是表单中选中的个角色 $roles = array(); foreach($form_state['values']['roleids'] as $rid) { if(!empty($rid)){ $roles[$rid] = $allRoles[$rid]; } } foreach($userids as $uid) { $account = (object)$account; $account = user_load($uid); //用户本来的角色 与 新添加的角色 相加 $account->roles = $account->roles + $roles; //好像 保存时 必须 要用 第二个参数 的组数形式 如果 把 第二个参数省掉,好像保存不起作用 user_save($account,array('roles'=> $account->roles)); if ($account->uid) { drupal_set_message('添加用户角色成功!用户名是:' . $account->name.' 添加的角色是'.implode(',',$roles)); } else { drupal_set_message('添加用户角色失败', 'error'); } } }