Drupal 自动更改模块中的用户角色
我正在尝试编写一个模块,一旦将节点表单插入数据库,该模块将更改用户的权限。当用户注册时,我已经分配了一个角色,但我希望在他们创建“公司”配置文件后更改该角色,在这种情况下,即当他们填写“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要删除实际上有 3 个查询:
To delete actualy there are 3 queries:
您无法编写通用代码,因为 Drupal 使用的是角色的角色 ID(rid),它是一个序列。
用户角色位于包含 uid 和 rod 的
user_roles
表中,因此要删除角色,您可以执行以下操作:要赋予新角色,您可以执行以下操作:
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:To give the new role you could do:
有一个更好的方法来完成这个......
为了响应您的编辑,如果您使用
global $user
对象,那么下次更新节点的人也将应用角色(如果您决定添加case 'update' :
到函数或其他)。相反,由于节点将分配有
$node->uid
,因此请使用它。或者,如果您想在可能没有
的情况下完成相同的操作>$node->uid
,global $user
也可以正常工作。 但是请记住,当您使用global $user
时,更改将应用于当前登录并完成任何操作的人员的帐户。瞧。我整晚都在这里。 ;)
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 addcase 'update':
to the function or whatever).Instead, since the node will have
$node->uid
assigned to it, use that.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 useglobal $user
, the changes will be applied to the account of the person who is currently logged in and completing whatever the action may be.Voila. I'm here all night. ;)