批量创建用户?

时间:2009-11-11 18:53:15

标签: drupal

我有一个包含名称,电子邮件和角色的600名用户的excel列表 - 我需要添加到我正在构建的drupal站点。

用户之间分配了2个角色。

作为一个额外的复杂功能,该网站正在使用内容配置文件模块,因此如果为每个新用户帐户创建,也会自动创建相应的配置文件节点,这将是一个很好的帮助。

如何批量创建新用户?

2 个答案:

答案 0 :(得分:3)

user_import模块怎么样?

答案 1 :(得分:1)

我有同样的事情,并为此创建了一个模块。 基本上,它读取用户以及从文件中获取的角色;就我而言,它是一个CSV文件,包含电子邮件,名称,角色和内容配置文件所需的内容。

假设您想要用户x@mail.com并自动填写他的内容档案数据名称,Sirname和City等。

在你的模块中:

从文件中读取该行 创建一个新用户 创建一个新节点,(stdClass对象,给它正确的类型('profile_data'或任何你的内容配置文件类型)并填充你的其余节点并保存。

样本:

<?php

//create a form with a button to read the CSV file

function bulk_users_submit() {
    $users = 0;
    $handle = fopen(drupal_get_path('module', 'bulk_users') .'/'.DATAFILE, "r");
    if (!$handle) {
        return $users;
    }
    while (($data = fgetcsv($handle)) !== FALSE) {
        //this is similar to what the users.module does
        if (bulk_users_create_user($data)) {
            $users++;
            bulk_users_create_profile($data);
        }
    }
    fclose($handle);
    return $users;
}


function bulk_users_create_profile($user, $data) {
    $node = new stdClass();
    $node->title = t('First and Last Name');
    $node->body = "";
    $node->type = 'first_and_last_name';
    $node->created = time();
    $node->changed = $node->created;
    $node->status = 1;
    $node->promote = 0;
    $node->sticky = 0;
    $node->format = 0;
    $node->uid = $data['uid'];
    $node->language = 'en';
    $node->field_firstname[0]['value'] = $data['firstname'];
    $node->field_lastname[0]['value'] = $data['lastname'];
    node_save($node);
}


?>

没有经过测试,但我希望这个想法很明确。

相关问题