Skip to content

Commit ba1caf9

Browse files
committed
feat: Add UserAction formatter
1 parent 97ff4ea commit ba1caf9

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters\ChildEntityFormatters;
4+
5+
use Models\UserAction;
6+
7+
/**
8+
* Copyright 2022 OpenStack Foundation
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
**/
19+
20+
21+
/**
22+
* Class UserActionAuditLogFormatter
23+
* @package App\Audit\ConcreteFormatters
24+
*/
25+
class UserActionAuditLogFormatter implements IChildEntityAuditLogFormatter
26+
{
27+
/**
28+
* @param UserAction $subject
29+
* @param string $child_entity_action_type
30+
* @param string|null $additional_info
31+
* @return string|null
32+
*/
33+
public function format($subject, string $child_entity_action_type, ?string $additional_info = ""): ?string
34+
{
35+
36+
if (!$subject instanceof UserAction) {
37+
return null;
38+
}
39+
40+
$owner = $subject->getOwner();
41+
$realm = $subject->hasRealm() ? $subject->getRealm() : 'N/A';
42+
$ip = $subject->getFromIp();
43+
44+
switch ($child_entity_action_type) {
45+
case IChildEntityAuditLogFormatter::CHILD_ENTITY_CREATION:
46+
return "A new UserAction which owner is \"{$owner->getFullName()} ({$owner->getID()})\", with realm \"{$realm}\" and IP \"{$ip}\" was created. {$additional_info}";
47+
case IChildEntityAuditLogFormatter::CHILD_ENTITY_UPDATE:
48+
return "An UserAction with ID {$subject->getID()} was changed. {$additional_info}";
49+
case IChildEntityAuditLogFormatter::CHILD_ENTITY_DELETION:
50+
return "An UserAction with ID {$subject->getID()} which owner is \"{$owner->getFullName()} ({$owner->getID()})\", with realm \"{$realm}\" and IP \"{$ip}\" was removed";
51+
}
52+
return "";
53+
}
54+
}

0 commit comments

Comments
 (0)