Passed
Pull Request — master (#6396)
by Angel Fernando Quiroz
13:45 queued 05:26
created

PermissionHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasPermission() 0 15 1
A __construct() 0 4 1
A getUserRoles() 0 6 2
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Helpers;
8
9
use Chamilo\CoreBundle\Repository\PermissionRelRoleRepository;
10
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11
12
class PermissionHelper
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_ANONYMOUS' !== $role;
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
40
        $results = $queryBuilder->getQuery()->getResult();
41
42
        return \count($results) > 0;
43
    }
44
}
45