Passed
Pull Request — master (#5329)
by Angel Fernando Quiroz
17:30 queued 09:52
created

TicketProjectHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
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\Settings\SettingsManager;
10
use Symfony\Bundle\SecurityBundle\Security;
11
12
use const JSON_ERROR_NONE;
13
14
class TicketProjectHelper
15
{
16
    public function __construct(
17
        private readonly Security $security,
18
        private readonly SettingsManager $settingsManager,
19
    ) {}
20
21
    public function userIsAllowInProject(int $projectId): bool
22
    {
23
        if ($this->security->isGranted('ROLE_ADMIN')) {
24
            return true;
25
        }
26
27
        $allowRoleList = self::getAllowedRolesFromProject($projectId);
0 ignored issues
show
Bug Best Practice introduced by
The method Chamilo\CoreBundle\Servi...lowedRolesFromProject() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        /** @scrutinizer ignore-call */ 
28
        $allowRoleList = self::getAllowedRolesFromProject($projectId);
Loading history...
28
29
        // Check if a role was set to the project.
30
        // Project 1 is considered the default and is accessible to all users
31
        if (!empty($allowRoleList)) {
32
            $result = false;
33
34
            foreach ($allowRoleList as $role) {
35
                if ($this->security->isGranted($role)) {
36
                    $result = true;
37
38
                    break;
39
                }
40
            }
41
42
            return $result;
43
        }
44
45
        return false;
46
    }
47
48
    public function getAllowedRolesFromProject(int $projectId): array
49
    {
50
        // Define a mapping from role IDs to role names
51
        $roleMap = [
52
            1 => 'ROLE_ADMIN',
53
            17 => 'ROLE_STUDENT_BOSS',
54
            4 => 'ROLE_RRHH',
55
            3 => 'ROLE_SESSION_MANAGER',
56
            // ... other mappings can be added as needed
57
        ];
58
59
        $jsonString = $this->settingsManager->getSetting('ticket.ticket_project_user_roles');
60
61
        if (empty($jsonString)) {
62
            return [];
63
        }
64
65
        $data = json_decode($jsonString, true);
66
67
        if (JSON_ERROR_NONE !== json_last_error()) {
68
            // Invalid JSON
69
            return [];
70
        }
71
72
        if (!isset($data['permissions'][$projectId])) {
73
            // No permissions for the given projectId
74
            return [];
75
        }
76
77
        $roleIds = $data['permissions'][$projectId];
78
79
        // Transform role IDs into role names using the defined mapping
80
        return array_map(fn ($roleId) => $roleMap[$roleId] ?? "$roleId", $roleIds);
81
    }
82
}
83