I have a dashboard controller where I show the dashboard to users who are not anonymous. My controller code is as below:
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:
any help on how to handle this problem?

