|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\ServiceHelper; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Repository\PermissionRelRoleRepository; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
11
|
|
|
|
|
12
|
|
|
class PermissionServiceHelper |
|
13
|
|
|
{ |
|
14
|
|
|
public function __construct( |
|
15
|
|
|
private ParameterBagInterface $parameterBag, |
|
16
|
|
|
private PermissionRelRoleRepository $permissionRelRoleRepository |
|
17
|
|
|
) {} |
|
18
|
|
|
|
|
19
|
|
|
public function getUserRoles(): array |
|
20
|
|
|
{ |
|
21
|
|
|
$roles = $this->parameterBag->get('security.role_hierarchy.roles'); |
|
22
|
|
|
|
|
23
|
|
|
return array_filter(array_keys($roles), function ($role) { |
|
24
|
|
|
return !str_starts_with($role, 'ROLE_CURRENT_') && $role !== 'ROLE_ANONYMOUS'; |
|
25
|
|
|
}); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function hasPermission(string $permissionSlug, array $roles): bool |
|
29
|
|
|
{ |
|
30
|
|
|
$queryBuilder = $this->permissionRelRoleRepository->createQueryBuilder('prr') |
|
31
|
|
|
->innerJoin('prr.permission', 'p') |
|
32
|
|
|
->where('p.slug = :permissionSlug') |
|
33
|
|
|
->andWhere('prr.roleCode IN (:roles)') |
|
34
|
|
|
->andWhere('prr.changeable = :changeable') |
|
35
|
|
|
->setParameter('permissionSlug', $permissionSlug) |
|
36
|
|
|
->setParameter('roles', $roles) |
|
37
|
|
|
->setParameter('changeable', true); |
|
38
|
|
|
|
|
39
|
|
|
$results = $queryBuilder->getQuery()->getResult(); |
|
40
|
|
|
|
|
41
|
|
|
return count($results) > 0; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|