以编程方式创建用户并赋予他们角色


46

我正在以编程方式创建用户,如下所示:

$newUser = array(
  'name' => $mail, 
  'pass' => 'password',  // note: do not md5 the password
  'mail' => $mail, 
  'status' => 1, 
  'init' => $mail,
  'roles' => array(5)
);
$user = user_save(null, $newUser);

我有一个角色ID等于5的角色;创建用户时,表“ users_roles”中仅存在角色ID值为0的行,但是如果我使用来打印用户对象var_dump(),则看起来已创建了角色。

我究竟做错了什么?


它只检查值的真实性,关键是要紧。array($role_id => 'anything')
RaisinBranCrunch

Answers:


46

这段代码对我有用:

$new_user = array(
  'name' => $name,
  'pass' => $sifra, // note: do not md5 the password
  'mail' => $email,
  'status' => 1,
  'init' => $email,
  'roles' => array(
    DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    3 => 'custom role',
  ),
);

// The first parameter is sent blank so a new user is created.
user_save('', $new_user);

我是否应该将密码写为“ abc12345”之类的密码,并且user_save()会转换为md5?
xstean

1
user_save会照顾好它的
-Gulok

只要密码位于user_save()的第二个参数中,它将被散列。如果不user_save($account)$edit参数,则不会发生md5转换。下面@SMTF的回答说明了这一点。
alec

感谢您救了我的一天
Rishab

13

这是我在网站上找到的示例。

$account = new stdClass;
$account->is_new = TRUE;
$account->name = 'foo';
$account->pass = user_hash_password('bar');
$account->mail = 'foo@example.com';
$account->init = 'foo@example.com';
$account->status = TRUE;
$account->roles = array(DRUPAL_AUTHENTICATED_RID => TRUE);
$account->timezone = variable_get('date_default_timezone', '');
user_save($account);

抱歉,我认为DRUPAL_AUTHENTICATED_RID => TRUE似乎是错误的,并且我认为不需要user_hash_password。
stefgosselin

user_hash_password('bar');是错的
乔尔·詹姆斯

在某些情况下,user_save()在保存密码之前无法对密码进行哈希处理(根据我的经验,我发现网络上的其他人也偶尔会遇到这种情况)。我不知道为什么(但是我想!)。在这些情况下,user_hash_password()似乎有必要打电话。
alec

此答案的代码确实需要user_hash_password()手动调用。这是因为它没有传递$edituser_save()。如果代码包含$edit = array('pass' => 'bar');(此答案不包括),那么$account->pass = user_hash...您可以做user_save($account, $edit);并且user_save()可以为您执行哈希处理,而不是这样做。
alec

13

要以编程方式创建具有角色和自定义字段值(例如,名字和姓氏)的用户,可以使用以下代码:

$new_user = array(
  'name' => 'xgramp',
  'pass' => 'idontwantnoonebutyoutoloveme',
  'mail' => 'xgparsons@flyingburritobrothers.la',
  'signature_format' => 'full_html',
  'status' => 1,
  'language' => 'en',
  'timezone' => 'America/Los_Angeles',
  'init' => 'Email',
  'roles' => array(
    DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    6 => 'member', // role id for custom roles varies per website
  ),
  'field_first_name' => array(
    'und' => array(
      0 => array(
        'value' => 'Gram',
      ),
    ),
  ),
  'field_last_name' => array(
    'und' => array(
      0 => array(
        'value' => 'Parsons',
      ),
    ),
  ),
);

$account = user_save(NULL, $new_user);

有关更多详细信息,请参见此博客文章和评论:http : //codekarate.com/blog/create-user-account-drupal-7-programmatically


您错过了角色列表上的括号
SeanJA 2014年

2

要以编程方式从电子邮件地址创建用户,请尝试以下操作:

// Use the e-mail address prefix as a user name.
$name = substr($mail, 0, strpos($mail, '@'));

// Make sure the user name isn't already taken.
$query = db_select('users', 'u')
  ->fields('u', array('uid'))
  ->condition('u.name', $name)
  ->execute();
$result = $query->fetch();

// If the user name is taken, append a random string to the end of it.
if ($result->uid) { $name .= '-' . user_password(); }

// Build the user account object and then save it.
$account = new stdClass();
$account->name = $name;
$account->mail = $mail;
$account->init = $mail;
$account->pass = user_password();
$account->status = 1;
user_save($account);
if ($account->uid) {
  drupal_set_message('Created new user with id %uid', array('%uid' => $account->uid));
}

0

以编程方式创建用户:

$newUser = array(
      'name' => 'theUserName,
      'pass' => 'thePassWord',
      'mail' => 'the@mail.com',
      'status' => 1,
      'roles' => array(DRUPAL_AUTHENTICATED_RID => 'authenticated user'),
      'init' =>  'the@mail.com',
  );
user_save(null, $newUser);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.