-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathAdminFederationController.php
More file actions
154 lines (128 loc) · 5.99 KB
/
Copy pathAdminFederationController.php
File metadata and controls
154 lines (128 loc) · 5.99 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
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\DTO\ConfirmDefederationDto;
use App\DTO\FederationSettingsDto;
use App\Form\ConfirmDefederationType;
use App\Form\FederationSettingsType;
use App\Repository\InstanceRepository;
use App\Service\InstanceManager;
use App\Service\SettingsManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class AdminFederationController extends AbstractController
{
public function __construct(
private readonly SettingsManager $settingsManager,
private readonly InstanceRepository $instanceRepository,
private readonly EntityManagerInterface $entityManager,
private readonly InstanceManager $instanceManager,
) {
}
#[IsGranted('ROLE_ADMIN')]
public function __invoke(Request $request): Response
{
$settings = $this->settingsManager->getDto();
$dto = new FederationSettingsDto(
$settings->KBIN_FEDERATION_ENABLED,
$settings->MBIN_USE_FEDERATION_ALLOW_LIST,
$settings->KBIN_FEDERATION_PAGE_ENABLED,
);
$form = $this->createForm(FederationSettingsType::class, $dto);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var FederationSettingsDto $dto */
$dto = $form->getData();
$settings->KBIN_FEDERATION_ENABLED = $dto->federationEnabled;
$settings->MBIN_USE_FEDERATION_ALLOW_LIST = $dto->federationUsesAllowList;
$settings->KBIN_FEDERATION_PAGE_ENABLED = $dto->federationPageEnabled;
$this->settingsManager->save($settings);
return $this->redirectToRoute('admin_federation');
}
$useAllowList = $this->settingsManager->getUseAllowList();
return $this->render(
'admin/federation.html.twig',
[
'form' => $form->createView(),
'useAllowList' => $useAllowList,
'instances' => $useAllowList ? $this->settingsManager->getAllowedInstances() : $this->settingsManager->getBannedInstances(),
'allInstances' => $this->instanceRepository->findAllOrdered(),
]
);
}
#[IsGranted('ROLE_ADMIN')]
public function banInstance(#[MapQueryParameter] string $instanceDomain, Request $request): Response
{
$instance = $this->instanceRepository->getOrCreateInstance($instanceDomain);
$form = $this->createForm(ConfirmDefederationType::class, new ConfirmDefederationDto());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var ConfirmDefederationDto $dto */
$dto = $form->getData();
if ($dto->confirm) {
$this->instanceManager->banInstance($instance);
return $this->redirectToRoute('admin_federation');
} else {
$this->addFlash('error', 'flash_error_defederation_must_confirm');
}
}
return $this->render('admin/federation_defederate_instance.html.twig', [
'form' => $form->createView(),
'instance' => $instance,
'counts' => $this->instanceRepository->getInstanceCounts($instance),
'useAllowList' => $this->settingsManager->getUseAllowList(),
], new Response(status: $form->isSubmitted() && !$form->isValid() ? 422 : 200));
}
#[IsGranted('ROLE_ADMIN')]
public function unbanInstance(#[MapQueryParameter] string $instanceDomain): Response
{
$instance = $this->instanceRepository->getOrCreateInstance($instanceDomain);
$this->instanceManager->unbanInstance($instance);
return $this->redirectToRoute('admin_federation');
}
#[IsGranted('ROLE_ADMIN')]
public function allowInstance(#[MapQueryParameter] string $instanceDomain): Response
{
$instance = $this->instanceRepository->getOrCreateInstance($instanceDomain);
$this->instanceManager->allowInstanceFederation($instance);
return $this->redirectToRoute('admin_federation');
}
#[IsGranted('ROLE_ADMIN')]
public function denyInstance(#[MapQueryParameter] string $instanceDomain, Request $request): Response
{
$instance = $this->instanceRepository->getOrCreateInstance($instanceDomain);
$form = $this->createForm(ConfirmDefederationType::class, new ConfirmDefederationDto());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var ConfirmDefederationDto $dto */
$dto = $form->getData();
if ($dto->confirm) {
$this->instanceManager->denyInstanceFederation($instance);
return $this->redirectToRoute('admin_federation');
} else {
$this->addFlash('error', 'flash_error_defederation_must_confirm');
}
}
return $this->render('admin/federation_defederate_instance.html.twig', [
'form' => $form->createView(),
'instance' => $instance,
'counts' => $this->instanceRepository->getInstanceCounts($instance),
'useAllowList' => $this->settingsManager->getUseAllowList(),
], new Response(status: $form->isSubmitted() && !$form->isValid() ? 422 : 200));
}
#[IsGranted('ROLE_ADMIN')]
public function blockInstanceGlobally(#[MapQueryParameter] string $instanceDomain): Response
{
$instance = $this->instanceRepository->findOneBy(['domain' => $instanceDomain]);
if (null === $instance) {
throw new NotFoundHttpException('instance '.$instanceDomain.' not found');
}
$this->instanceManager->blockInstancesGlobally([$instance]);
return $this->redirectToRoute('admin_federation');
}
}