-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProtectedAccessController.php
More file actions
147 lines (131 loc) · 5.74 KB
/
ProtectedAccessController.php
File metadata and controls
147 lines (131 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* NovaeZProtectedContentBundle.
*
* @package Novactive\Bundle\eZProtectedContentBundle
*
* @author Novactive
* @copyright 2019 Novactive
* @license https://github.com/Novactive/eZProtectedContentBundle/blob/master/LICENSE MIT Licence
*/
declare(strict_types=1);
namespace Novactive\Bundle\eZProtectedContentBundle\Controller\Admin;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\HttpCache\Handler\ContentTagInterface;
use Ibexa\Core\Repository\SiteAccessAware\Repository;
use Novactive\Bundle\eZProtectedContentBundle\Entity\ProtectedAccess;
use Novactive\Bundle\eZProtectedContentBundle\Form\ProtectedAccessType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
class ProtectedAccessController
{
public const GROUP='nova_protected_content';
public const STATE_DEFAULT='default';
public const STATE_PROTECTED='protected';
public function __construct(
protected readonly Repository $repository,
protected readonly \Ibexa\Contracts\Core\Search\Handler $searchHandler,
protected readonly \Ibexa\Contracts\Core\Persistence\Handler $persistenceHandler,
) { }
/**
* @Route("/handle/{locationId}/{access}", name="novaezprotectedcontent_bundle_admin_handle_form",
* defaults={"accessId": null})
*/
//#[Route(path: '/handle/{locationId}/{access}', name: 'novaezprotectedcontent_bundle_admin_handle_form')]
public function handle(
int $locationId,
Request $request,
FormFactoryInterface $formFactory,
EntityManagerInterface $entityManager,
RouterInterface $router,
ContentTagInterface $responseTagger,
?ProtectedAccess $access = null,
): RedirectResponse {
if ($request->isMethod('post')) {
$location = $this->repository->getLocationService()->loadLocation($locationId);
$now = new DateTime();
if (null === $access) {
$access = new ProtectedAccess();
$access->setCreated($now);
}
$form = $formFactory->create(ProtectedAccessType::class, $access);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$access->setUpdated($now);
$entityManager->persist($access);
$entityManager->flush();
$responseTagger->addLocationTags([$location->id]);
$responseTagger->addParentLocationTags([$location->parentLocationId]);
$content = $location->getContent();
$this->setState($content, SELF::STATE_DEFAULT);
if ($access->isProtectChildren()) {
$this->updateChildrenState($content, SELF::STATE_DEFAULT);
}
}
}
return new RedirectResponse(
$router->generate('ibexa.content.view', ['contentId' => $location->contentId,
'locationId' => $location->id,
]).
'#ibexa-tab-location-view-protect-content#tab'
);
}
#[Route(path: '/remove/{locationId}/{access}', name: 'novaezprotectedcontent_bundle_admin_remove_protection')]
public function remove(
Location $location,
EntityManagerInterface $entityManager,
RouterInterface $router,
int $access,
ContentTagInterface $responseTagger
): RedirectResponse {
$access = $entityManager->find(ProtectedAccess::class, $access);
$entityManager->remove($access);
$entityManager->flush();
$responseTagger->addLocationTags([$location->id]);
$responseTagger->addParentLocationTags([$location->parentLocationId]);
$content = $location->getContent();
$this->setState($content, SELF::STATE_DEFAULT);
if ($access->isProtectChildren()) {
$this->updateChildrenState($content, SELF::STATE_DEFAULT);
}
return new RedirectResponse(
$router->generate('ibexa.content.view', ['contentId' => $location->contentId,
'locationId' => $location->id,
]).
'#ibexa-tab-location-view-protect-content#tab'
);
}
protected function updateChildrenState(Content $content, string $state): void
{
$locations = $this->repository->getLocationService()->loadLocations($content->contentInfo);
$pathStringArray = [];
foreach ($locations as $location) {
/** @var Location $location */
$pathStringArray[] = $location->pathString;
}
if ($pathStringArray) {
$query = new Query();
$query->limit = 100;
$query->filter = new Query\Criterion\LogicalAnd([
new Query\Criterion\Subtree($pathStringArray)
]);
$searchResult = $this->repository->getSearchService()->findContent($query);
foreach ($searchResult->searchHits as $hit) {
$this->setState($hit->valueObject, $state);
}
}
}
public function setState(Content $content, string $state): void
{
$group = $this->repository->getObjectStateService()->loadObjectStateGroupByIdentifier(self::GROUP);
$state = $this->repository->getObjectStateService()->loadObjectStateByIdentifier($group,$state);
$this->repository->getObjectStateService()->setContentState($content->getContentInfo(), $group, $state);
}
}