php - Check specific user permission in a twig view

In a TWIG view of my Symfony 5 project, I have the following piece of code :
<table class="table table-hover table-striped datatable">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
<th>Created at</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.createdAt|date('d/m/Y H:i') }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Nothing impressive. I wanted to know if there was a way to check if theuser
in my loop has a specific role, let's sayROLE_STAFF
(More precisely, I want to check it against the hierarchy. If he has a role that "inherits"ROLE_STAFF
, it should also satisfy the condition). I have seen this post but it's quite old, and I'm hoping maybe something has been implemented since the moment it was written.
I tried injecting theAccessDecisionManagerInterface
in myUser
entity without any good result. My method (thereafter) does not work because$this->decisionManager
is null. I guess it has not been injected properly. I inject it by the constructor :
public function __construct(AccessDecisionManagerInterface $decisionManager)
{
$this->decisionManager = $decisionManager;
}
public function hasRolePermissions(string $role)
{
$decisionManager = new AccessDecisionManager();
$token = new UsernamePasswordToken($this, '', '', $this->getRoles());
return $this->decisionManager->decide($token, [$role]);
}
Although a solution that does not useisGranted
, like a service, or an injection, is perfectly acceptable to me, I would prefer to keep it simple. I was planning to build a twig function, but I would like to know if I'm reinventing the wheel or not. This seems a common enough issue, so I hope there is some built-in functionality I'm not aware of.
Answer
Solution:
You can use the class to find all "reachable" roles for a current user.
In the Symfony framework, this class is available as thesecurity.role_hierarchy
service (autowirable asSymfony\Component\Security\Core\Role\RoleHierarchyInterface
).
// check for ROLE_STAFF while taking the role hierarchy into account
$isStaff = in_array(
'ROLE_STAFF',
$roleHierarchy->getReachableRoles($user->getRoles())
);
You can write a custom Twig extension (e.g. a custom Twig test) to be able to do this in your templates.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.