简体   繁体   English

如何在drupal 7.x中获得用户角色?

[英]How to get user roles in drupal 7.x?

I want to get the user roles Just like getting node types using this 我想获得用户角色就像使用它获取节点类型一样

node_type_get_types();

is there anyway to get user roles using a function like this? 无论如何使用这样的函数来获取用户角色?

请参阅user_roles()函数,该函数提供所有角色的列表,或者您可以选择过滤特定条件(我链接到的文档页面上有更多详细信息)。

If you want to check for a specific role of the current user (in this example we use "authenticated user"): 如果要检查当前用户的特定角色(在此示例中,我们使用“authenticated user”):

global $user;

if (in_array('authenticated user', $user->roles)){
    //do stuff
}

You can use user_load() to retreive the roles for a user. 您可以使用user_load()来检索用户的角色。

example: 例:

$user = user_load(1);
$roles = $user->roles;
// $roles contains all the users assigned roles

Alternative approach to checking roles would be to see if the user has a certain role example. 检查角色的替代方法是查看用户是否具有某个角色示例。

if(user_access('access content')){
   //Good to go....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM