`

Drupal checking if user has a role

阅读更多
<?php
  global $user;

  // Check to see if $user has the administrator role.
  if (in_array('administrator', array_values($user->roles))) {
    // Do something.
  }
?>


<?php
/**
* Check to see if a user has been assigned a certain role.
*
* @param $role
*   The name of the role you're trying to find.
* @param $user
*   The user object for the user you're checking; defaults to the current user.
* @return
*   TRUE if the user object has the role, FALSE if it does not.
*/
function user_has_role($role, $user = NULL) {
  if ($user == NULL) {
    global $user;
  }

  if (is_array($user->roles) && in_array($role, array_values($user->roles))) {
    return TRUE;
  }

  return FALSE;
}
?>
分享到:
评论

相关推荐

    Drupal.8.for.Absolute.Beginners.1430264667

    Drupal 8 for Absolute Beginners is your definitive guide to starting from scratch with Drupal even if you have little web knowledge. This book teaches you the basics of HTML, CSS, JavaScript, and ...

    Drupal.8.Development.Beginners.Guide.2nd.Edition.epub

    If you are a developer who wants to use Drupal to enhance your website project and web application to manage content, this book is for you. Whether you are new to Drupal or an experienced web ...

    Drupal UserDashboard模块

    Drupal UserDashboard 模块; User Dashboard模块可以提供“个人信息面板定制”功能,类似于“用户中心”这样的功能

    drupal-9.0.1_drupal9_drupal9教程_drupal9开发实例_

    Drupal 9.0.1是Drupal内容管理系统的一个重要版本,带来了许多新特性和改进,旨在提升网站构建者的体验和网站的性能。Drupal是一款开源的PHP框架,被广泛用于创建复杂、可扩展的Web应用程序和网站。这个9.0.1版本的...

    Drupal7宝典+Drupal开发指南+Using Drupal

    Drupal是开源的内容管理系统(CMS),在全球范围内被广泛用于构建各种网站,从个人博客到复杂的商业平台。本资源包包含了三本书籍,可以帮助你深入理解和掌握Drupal7的各个方面: 1. **Drupal7宝典**: 这本书全面...

    Drupal data Drupal data

    Drupal 是一个强大的开源内容管理框架,它用于构建各种类型的网站,从小型个人博客到大型企业级应用程序。在“Drupal data Drupal data”的标题和描述中,虽然没有提供具体的信息,但我们可以推断这可能涉及到与...

    drupal高级开发手册

    - **访问控制**:深入讲解Drupal的权限管理机制,包括角色(Role)、权限(Permission)的概念及其配置方法。 - **搜索与索引**:介绍如何使用Drupal内置的搜索功能及第三方搜索引擎集成方案,如Solr、Elasticsearch等。...

    Decoupled Drupal in Practice

    Several projects point to a decoupled future for Drupal, including the Contenta CMS and work to modernize Drupal’s JavaScript using React. Begin learning about these and other exciting developments ...

    Drupal 8 Explained: Your Step-by-Step Guide to Drupal 8

    You will become a confident Drupal 8 user. Please note: This is book is 100% aimed at Drupal beginners. Drupal 8 Explained contains very little code and that is absolutely deliberate. If you want to ...

    drupal7源码

    `user` 表存储用户信息,`role` 和 `role_permissions` 表定义了角色及其关联的权限。 页面呈现由 Drupal 的钩子(hook)系统控制。钩子是 Drupal 中的一种回调机制,允许模块在特定事件(如页面渲染、内容保存等)...

    Drupal 8 Module Development 2nd Edition

    Write a Drupal 8 module with custom functionality and hook into various extension points Master numerous Drupal 8 sub-systems and APIs Model, store, and manipulate data in various ways and for various...

    drupal7与drupal6版本修改内容

    ### Drupal 7 与 Drupal 6 版本修改内容 #### 概述 Drupal 是一个开源的内容管理系统(CMS),被广泛用于构建各种类型的网站。从 Drupal 6 升级到 Drupal 7,Drupal 社区引入了大量的改进和新功能,以提高用户体验...

    Drupal专业开发指南(Drupal5)

    ### Drupal专业开发指南(Drupal5):关键知识点解析 #### 一、Drupal概述 - **定义**:Drupal是一款开放源代码的内容管理系统(Content Management System, CMS),被广泛应用于搭建多种类型的网站,包括博客、论坛、...

    drupal 6.12

    This will create a new directory drupal-x.x/ containing all Drupal files and directories. Move the contents of that directory into a directory within your web server's document root or your public ...

    drupal7安装说明

    Drupal 7 是一个流行的开源内容管理系统,用于构建各种类型的网站。本教程将详细介绍如何在本地环境中安装 Drupal 7,特别是针对使用 Apache2、PHP5 和 MySQL5 的集成环境,如 AppServ。 首先,确保你已经安装了...

Global site tag (gtag.js) - Google Analytics