Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
91 / 91
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
UserController
100.00% covered (success)
100.00%
91 / 91
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 index
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 edit
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 list
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
1 / 1
5
 getBaseTemplateVars
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * The User Administration Controller
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2024-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2024-11-23
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Administration;
21
22use phpMyFAQ\Core\Exception;
23use phpMyFAQ\Enums\PermissionType;
24use phpMyFAQ\Filter;
25use phpMyFAQ\Pagination;
26use phpMyFAQ\Pagination\UrlConfig;
27use phpMyFAQ\Session\Token;
28use phpMyFAQ\Twig\Extensions\PermissionTranslationTwigExtension;
29use phpMyFAQ\User;
30use Symfony\Component\HttpFoundation\Request;
31use Symfony\Component\HttpFoundation\Response;
32use Symfony\Component\Routing\Attribute\Route;
33use Twig\Error\LoaderError;
34use Twig\Extension\AttributeExtension;
35
36final class UserController extends AbstractAdministrationController
37{
38    public function __construct(
39        private readonly User $user,
40    ) {
41        parent::__construct();
42    }
43
44    /**
45     * @throws Exception
46     * @throws LoaderError
47     * @throws \Exception
48     */
49    #[Route(path: '/user', name: 'admin.user', methods: ['GET'])]
50    public function index(Request $request): Response
51    {
52        $this->userHasPermission(PermissionType::USER_ADD);
53        $this->userHasPermission(PermissionType::USER_DELETE);
54        $this->userHasPermission(PermissionType::USER_EDIT);
55
56        $this->addExtension(new AttributeExtension(PermissionTranslationTwigExtension::class));
57        return $this->render('@admin/user/user.twig', [
58            ...$this->getHeader($request),
59            ...$this->getFooter(),
60            ...$this->getBaseTemplateVars(),
61        ]);
62    }
63
64    /**
65     * @throws Exception
66     * @throws LoaderError
67     * @throws \Exception
68     */
69    #[Route(path: '/user/edit/{userId}', name: 'admin.user.edit', methods: ['GET'])]
70    public function edit(Request $request): Response
71    {
72        $this->userHasPermission(PermissionType::USER_ADD);
73        $this->userHasPermission(PermissionType::USER_DELETE);
74        $this->userHasPermission(PermissionType::USER_EDIT);
75
76        $userId = (int) Filter::filterVar($request->attributes->get('userId'), FILTER_VALIDATE_INT);
77
78        $this->addExtension(new AttributeExtension(PermissionTranslationTwigExtension::class));
79        return $this->render('@admin/user/user.twig', [
80            ...$this->getHeader($request),
81            ...$this->getFooter(),
82            ...$this->getBaseTemplateVars(),
83            'userId' => $userId,
84        ]);
85    }
86
87    /**
88     * @throws Exception
89     * @throws LoaderError
90     * @throws \Exception
91     */
92    #[Route(path: '/user/list', name: 'admin.user.list', methods: ['GET'])]
93    public function list(Request $request): Response
94    {
95        $this->userHasPermission(PermissionType::USER_ADD);
96        $this->userHasPermission(PermissionType::USER_DELETE);
97        $this->userHasPermission(PermissionType::USER_EDIT);
98
99        $allUsers = $this->user->getAllUsers(false);
100        $numUsers = is_countable($allUsers) ? count($allUsers) : 0;
101
102        $page = Filter::filterVar($request->query->get('page'), FILTER_VALIDATE_INT, 0);
103        $perPage = 10;
104        $lastPage = $page * $perPage;
105        $firstPage = $lastPage - $perPage;
106
107        $pagination = new Pagination(
108            baseUrl: sprintf('%sadmin/user/list?page=%d', $this->configuration->getDefaultUrl(), $page),
109            total: $numUsers,
110            perPage: $perPage,
111            urlConfig: new UrlConfig(pageParamName: 'page'),
112        );
113
114        $counter = 0;
115        $displayedCounter = 0;
116        $users = [];
117        foreach ($allUsers as $allUser) {
118            $this->user->getUserById($allUser, true);
119
120            if ($displayedCounter >= $perPage) {
121                continue;
122            }
123
124            ++$counter;
125            if ($counter <= $firstPage) {
126                continue;
127            }
128
129            ++$displayedCounter;
130
131            $tempUser = [
132                'display_name' => $this->user->getUserData('display_name'),
133                'id' => $this->user->getUserId(),
134                'email' => $this->user->getUserData('email'),
135                'status' => $this->user->getStatus(),
136                'isSuperAdmin' => $this->user->isSuperAdmin(),
137                'isVisible' => $this->user->getUserData('is_visible'),
138                'login' => $this->user->getLogin(),
139            ];
140
141            $users[] = $tempUser;
142        }
143
144        return $this->render('@admin/user/user-list.twig', [
145            ...$this->getHeader($request),
146            ...$this->getFooter(),
147            ...$this->getBaseTemplateVars(),
148            'perPage' => $perPage,
149            'numUsers' => $numUsers,
150            'pagination' => $pagination->render(),
151            'users' => $users,
152            'userIsSuperAdmin' => $this->currentUser->isSuperAdmin(),
153        ]);
154    }
155
156    /**
157     * @throws \Exception
158     * @return array<string, mixed>
159     */
160    private function getBaseTemplateVars(): array
161    {
162        $currentUserId = $this->currentUser->getUserId();
163
164        return [
165            'permissionAddUser' => $this->currentUser?->perm->hasPermission(
166                $currentUserId,
167                PermissionType::USER_ADD->value,
168            ),
169            'permissionDeleteUser' => $this->currentUser->perm->hasPermission(
170                $currentUserId,
171                PermissionType::USER_DELETE->value,
172            ),
173            'permissionEditUser' => $this->currentUser->perm->hasPermission(
174                $currentUserId,
175                PermissionType::USER_EDIT->value,
176            ),
177            'csrfToken_updateUserData' => Token::getInstance($this->session)->getTokenString('update-user-data'),
178            'csrfToken_updateUserRights' => Token::getInstance($this->session)->getTokenString('update-user-rights'),
179            'csrfToken_activateUser' => Token::getInstance($this->session)->getTokenString('activate-user'),
180            'csrfToken_deleteUser' => Token::getInstance($this->session)->getTokenString('delete-user'),
181            'csrfToken_addUser' => Token::getInstance($this->session)->getTokenString('add-user'),
182            'csrfToken_overwritePassword' => Token::getInstance($this->session)->getTokenString('overwrite-password'),
183            'currentUserId' => $currentUserId,
184            'userRights' => $this->user->perm->getAllRightsData(),
185            'userIsSuperAdmin' => $this->currentUser->isSuperAdmin(),
186        ];
187    }
188}