Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

AuditLogEntry::mapEventToAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Identity\Entity;
20
21
use Doctrine\ORM\Mapping as ORM;
22
use JsonSerializable;
23
use Surfnet\StepupMiddleware\ApiBundle\Exception\LogicException;
24
25
/**
26
 * @SuppressWarnings(PHPMD.UnusedPrivateField)
27
 *
28
 * @ORM\Entity(repositoryClass="Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\AuditLogRepository")
29
 * @ORM\Table(
30
 *      name="audit_log",
31
 *      indexes={
32
 *          @ORM\Index(name="idx_auditlog_actorid", columns={"actor_id"}),
33
 *          @ORM\Index(name="idx_auditlog_identityid", columns={"identity_id"}),
34
 *          @ORM\Index(name="idx_auditlog_identityinstitution", columns={"identity_institution"}),
35
 *          @ORM\Index(name="idx_auditlog_secondfactorid", columns={"second_factor_id"})
36
 *      }
37
 * )
38
 */
39
class AuditLogEntry implements JsonSerializable
40
{
41
    /**
42
     * Maps event FQCNs to action names.
43
     *
44
     * @var string[]
45
     */
46
    private static $eventActionMap = [
47
        'Surfnet\Stepup\Identity\Event\CompliedWithUnverifiedSecondFactorRevocationEvent' => 'revoked_by_ra',
48
        'Surfnet\Stepup\Identity\Event\CompliedWithVerifiedSecondFactorRevocationEvent'   => 'revoked_by_ra',
49
        'Surfnet\Stepup\Identity\Event\CompliedWithVettedSecondFactorRevocationEvent'     => 'revoked_by_ra',
50
        'Surfnet\Stepup\Identity\Event\EmailVerifiedEvent'                                => 'email_verified',
51
        'Surfnet\Stepup\Identity\Event\GssfPossessionProvenEvent'                         => 'possession_proven',
52
        'Surfnet\Stepup\Identity\Event\IdentityCreatedEvent'                              => 'created',
53
        'Surfnet\Stepup\Identity\Event\IdentityEmailChangedEvent'                         => 'email_changed',
54
        'Surfnet\Stepup\Identity\Event\IdentityRenamedEvent'                              => 'renamed',
55
        'Surfnet\Stepup\Identity\Event\PhonePossessionProvenEvent'                        => 'possession_proven',
56
        'Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent'                           => 'vetted',
57
        'Surfnet\Stepup\Identity\Event\UnverifiedSecondFactorRevokedEvent'                => 'revoked',
58
        'Surfnet\Stepup\Identity\Event\VerifiedSecondFactorRevokedEvent'                  => 'revoked',
59
        'Surfnet\Stepup\Identity\Event\VettedSecondFactorRevokedEvent'                    => 'revoked',
60
        'Surfnet\Stepup\Identity\Event\YubikeyPossessionProvenEvent'                      => 'possession_proven',
61
        'Surfnet\Stepup\Identity\Event\YubikeySecondFactorBootstrappedEvent'              => 'bootstrapped',
62
        'Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaEvent'                      => 'accredited_as_raa',
63
        'Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaEvent'                       => 'accredited_as_ra',
64
        'Surfnet\Stepup\Identity\Event\AppointedAsRaaEvent'                               => 'appointed_as_raa',
65
        'Surfnet\Stepup\Identity\Event\AppointedAsRaEvent'                                => 'appointed_as_ra',
66
        'Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedEvent'               => 'retracted_as_ra',
67
    ];
68
69
    /**
70
     * @ORM\Id
71
     * @ORM\Column(length=36)
72
     *
73
     * @var string
74
     */
75
    public $id;
76
77
    /**
78
     * @ORM\Column(length=36, nullable=true)
79
     *
80
     * @var string|null
81
     */
82
    public $actorId;
83
84
    /**
85
     * @ORM\Column(type="stepup_common_name", nullable=true)
86
     *
87
     * @var \Surfnet\Stepup\Identity\Value\CommonName
88
     */
89
    public $actorCommonName;
90
91
    /**
92
     * @ORM\Column(type="institution", nullable=true)
93
     *
94
     * @var \Surfnet\Stepup\Identity\Value\Institution|null
95
     */
96
    public $actorInstitution;
97
98
    /**
99
     * @ORM\Column(length=36)
100
     *
101
     * @var string
102
     */
103
    public $identityId;
104
105
    /**
106
     * @ORM\Column(type="institution")
107
     *
108
     * @var \Surfnet\Stepup\Identity\Value\Institution
109
     */
110
    public $identityInstitution;
111
112
    /**
113
     * @ORM\Column(length=36, nullable=true)
114
     *
115
     * @var string|null
116
     */
117
    public $secondFactorId;
118
119
    /**
120
     * @ORM\Column(length=255, nullable=true)
121
     *
122
     * @var string
123
     */
124
    public $secondFactorIdentifier;
125
126
    /**
127
     * @ORM\Column(length=36, nullable=true)
128
     *
129
     * @var string|null
130
     */
131
    public $secondFactorType;
132
133
    /**
134
     * @ORM\Column(length=255)
135
     *
136
     * @var string
137
     */
138
    public $event;
139
140
    /**
141
     * @ORM\Column(type="stepup_datetime")
142
     *
143
     * @var \Surfnet\Stepup\DateTime\DateTime
144
     */
145
    public $recordedOn;
146
147
    public function jsonSerialize()
148
    {
149
        return [
150
            'actor_id'                 => $this->actorId,
151
            'actor_institution'        => $this->actorInstitution ? (string) $this->actorInstitution : null,
152
            'actor_common_name'        => $this->actorCommonName,
153
            'identity_id'              => $this->identityId,
154
            'identity_institution'     => (string) $this->identityInstitution,
155
            'second_factor_id'         => $this->secondFactorId,
156
            'second_factor_type'       => $this->secondFactorType ? (string) $this->secondFactorType : null,
157
            'second_factor_identifier' => $this->secondFactorIdentifier,
158
            'action'                   => $this->mapEventToAction($this->event),
159
            'recorded_on'              => (string) $this->recordedOn,
160
        ];
161
    }
162
163
    /**
164
     * Maps an event FQCN to an action name (eg. '...\Event\IdentityCreatedEvent' to 'created').
165
     *
166
     * @param string $event Event FQCN
167
     * @return string Action name
168
     */
169
    private function mapEventToAction($event)
170
    {
171
        if (!isset(self::$eventActionMap[$event])) {
172
            throw new LogicException(sprintf("Action name for event '%s' not registered", $event));
173
        }
174
175
        return self::$eventActionMap[$event];
176
    }
177
}
178