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); |
|
|
|
|
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
|
|
|
|