Issues (3627)

ApiBundle/EventListener/ClientSubscriber.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2019 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\ApiBundle\EventListener;
13
14
use Mautic\ApiBundle\ApiEvents;
15
use Mautic\ApiBundle\Event as Events;
16
use Mautic\CoreBundle\Helper\CoreParametersHelper;
17
use Mautic\CoreBundle\Helper\IpLookupHelper;
18
use Mautic\CoreBundle\Model\AuditLogModel;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
class ClientSubscriber implements EventSubscriberInterface
22
{
23
    /**
24
     * @var IpLookupHelper
25
     */
26
    private $ipLookupHelper;
27
28
    /**
29
     * @var AuditLogModel
30
     */
31
    private $auditLogModel;
32
33
    public function __construct(
34
        IpLookupHelper $ipLookupHelper,
35
        AuditLogModel $auditLogModel
36
    ) {
37
        $this->ipLookupHelper       = $ipLookupHelper;
38
        $this->auditLogModel        = $auditLogModel;
39
    }
40
41
    public static function getSubscribedEvents(): array
42
    {
43
        return [
44
            ApiEvents::CLIENT_POST_SAVE   => ['onClientPostSave', 0],
45
            ApiEvents::CLIENT_POST_DELETE => ['onClientDelete', 0],
46
        ];
47
    }
48
49
    /**
50
     * Add a client change entry to the audit log.
51
     */
52
    public function onClientPostSave(Events\ClientEvent $event): void
53
    {
54
        $client = $event->getClient();
55
        if (!$details = $event->getChanges()) {
56
            return;
57
        }
58
59
        $log = [
60
            'bundle'    => 'api',
61
            'object'    => 'client',
62
            'objectId'  => $client->getId(),
63
            'action'    => ($event->isNew()) ? 'create' : 'update',
64
            'details'   => $details,
65
            'ipAddress' => $this->ipLookupHelper->getIpAddressFromRequest(),
66
        ];
67
        $this->auditLogModel->writeToLog($log);
68
    }
69
70
    /**
71
     * Add a role delete entry to the audit log.
72
     */
73
    public function onClientDelete(Events\ClientEvent $event): void
74
    {
75
        $client = $event->getClient();
76
        $log    = [
77
            'bundle'    => 'api',
78
            'object'    => 'client',
79
            'objectId'  => $client->deletedId,
0 ignored issues
show
The property deletedId does not seem to exist on Mautic\ApiBundle\Entity\oAuth2\Client.
Loading history...
80
            'action'    => 'delete',
81
            'details'   => ['name' => $client->getName()],
82
            'ipAddress' => $this->ipLookupHelper->getIpAddressFromRequest(),
83
        ];
84
        $this->auditLogModel->writeToLog($log);
85
    }
86
}
87