简体   繁体   English

登录到站点时,非管理员用户的用户 ID 返回为 0 - Drupal

[英]User ID returned as 0 for non-admin users when logged in to the site- Drupal

I have a dashboard controller where I show the dashboard to users who are not anonymous.我有一个仪表板 controller,我在其中向非匿名用户展示仪表板。 My controller code is as below:我的 controller 代码如下:

class DashboardController extends ControllerBase {  
    protected $currentUser;

    /**
     * {@inheritdoc}
     */
    public function __construct(AccountProxy $current_user) {
        $this->currentUser = $current_user;
    }

    //HERE IS WHERE THE PROBLEM IS. FOR NON-ADMIN USERS, THE USERID IS RETURNED AS 0
    public function access(AccountInterface $account) {
      dd($this->currentUser);
      if (!$this->currentUser->isAuthenticated()) {
            return AccessResult::forbidden();
        } else {
            return AccessResult::allowed();
        }

    //FUNCTION TO DISPLAY DASHBOARD
    public function accessDashboard(AccountInterface $account) {
    $current_user = $this->currentUser;
      $roles = $current_user->getRoles();
      $current_user_record = \Drupal\user\Entity\User::load($current_user->id());
      if (!$current_user->isAuthenticated()) {
        return AccessResult::forbidden();
      }
      if ($current_user->hasPermission('view school dashboard')) {
        //SHOW THE ASSOCIATED SCHOOL NAMES DASHBOARD
      } 
      return AccessResult::forbidden();
    }
}

Screenshot of what I see when logged in as a non-admin user:我以非管理员用户身份登录时看到的屏幕截图:

在此处输入图像描述

When logging in as an Administrator, I can clearly see the user ID and details as below:以管理员身份登录时,我可以清楚地看到用户 ID 和详细信息,如下所示:

在此处输入图像描述

any help on how to handle this problem?有关如何处理此问题的任何帮助?

In Drupal, if a visitor/user not logged in (Anonymous User) then they will share the UID of 0.在 Drupal 中,如果访客/用户未登录(匿名用户),那么他们将共享 0 的 UID。

You can find this from the below doc: Drupal - User Documentation您可以从以下文档中找到它: Drupal - 用户文档

Also, in your first screenshot the object you dump is of AnonymousUserSession which is the implementation representing anonymous users.此外,在您的第一个屏幕截图中,您转储的 object 属于AnonymousUserSession ,它是代表匿名用户的实现。 So I think there are no authenticated users yet.所以我认为还没有经过身份验证的用户。

Drupal - Class AnonymousUserSession Drupal - Class 匿名用户会话

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

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