如果要批量创建用户,只需要写个循环即可
欢迎各位兄弟 发布技术文章
这里的技术是共享的
如果要批量创建用户,只需要写个循环即可
$newuser['roles']=array(2=>0,3=>0);//可以将0改为任意值,不影响
如果创建了Roles的话,这个修改了也不影响吗?
测试一下的话,你就不用问这个问题了。我没试过,不过估计是这样子的
来自 http://www.trackself.com/archives/1352.html
user_save(美元帐户,$阵列=阵列(),$类别='账户')
模块/用户/ user.module的,第106行开始
更新一个使用者帐号内容,或是建立一个新的帐号
一个用户$帐户物件,如果用户的uid(帐号ID)为空,则建立新的帐号,如果有东西的话,就更新这个帖子的帐号资料
$阵列一个带有帐号资讯的阵列。例如:数组('名称'=>'我的名字');值为NULL的话,就表示把这个栏位清空。
(注:在更新中,没有列在这个阵列里的栏位会保持不变)
类别(选择性参数)钩子函数。
第一个参数要求的是一个用户的物件,但是其实只用到里面的UID属性。
所以要新增一个帐号的时候,并不用费心去建立一个“空的用户物件”,给它一个''(空字串)就好了。
如:
角色=阵列(
'3'=>'角色名称',
'4'=>'角色名称',
'5'=>'角色名称'
);
user_save('',阵列(“名称”=>“纳格髓“,”通行证“=>'未知',”状态“=> 1,'角色'=> $的角色,”profile_tel => '0800 123456,));
注:
在Drupal的的群组(角色)中,1表示访客,2 1或2也不会被记入资料库中。
(原来文章的例子是直接打阵列(3,4,5)来加入编号3,4,5的角色,这种写法是错误的)
<?php
function user_save($account, $array = array(), $category = 'account') {
// Dynamically compose a SQL query:
$user_fields = user_fields();
if ($account->uid) {
user_module_invoke('update', $array, $account, $category);
$data = unserialize(db_result(db_query('SELECT data FROM {users} WHERE uid = %d',$account->uid)));
foreach ($array as $key => $value) {
if ($key == 'pass' && !empty($value)) {
$query .= "$key = '%s', ";
$v[] = md5($value);
}
else if ((substr($key, 0, 4) !== 'auth') && ($key != 'pass')) {
if (in_array($key, $user_fields)) {
// Save standard fields
$query .= "$key = '%s', ";
$v[] = $value;
}
else if ($key != 'roles') {
// Roles is a special case: it used below.
if ($value === NULL) {
unset($data[$key]);
}
else {
$data[$key] = $value;
}
}
}
}
$query .= "data = '%s' ";
$v[] = serialize($data);
db_query("UPDATE {users} SET $query WHERE uid = %d", array_merge($v, array($account->uid)));
// Reload user roles if provided
if (is_array($array['roles'])) {
db_query('DELETE FROM {users_roles} WHERE uid = %d', $account->uid);
foreach (array_keys($array['roles']) as $rid) {
if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $rid);
}
}
}
// Delete a blocked user's sessions to kick them if they are online.
if (isset($array['status']) && $array['status'] == 0) {
sess_destroy_uid($account->uid);
}
// If the password changed, delete all open sessions and recreate
// the current one.
if (isset($array['pass'])) {
sess_destroy_uid($account->uid);
sess_regenerate();
}
// Refresh user object
$user = user_load(array('uid' => $account->uid));
user_module_invoke('after_update', $array, $user, $category);
}
else {
$array['uid'] = db_next_id('{users}_uid');
if (!isset($array['created'])) { // Allow 'created' to be set by hook_auth
$array['created'] = time();
}
// Note, we wait with saving the data column to prevent module-handled
// fields from being saved there. We cannot invoke hook_user('insert') here
// because we don't have a fully initialized user object yet.
foreach ($array as $key => $value) {
switch ($key) {
case 'pass':
$fields[] = $key;
$values[] = md5($value);
$s[] = "'%s'";
break;
case 'uid': case 'mode': case 'sort':
case 'threshold': case 'created': case 'access':
case 'login': case 'status':
$fields[] = $key;
$values[] = $value;
$s[] = "%d";
break;
default:
if (substr($key, 0, 4) !== 'auth' && in_array($key, $user_fields)) {
$fields[] = $key;
$values[] = $value;
$s[] = "'%s'";
}
break;
}
}
db_query('INSERT INTO {users} ('. implode(', ', $fields) .') VALUES ('. implode(', ', $s) .')', $values);
// Build the initial user object.
$user = user_load(array('uid' => $array['uid']));
user_module_invoke('insert', $array, $user, $category);
// Build and save the serialized data field now
$data = array();
foreach ($array as $key => $value) {
if ((substr($key, 0, 4) !== 'auth') && ($key != 'roles') && (!in_array($key,$user_fields)) && ($value !== NULL)) {
$data[$key] = $value;
}
}
db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $user->uid);
// Save user roles (delete just to be safe).
if (is_array($array['roles'])) {
db_query('DELETE FROM {users_roles} WHERE uid = %d', $array['uid']);
foreach (array_keys($array['roles']) as $rid) {
if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)',$array['uid'], $rid);
}
}
}
// Build the finished user object.
$user = user_load(array('uid' => $array['uid']));
}
// Save distributed authentication mappings
$authmaps = array();
foreach ($array as $key => $value) {
if (substr($key, 0, 4) == 'auth') {
$authmaps[$key] = $value;
}
}
if (sizeof($authmaps) > 0) {
user_set_authmaps($user, $authmaps);
}
return $user;
}
?>
$new_user = array(
'name' => $form_state['values']['name'],
'pass' => user_password(),
'mail' => $form_state['values']['mail'],
'init' => $form_state['values']['mail'],
'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_first_name']))),
'field_last_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_last_name']))),
'field_phone_number' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_phone_number']))),
'status' => 1,
'access' => REQUEST_TIME,
'roles' => $roles,
);
// $account returns user object(返回刚才插入的用户对象)
$account = user_save(null, $new_user);
这样表示更新一个用户信息
$account = user_save($account, $new_user);
来自 http://blog.csdn.net/wjc19911118/article/details/7758430
2 comments