【问题标题】:How to assign a role programatically in Drupal 6? 'role' => 'rolename didn´t worked如何在 Drupal 6 中以编程方式分配角色? '角色' => '角色名无效
【发布时间】:2012-11-21 03:55:42
【问题描述】:

在我的 Drupal 6 站点中,我具有使用节点表单内的信息创建新用户的功能:

function altas_create_user($name,$pass,$mail) {
  $newuser = array(
    'name' => $name,
    'mail' => $mail,
    'pass' => $pass,
    'access' => 1,
    'notify' => 1,
    'status' => 1,
  );
  return user_save(NULL, $newuser); 
}

现在,我注意到我没有为该用户分配角色。我该怎么做?

我试过添加这个,但没有用:'role' => 'rolename',

我该怎么做?感谢您的帮助!

编辑:我忘了说我已经查看了 API,found 可能我应该使用 'roles' => $roles,但不明白如何用我的角色名实际填写 $roles。

【问题讨论】:

    标签: php drupal drupal-roles


    【解决方案1】:

    解决了! (找到答案here)并认为我会发布结果,以防万一它对某人有所帮助: 我应该

    // Get an array of roles
    $roles = user_roles();
    

    首先(在函数内部,在 $newuser 数组之前),然后,

    'roles' => array(array_search('myrolename', $roles) => 1),
    

    在那里,它就像一个魅力。

    【讨论】:

      猜你喜欢
      • 2016-03-19
      • 2012-06-29
      • 1970-01-01
      • 2011-04-01
      • 2023-02-14
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多