|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Controller; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\PermissionRelRole; |
|
10
|
|
|
use Chamilo\CoreBundle\Form\PermissionType; |
|
11
|
|
|
use Chamilo\CoreBundle\Repository\PermissionRelRoleRepository; |
|
12
|
|
|
use Chamilo\CoreBundle\Repository\PermissionRepository; |
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
18
|
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The Permission controller manages the /permissions page to control what roles has what permission |
|
22
|
|
|
*/ |
|
23
|
|
|
class PermissionController extends AbstractController |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
#[IsGranted('ROLE_ADMIN')] |
|
27
|
|
|
#[Route('/permissions/test', name: 'permissions_test')] |
|
28
|
|
|
public function testPermissions(): Response |
|
29
|
|
|
{ |
|
30
|
|
|
// Test roles and permission slug |
|
31
|
|
|
$roles = ['ROLE_STUDENT', 'ROLE_TEACHER']; |
|
32
|
|
|
$permissionSlug = 'analytics:view'; |
|
33
|
|
|
|
|
34
|
|
|
// Call the api_get_permission function and log the result |
|
35
|
|
|
$hasPermission = api_get_permission($permissionSlug, $roles); |
|
36
|
|
|
error_log('Permission check for ' . $permissionSlug . ' with roles ' . implode(', ', $roles) . ': ' . ($hasPermission ? 'true' : 'false')); |
|
37
|
|
|
|
|
38
|
|
|
// Return a simple response for testing purposes |
|
39
|
|
|
return new Response('<html><body>Permission check result: ' . ($hasPermission ? 'true' : 'false') . '</body></html>'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
#[IsGranted('ROLE_ADMIN')] |
|
43
|
|
|
#[Route('/permissions', name: 'permissions')] |
|
44
|
|
|
public function index( |
|
45
|
|
|
PermissionRepository $permissionRepo, |
|
46
|
|
|
PermissionRelRoleRepository $permissionRelRoleRepo, |
|
47
|
|
|
Request $request, |
|
48
|
|
|
EntityManagerInterface $em |
|
49
|
|
|
): Response { |
|
50
|
|
|
$permissions = $permissionRepo->findAll(); |
|
51
|
|
|
$roles = ['ROLE_INVITEE', 'ROLE_STUDENT', 'ROLE_TEACHER', 'ROLE_ADMIN', 'ROLE_SUPER_ADMIN', 'ROLE_GLOBAL_ADMIN', 'ROLE_RRHH', 'ROLE_QUESTION_MANAGER', 'ROLE_SESSION_MANAGER', 'ROLE_STUDENT_BOSS']; |
|
52
|
|
|
|
|
53
|
|
|
if ($request->isMethod('POST')) { |
|
54
|
|
|
$data = $request->request->all('permissions'); |
|
55
|
|
|
foreach ($permissions as $permission) { |
|
56
|
|
|
foreach ($roles as $role) { |
|
57
|
|
|
$checkboxValue = isset($data[$permission->getSlug()][$role]); |
|
58
|
|
|
error_log('Processing role: ' . $role . ' with value: ' . ($checkboxValue ? 'true' : 'false')); |
|
59
|
|
|
$permRelRole = $permissionRelRoleRepo->findOneBy(['permission' => $permission, 'roleCode' => $role]); |
|
60
|
|
|
|
|
61
|
|
|
if ($checkboxValue) { |
|
62
|
|
|
if (!$permRelRole) { |
|
63
|
|
|
$permRelRole = new PermissionRelRole(); |
|
64
|
|
|
$permRelRole->setPermission($permission); |
|
65
|
|
|
$permRelRole->setRoleCode($role); |
|
66
|
|
|
} |
|
67
|
|
|
$permRelRole->setChangeable(true); |
|
68
|
|
|
$permRelRole->setUpdatedAt(new \DateTime()); |
|
69
|
|
|
$em->persist($permRelRole); |
|
70
|
|
|
error_log('Persisting PermissionRelRole for permission: ' . $permission->getSlug() . ' and role: ' . $role); |
|
71
|
|
|
} else { |
|
72
|
|
|
if ($permRelRole) { |
|
73
|
|
|
$em->remove($permRelRole); |
|
74
|
|
|
error_log('Removing PermissionRelRole for permission: ' . $permission->getSlug() . ' and role: ' . $role); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
$em->flush(); |
|
80
|
|
|
error_log('Flush complete'); |
|
81
|
|
|
|
|
82
|
|
|
return $this->redirectToRoute('permissions'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$forms = []; |
|
86
|
|
|
foreach ($permissions as $permission) { |
|
87
|
|
|
$defaultData = []; |
|
88
|
|
|
foreach ($roles as $role) { |
|
89
|
|
|
$permRelRole = $permissionRelRoleRepo->findOneBy(['permission' => $permission, 'roleCode' => $role]); |
|
90
|
|
|
$defaultData[$role] = $permRelRole ? $permRelRole->isChangeable() : false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$form = $this->createForm(PermissionType::class, $defaultData, ['roles' => $roles]); |
|
94
|
|
|
$forms[$permission->getSlug()] = $form->createView(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->render('@ChamiloCore/Permission/index.html.twig', [ |
|
98
|
|
|
'permissions' => $permissions, |
|
99
|
|
|
'forms' => $forms, |
|
100
|
|
|
'roles' => $roles |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|