Passed
Pull Request — master (#5329)
by Angel Fernando Quiroz
07:03
created

TicketProjectHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 66
rs 10
c 1
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedRolesFromProject() 0 33 4
A __construct() 0 4 1
A userIsAllowInProject() 0 24 5
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\Component\Security\Core\Security;
11
12
class TicketProjectHelper
13
{
14
    public function __construct(
15
        private readonly Security $security,
16
        private readonly SettingsManager $settingsManager,
17
    ) { }
18
19
    public function userIsAllowInProject(int $projectId): bool
20
    {
21
        if ($this->security->isGranted('ROLE_ADMIN')) {
22
            return true;
23
        }
24
25
        $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

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