Checking the user credentials
You can easily check whether a given user should access a certain resource as long as you have that user account at hand. Here, you can encounter two scenarios:
- You want to interrogate the current user
- You want to interrogate a given user, not necessarily the current one
As we saw in Chapter 2, Creating Your First Module, the current user is represented by a service, which implements the AccountProxyInterface
interface. This service can be accessed by the current_user
key or statically with this shorthand:
/** @var AccountProxyInterface $accountProxy */ $accountProxy = \Drupal::currentUser();
From this account proxy, we can request the AccountInterface
, which represents the actual logged-in user account (the UserSession
object). It holds a reference to the User entity, with a few of its "account" related data, but that is pretty much it. If we need to access its entity fields, we need to load the entity as we normally do:
/** @var UserInterface $user */ $user = \Drupal...