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