DKIMCrudController::edit()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Controller\Admin;
12
13
use App\Entity\Domain;
14
use App\Service\DKIM\FormatterService;
15
use App\Service\DKIM\KeyGenerationService;
16
use DomainException;
17
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
18
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
19
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
20
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
21
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
22
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
23
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
24
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
25
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
26
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
27
use Symfony\Component\HttpFoundation\Response;
28
29
class DKIMCrudController extends AbstractCrudController
30
{
31
    private FormatterService $formatterService;
32
    private KeyGenerationService $keyGenerationService;
33
34
    public function __construct(FormatterService $formatterService, KeyGenerationService $keyGenerationService)
35
    {
36
        $this->formatterService = $formatterService;
37
        $this->keyGenerationService = $keyGenerationService;
38
    }
39
40
    public static function getEntityFqcn(): string
41
    {
42
        return Domain::class;
43
    }
44
45
    public function edit(AdminContext $context)
46
    {
47
        $parameters = parent::edit($context);
48
49
        if ($parameters instanceof KeyValueStore && null !== ($entityDto = $parameters->get('entity'))) {
50
            /** @var Domain $entity */
51
            $entity = $entityDto->getInstance();
52
53
            if ('' !== $entity->getDkimPrivateKey()) {
54
                $expectedRecord = $this->formatterService->getTXTRecord(
55
                    $this->keyGenerationService->extractPublicKey($entity->getDkimPrivateKey()),
56
                    KeyGenerationService::DIGEST_ALGORITHM
57
                );
58
                $entity->setExpectedDnsRecord(wordwrap($expectedRecord, 40, "\n", true));
59
                $entity->setCurrentDnsRecord(wordwrap($entity->getDkimStatus()->getCurrentRecord(), 40, "\n", true));
60
            }
61
        }
62
63
        return $parameters;
64
    }
65
66
    public function configureCrud(Crud $crud): Crud
67
    {
68
        $helpMessage = 'If you enable DKIM for this domain, all outgoing mails will have a DKIM signature attached. You should set up the DNS record before doing this. After you have generated a private key, a DNS record is provided that needs to be added to your domain\'s zone.';
69
70
        return $crud
71
            ->setHelp(Crud::PAGE_EDIT, $helpMessage)
72
            ->setHelp(Crud::PAGE_NEW, $helpMessage)
73
            ->setSearchFields(['name'])
74
            ->overrideTemplate('crud/edit', 'admin/dkim/edit.html.twig')
75
            ->setPageTitle(Crud::PAGE_INDEX, 'DKIM');
76
    }
77
78
    public function configureActions(Actions $actions): Actions
79
    {
80
        $recreateKey = Action::new(
81
            'recreateKey',
82
            'Recreate Key',
83
            'fas fa-certificate'
84
        )
85
            ->linkToCrudAction('recreateKey')
86
            ->setCssClass('btn btn-danger');
87
88
        return $actions->add(Crud::PAGE_EDIT, $recreateKey);
89
    }
90
91
    public function recreateKey(AdminContext $adminContext): Response
92
    {
93
        $domain = $adminContext->getEntity()->getInstance();
94
        $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
95
96
        if (!$domain) {
97
            throw new DomainException('Domain not found.');
98
        }
99
100
        $keyPair = $this->keyGenerationService->createKeyPair();
101
        $domain->setDkimPrivateKey($keyPair->getPrivate());
102
103
        $this->addFlash('info', 'Private key successfully recreated. You need to update your DNS zone now.');
104
105
        $this->getDoctrine()->getManager()->flush();
106
107
        $url = $adminUrlGenerator
108
            ->setController(DKIMCrudController::class)
109
            ->setAction(Crud::PAGE_EDIT)
110
            ->setEntityId($domain->getId())
111
            ->generateUrl();
112
113
        return $this->redirect($url);
114
    }
115
116
    public function configureFields(string $pageName): iterable
117
    {
118
        $name = TextField::new('name')->setFormTypeOption('disabled', true);
119
        $dkimEnabled = BooleanField::new('dkimEnabled', 'Enabled');
120
        $dkimSelector = TextField::new('dkimSelector', 'Selector');
121
        $id = IdField::new('id', 'ID');
122
        $dkimStatusDkimRecordFound = BooleanField::new(
123
            'dkimStatus.dkimRecordFound',
124
            'Domain Key found'
125
        )->renderAsSwitch(false);
126
        $dkimStatusDkimRecordValid = BooleanField::new('dkimStatus.dkimRecordValid', 'Record valid')->renderAsSwitch(
127
            false
128
        );
129
130
        if (Crud::PAGE_DETAIL === $pageName) {
131
            return [$id, $name, $dkimEnabled, $dkimSelector];
132
        }
133
134
        if (Crud::PAGE_NEW === $pageName) {
135
            return [$name, $dkimEnabled, $dkimSelector];
136
        }
137
138
        if (Crud::PAGE_EDIT === $pageName) {
139
            return [$name, $dkimEnabled, $dkimSelector];
140
        }
141
142
        return [$name, $dkimEnabled, $dkimStatusDkimRecordFound, $dkimStatusDkimRecordValid];
143
    }
144
}
145