Passed
Push — master ( 955e4f...05df7e )
by Jeff
02:10
created

DKIMCrudController::recreateKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 17
rs 9.9666
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\CrudUrlGenerator;
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
        return $crud
69
            ->setHelp(
70
                'edit',
71
                '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.'
72
            )
73
            ->setHelp(
74
                'new',
75
                '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.'
76
            )
77
            ->setSearchFields(['name'])
78
            ->overrideTemplate('crud/edit', 'admin/dkim/edit.html.twig')
79
            ->setPageTitle('index', 'DKIM');
80
    }
81
82
    public function configureActions(Actions $actions): Actions
83
    {
84
        $recreateKey = Action::new('recreateKey', 'Recreate Key', 'fa fa-file-invoice')->linkToCrudAction(
85
            'recreateKey'
86
        );
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
        $crudUrlGenerator = $this->get(CrudUrlGenerator::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
        return $this->redirect($crudUrlGenerator->build()->setAction(Action::EDIT)->generateUrl());
108
    }
109
110
    public function configureFields(string $pageName): iterable
111
    {
112
        $name = TextField::new('name')->setFormTypeOption('disabled', true);
113
        $dkimEnabled = BooleanField::new('dkimEnabled', 'Enabled');
114
        $dkimSelector = TextField::new('dkimSelector', 'Selector');
115
        $id = IdField::new('id', 'ID');
116
        $dkimStatusDkimRecordFound = BooleanField::new(
117
            'dkimStatus.dkimRecordFound',
118
            'Domain Key found'
119
        )->renderAsSwitch(false);
120
        $dkimStatusDkimRecordValid = BooleanField::new('dkimStatus.dkimRecordValid', 'Record valid')->renderAsSwitch(
121
            false
122
        );
123
124
        if (Crud::PAGE_DETAIL === $pageName) {
125
            return [$id, $name, $dkimEnabled, $dkimSelector];
126
        }
127
128
        if (Crud::PAGE_NEW === $pageName) {
129
            return [$name, $dkimEnabled, $dkimSelector];
130
        }
131
132
        if (Crud::PAGE_EDIT === $pageName) {
133
            return [$name, $dkimEnabled, $dkimSelector];
134
        }
135
136
        return [$name, $dkimEnabled, $dkimStatusDkimRecordFound, $dkimStatusDkimRecordValid];
137
    }
138
}
139