|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * --------------------------------------------------------------------- |
| 5 | + * |
| 6 | + * GLPI - Gestionnaire Libre de Parc Informatique |
| 7 | + * |
| 8 | + * http://glpi-project.org |
| 9 | + * |
| 10 | + * @copyright 2015-2025 Teclib' and contributors. |
| 11 | + * @licence https://www.gnu.org/licenses/gpl-3.0.html |
| 12 | + * |
| 13 | + * --------------------------------------------------------------------- |
| 14 | + * |
| 15 | + * LICENSE |
| 16 | + * |
| 17 | + * This file is part of GLPI. |
| 18 | + * |
| 19 | + * This program is free software: you can redistribute it and/or modify |
| 20 | + * it under the terms of the GNU General Public License as published by |
| 21 | + * the Free Software Foundation, either version 3 of the License, or |
| 22 | + * (at your option) any later version. |
| 23 | + * |
| 24 | + * This program is distributed in the hope that it will be useful, |
| 25 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | + * GNU General Public License for more details. |
| 28 | + * |
| 29 | + * You should have received a copy of the GNU General Public License |
| 30 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 31 | + * |
| 32 | + * --------------------------------------------------------------------- |
| 33 | + */ |
| 34 | + |
| 35 | +namespace Glpi\Controller\Form; |
| 36 | + |
| 37 | +use Glpi\Controller\AbstractController; |
| 38 | +use Glpi\Controller\Form\Utils\CanCheckAccessPolicies; |
| 39 | +use Glpi\Exception\Http\BadRequestHttpException; |
| 40 | +use Glpi\Exception\Http\NotFoundHttpException; |
| 41 | +use Glpi\Form\AnswersHandler\AnswersHandler; |
| 42 | +use Glpi\Form\EndUserInputNameProvider; |
| 43 | +use Glpi\Form\Form; |
| 44 | +use Glpi\Form\Section; |
| 45 | +use Glpi\Form\ValidationResult; |
| 46 | +use Glpi\Http\Firewall; |
| 47 | +use Glpi\Security\Attribute\SecurityStrategy; |
| 48 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 49 | +use Symfony\Component\HttpFoundation\Request; |
| 50 | +use Symfony\Component\HttpFoundation\Response; |
| 51 | +use Symfony\Component\Routing\Attribute\Route; |
| 52 | + |
| 53 | +final class ValidateAnswerController extends AbstractController |
| 54 | +{ |
| 55 | + use CanCheckAccessPolicies; |
| 56 | + |
| 57 | + #[SecurityStrategy(Firewall::STRATEGY_NO_CHECK)] // Some forms can be accessed anonymously |
| 58 | + #[Route( |
| 59 | + "/Form/ValidateAnswers", |
| 60 | + name: "glpi_form_validate_answers", |
| 61 | + methods: "POST" |
| 62 | + )] |
| 63 | + public function __invoke(Request $request): Response |
| 64 | + { |
| 65 | + $form = $this->loadSubmittedForm($request); |
| 66 | + $section = $this->loadSubmittedSection($request); |
| 67 | + $this->checkFormAccessPolicies($form, $request); |
| 68 | + |
| 69 | + $validation_result = $this->checkSubmittedAnswersValidation($section, $request); |
| 70 | + return new JsonResponse([ |
| 71 | + 'success' => $validation_result->isValid(), |
| 72 | + 'errors' => $validation_result->getErrors(), |
| 73 | + ]); |
| 74 | + } |
| 75 | + |
| 76 | + private function loadSubmittedForm(Request $request): Form |
| 77 | + { |
| 78 | + $forms_id = $request->request->getInt("forms_id"); |
| 79 | + if (!$forms_id) { |
| 80 | + throw new BadRequestHttpException(); |
| 81 | + } |
| 82 | + |
| 83 | + $form = Form::getById($forms_id); |
| 84 | + if (!$form) { |
| 85 | + throw new NotFoundHttpException(); |
| 86 | + } |
| 87 | + |
| 88 | + return $form; |
| 89 | + } |
| 90 | + |
| 91 | + private function loadSubmittedSection(Request $request): Section |
| 92 | + { |
| 93 | + $section_uuid = $request->request->getString("section_uuid"); |
| 94 | + if (!$section_uuid) { |
| 95 | + throw new BadRequestHttpException(); |
| 96 | + } |
| 97 | + |
| 98 | + $section = Section::getByUuid($section_uuid); |
| 99 | + if (!$section) { |
| 100 | + throw new NotFoundHttpException(); |
| 101 | + } |
| 102 | + |
| 103 | + return $section; |
| 104 | + } |
| 105 | + |
| 106 | + private function checkSubmittedAnswersValidation( |
| 107 | + Form|Section $questions_container, |
| 108 | + Request $request |
| 109 | + ): ValidationResult { |
| 110 | + $post = $request->request->all(); |
| 111 | + $provider = new EndUserInputNameProvider(); |
| 112 | + |
| 113 | + $answers = $provider->getAnswers($post); |
| 114 | + if (empty($answers)) { |
| 115 | + throw new BadRequestHttpException(); |
| 116 | + } |
| 117 | + |
| 118 | + $handler = AnswersHandler::getInstance(); |
| 119 | + return $handler->validateAnswers($questions_container, $answers); |
| 120 | + } |
| 121 | +} |
0 commit comments