Passed
Pull Request — master (#5651)
by Yannick
14:42 queued 05:42
created

PermissionController::index()   B

Complexity

Conditions 11
Paths 10

Size

Total Lines 59
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 39
c 1
b 0
f 0
nc 10
nop 4
dl 0
loc 59
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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