UserController::privacyPolicy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\LegalBundle\Controller;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
18
use Nucleos\UserBundle\Security\LoginManager;
19
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
20
use Symfony\Bundle\SecurityBundle\Security;
21
use Symfony\Component\DependencyInjection\Attribute\Autowire;
22
use Symfony\Component\HttpFoundation\RedirectResponse;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\Routing\Attribute\Route;
26
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
27
use Zikula\CoreBundle\Site\SiteDefinitionInterface;
28
use Zikula\LegalBundle\Form\Type\AcceptPoliciesType;
29
use Zikula\LegalBundle\Helper\AcceptPoliciesHelper;
30
use Zikula\ThemeBundle\Controller\Dashboard\UserDashboardController;
31
use Zikula\UsersBundle\Entity\User;
32
use Zikula\UsersBundle\Repository\UserRepositoryInterface;
33
34
#[Route('/legal')]
35
class UserController extends AbstractController
36
{
37
    private string $firewallName;
38
39
    public function __construct(
40
        private readonly SiteDefinitionInterface $site,
41
        private readonly array $legalConfig,
42
        private readonly LoginManager $loginManager,
43
        #[Autowire(param: 'nucleos_user.firewall_name')]
44
        string $firewallName
45
    ) {
46
        $this->firewallName = $firewallName;
47
    }
48
49
    /**
50
     * Main user function.
51
     * Redirects to the legal notice document.
52
     */
53
    #[Route('', name: 'zikulalegalbundle_user_index', methods: ['GET'])]
54
    public function index(AdminUrlGenerator $urlGenerator): RedirectResponse
55
    {
56
        $url = $urlGenerator
57
            ->setDashboard(UserDashboardController::class)
58
            // ->setController(self::class)
59
            ->setRoute('zikulalegalbundle_user_legalnotice')
60
            ->generateUrl();
61
        $url = str_replace('/admin?route', '/en?route', $url); // TODO remove hack
62
63
        return $this->redirect($url);
64
    }
65
66
    /**
67
     * Display Legal notice.
68
     *
69
     * @throws AccessDeniedException Thrown if the user does not have the appropriate access level for the function
70
     */
71
    #[Route('/legalnotice', name: 'zikulalegalbundle_user_legalnotice', methods: ['GET'])]
72
    public function legalNotice(): Response
73
    {
74
        return $this->renderDocument('legalNotice', 'legal_notice');
75
    }
76
77
    /**
78
     * Display Privacy Policy
79
     *
80
     * @throws AccessDeniedException Thrown if the user does not have the appropriate access level for the function
81
     */
82
    #[Route('/privacypolicy', name: 'zikulalegalbundle_user_privacypolicy', methods: ['GET'])]
83
    public function privacyPolicy(): Response
84
    {
85
        return $this->renderDocument('privacyPolicy', 'privacy_policy');
86
    }
87
88
    /**
89
     * Display Terms of Use
90
     *
91
     * @throws AccessDeniedException Thrown if the user does not have the appropriate access level for the function
92
     */
93
    #[Route('/termsofuse', name: 'zikulalegalbundle_user_termsofuse', methods: ['GET'])]
94
    public function termsOfUse(): Response
95
    {
96
        return $this->renderDocument('termsOfUse', 'terms_of_use');
97
    }
98
99
    /**
100
     * Display Accessibility statement
101
     *
102
     * @throws AccessDeniedException Thrown if the user does not have the appropriate access level for the function
103
     */
104
    #[Route('/accessibilitystatement', name: 'zikulalegalbundle_user_accessibilitystatement', methods: ['GET'])]
105
    public function accessibilityStatement(): Response
106
    {
107
        return $this->renderDocument('accessibilityStatement', 'accessibility');
108
    }
109
110
    /**
111
     * Display Trade conditions
112
     *
113
     * @throws AccessDeniedException Thrown if the user does not have the appropriate access level for the function
114
     */
115
    #[Route('/tradeconditions', name: 'zikulalegalbundle_user_tradeconditions', methods: ['GET'])]
116
    public function tradeConditions(): Response
117
    {
118
        return $this->renderDocument('tradeConditions', 'trade_conditions');
119
    }
120
121
    /**
122
     * Display Cancellation right policy
123
     *
124
     * @throws AccessDeniedException Thrown if the user does not have the appropriate access level for the function
125
     */
126
    #[Route('/cancellationrightpolicy', name: 'zikulalegalbundle_user_cancellationrightpolicy', methods: ['GET'])]
127
    public function cancellationRightPolicy(): Response
128
    {
129
        return $this->renderDocument('cancellationRightPolicy', 'cancellation_right_policy');
130
    }
131
132
    /**
133
     * Render and display the specified legal document, or redirect to the specified custom URL if it exists.
134
     *
135
     * If a custom URL for the legal document exists, as specified by the bundle configuration, then
136
     * this function will redirect the user to that URL.
137
     *
138
     * If no custom URL exists, then this function will render and return the appropriate template for the legal document.
139
     *
140
     * @throws AccessDeniedException Thrown if the user does not have the appropriate access level for the function
141
     */
142
    private function renderDocument(string $documentName, string $policyConfigKey): Response
143
    {
144
        $policyConfig = $this->legalConfig['policies'][$policyConfigKey];
145
        if (!$policyConfig['enabled']) {
146
            return $this->render('@ZikulaLegal/User/Policy/Display/policyNotActive.html.twig');
147
        }
148
149
        $customUrl = $policyConfig['custom_url'] ?: null;
150
        if (!empty($customUrl)) {
151
            return $this->redirect($customUrl);
152
        }
153
154
        return $this->render('@ZikulaLegal/User/Policy/Display/' . $documentName . '.html.twig', [
155
            'adminMail' => $this->site->getAdminMail(),
156
        ]);
157
    }
158
159
    #[Route('/acceptpolicies', name: 'zikulalegalbundle_user_acceptpolicies')]
160
    public function acceptPolicies(
161
        Request $request,
162
        ManagerRegistry $doctrine,
163
        Security $security,
164
        UserRepositoryInterface $userRepository,
165
        AcceptPoliciesHelper $acceptPoliciesHelper
166
    ): Response {
167
        $currentUser = $security->getUser();
168
        $loginRequired = null === $currentUser;
169
        $userId = $currentUser?->getId() ?? 0;
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of Symfony\Component\Security\Core\User\UserInterface such as Zikula\UsersBundle\Entity\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

169
        $userId = $currentUser?->/** @scrutinizer ignore-call */ getId() ?? 0;
Loading history...
170
171
        $form = $this->createForm(AcceptPoliciesType::class, $currentUser, [
172
            'userId' => $userId,
173
            'loginRequired' => $loginRequired,
174
        ]);
175
        $form->handleRequest($request);
176
        if ($form->isSubmitted() && $form->isValid()) {
177
            $data = $form->getData();
178
            /** @var User $userEntity */
179
            $userEntity = $userRepository->find($data['userId']);
180
            $policiesToCheck = $acceptPoliciesHelper->getActivePolicies();
181
            $nowUTC = new \DateTime('now', new \DateTimeZone('UTC'));
182
            foreach ($policiesToCheck as $policyName => $isEnabled) {
183
                $setter = 'set' . ucfirst($policyName);
184
                $userEntity->{$setter}($data[$policyName . 'Accepted'] && $isEnabled ? $nowUTC : null);
185
            }
186
            $doctrine->getManager()->flush();
187
            if ($data['hasAcceptedPolicies'] && $data['loginRequired']) {
188
                $this->loginManager->logInUser($this->firewallName, $userEntity);
189
            }
190
191
            return $this->redirectToRoute('user_home');
192
        }
193
194
        return $this->render('@ZikulaLegal/User/acceptPolicies.html.twig', [
195
            'loginRequired' => $loginRequired,
196
            'form' => $form,
197
        ]);
198
    }
199
}
200