Drupal 自动更改模块中的用户角色

发布于 2024-08-30 17:51:35 字数 299 浏览 4 评论 0原文

我正在尝试编写一个模块,一旦将节点表单插入数据库,该模块将更改用户的权限。当用户注册时,我已经分配了一个角色,但我希望在他们创建“公司”配置文件后更改该角色,在这种情况下,即当他们填写“company_post”类型的 cck 表单时。我的代码如下...

type == 'company_post') { } ?>

我不确定在 if 语句中放置什么,因为我真的不知道如何引用用户角色或如何更改它们。 ? 所以我的问题是我可以使用什么代码将用户当前角色更改为新角色? (这两个角色已经在drupal中创建并且具有单独的权限)

I am trying to write a module that will change a user's permissions once a node form is inserted into the database. I already have a role assigned when a user registers, but i want that role to be changed once they create a 'company' profile in this case which is when they fill out a cck form of 'company_post' type. My code is below...

type == 'company_post') {

}

?>

im not sure what to put in the if statement because I don't really know how to reference the users roles or how to change them.
?
So my question is what code can I use to change the users current role to a new role? (Both roles are already created in drupal and have seperate permissions)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清音悠歌 2024-09-06 17:51:35

要删除实际上有 3 个查询:

db_query('DELETE FROM {role} WHERE rid = %d', $form_state['values']['rid']);

db_query('DELETE FROM {permission} WHERE rid = %d', $form_state['values']['rid']);

// Update the users who have this role set:

db_query('DELETE FROM {users_roles} WHERE rid = %d', $form_state['values']['rid']);

To delete actualy there are 3 queries:

db_query('DELETE FROM {role} WHERE rid = %d', $form_state['values']['rid']);

db_query('DELETE FROM {permission} WHERE rid = %d', $form_state['values']['rid']);

// Update the users who have this role set:

db_query('DELETE FROM {users_roles} WHERE rid = %d', $form_state['values']['rid']);
寄与心 2024-09-06 17:51:35

您无法编写通用代码,因为 Drupal 使用的是角色的角色 ID(rid),它是一个序列。

用户角色位于包含 uid 和 rod 的 user_roles 表中,因此要删除角色,您可以执行以下操作:

global $user;
$rid = x; // x = the id of the role to remove
db_query("DELETE FROM {user_roles} WHERE uid = %d AND rid = %d", $user->uid, $rid);

要赋予新角色,您可以执行以下操作:

$record = array(
  'uid' => $user->uid,
  'rid' => y, // y = the id of the role to give.
);
drupal_write_record('user_roles', $record);

You can't make generic code, as the what Drupal use is the role id (rid) of a role, which is a serial.

A users roles, is in the user_roles table containing uid and rid, so to remove a role you would do:

global $user;
$rid = x; // x = the id of the role to remove
db_query("DELETE FROM {user_roles} WHERE uid = %d AND rid = %d", $user->uid, $rid);

To give the new role you could do:

$record = array(
  'uid' => $user->uid,
  'rid' => y, // y = the id of the role to give.
);
drupal_write_record('user_roles', $record);
浅唱々樱花落 2024-09-06 17:51:35

有一个更好的方法来完成这个......
为了响应您的编辑,如果您使用 global $user 对象,那么下次更新节点的人也将应用角色(如果您决定添加 case 'update' : 到函数或其他)。
相反,由于节点将分配有 $node->uid,因此请使用它。

<?php
function role_change_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { 
 switch ($op) {
  case 'insert':
  if ($node->type == 'company_post') {
   // Assuming the role id you want to assign is "3"...
   $role_id = 3;
   $role_name = db_result(db_query('SELECT name FROM {role} WHERE rid = %d',$role_id));
   $author = user_load(array('uid' => $node->uid));
   if ($author !== FALSE && !isset($author->roles[$role_id])) {
    $new_roles_combined_with_old_ones = $author->roles + array($role_id => $role_name);
    user_save($author, array('roles' => $new_roles_combined_with_old_ones));
   }
  }
  break;
 }
}
?>

或者,如果您想在可能没有 的情况下完成相同的操作>$node->uid, global $user 也可以正常工作。 但是请记住,当您使用global $user时,更改将应用​​于当前登录并完成任何操作的人员的帐户。

<?php
global $user;
// Assuming the role id you want to assign is "5"...
$role_id = 5;
$role_name = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $role_id));
// Make sure user is valid, has a uid, and does't already have role 5 assigned to them.
if ($user !== FALSE && $user->uid && !isset($user->roles[$role_id])) {
  $new_roles_combined_with_old_ones = $user->roles + array($role_id => $role_name);
  user_save($user, array('roles' => $new_roles_combined_with_old_ones));
}
?>

瞧。我整晚都在这里。 ;)

There's a much better way to accomplish this...
In response to your edit, if you use the global $user object, then whoever updates the node next time will have the roles applied to them also (if you decided to add case 'update': to the function or whatever).
Instead, since the node will have $node->uid assigned to it, use that.

<?php
function role_change_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { 
 switch ($op) {
  case 'insert':
  if ($node->type == 'company_post') {
   // Assuming the role id you want to assign is "3"...
   $role_id = 3;
   $role_name = db_result(db_query('SELECT name FROM {role} WHERE rid = %d',$role_id));
   $author = user_load(array('uid' => $node->uid));
   if ($author !== FALSE && !isset($author->roles[$role_id])) {
    $new_roles_combined_with_old_ones = $author->roles + array($role_id => $role_name);
    user_save($author, array('roles' => $new_roles_combined_with_old_ones));
   }
  }
  break;
 }
}
?>

Or, if you want to accomplish the same thing in a situation that may not have a $node->uid, global $user will work fine too. But remember that when you use global $user, the changes will be applied to the account of the person who is currently logged in and completing whatever the action may be.

<?php
global $user;
// Assuming the role id you want to assign is "5"...
$role_id = 5;
$role_name = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $role_id));
// Make sure user is valid, has a uid, and does't already have role 5 assigned to them.
if ($user !== FALSE && $user->uid && !isset($user->roles[$role_id])) {
  $new_roles_combined_with_old_ones = $user->roles + array($role_id => $role_name);
  user_save($user, array('roles' => $new_roles_combined_with_old_ones));
}
?>

Voila. I'm here all night. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文