From 97e605492e87ecd8acc4498df2b59ba1af3728c4 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 2 Dec 2018 10:50:25 +0100 Subject: [PATCH] Next step for audit implementation --- Admin/Hooks/Web/Api.php | 6 +- Controller/ApiController.php | 15 ++- Models/Audit.php | 199 ++++++++++++++++++++++++++++++++--- Models/AuditMapper.php | 24 +++++ 4 files changed, 226 insertions(+), 18 deletions(-) diff --git a/Admin/Hooks/Web/Api.php b/Admin/Hooks/Web/Api.php index 74b5974..efa4c1c 100644 --- a/Admin/Hooks/Web/Api.php +++ b/Admin/Hooks/Web/Api.php @@ -1,13 +1,13 @@ [ + 'POST:Module:.*?\-create' => [ 'callback' => ['\Modules\Auditor\Controller\ApiController:apiLogCreate'], ], - 'PRE:Module:.*?\-update' => [ + 'POST:Module:.*?\-update' => [ 'callback' => ['\Modules\Auditor\Controller\ApiController:apiLogUpdate'], ], - 'PRE:Module:.*?\-delete' => [ + 'POST:Module:.*?\-delete' => [ 'callback' => ['\Modules\Auditor\Controller\ApiController:apiLogDelete'], ], ]; diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 45c7af4..6c19ecf 100644 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -24,6 +24,9 @@ use phpOMS\Account\Account; use phpOMS\Account\PermissionType; use phpOMS\DataStorage\Database\RelationType; +use Modules\Auditor\Models\Audit; +use Modules\Auditor\Models\AuditMapper; + final class ApiController extends Controller { public function apiLogCreate( @@ -33,10 +36,12 @@ final class ApiController extends Controller int $type = 0, int $subtype = 0, string $module = null, + string $ref = null, string $content = null ) : void { - + $audit = new Audit($account, null, $new->__toString(), $type, $subtype, $module, $ref, $content); + AuditMapper::create($audit); } public function apiLogUpdate( @@ -46,10 +51,12 @@ final class ApiController extends Controller int $type = 0, int $subtype = 0, string $module = null, + string $ref = null, string $content = null ) : void { - + $audit = new Audit($account, $old->__toString(), $new->__toString(), $type, $subtype, $module, $ref, $content); + AuditMapper::create($audit); } public function apiLogDelete( @@ -59,9 +66,11 @@ final class ApiController extends Controller int $type = 0, int $subtype = 0, string $module = null, + string $ref = null, string $content = null ) : void { - + $audit = new Audit($account, $new->__toString(), null, $type, $subtype, $module, $ref, $content); + AuditMapper::create($audit); } } diff --git a/Models/Audit.php b/Models/Audit.php index bc1b204..922cd60 100644 --- a/Models/Audit.php +++ b/Models/Audit.php @@ -16,80 +16,255 @@ namespace Modules\Auditor\Models; use phpOMS\Account\Account; +/** + * Audit class. + * + * @package Modules\Auditor + * @license OMS License 1.0 + * @link http://website.orange-management.de + * @since 1.0.0 + */ class Audit { + /** + * Audit id. + * + * @var int + * @since 1.0.0 + */ private $id = 0; + /** + * Audit account. + * + * @var int|Account + * @since 1.0.0 + */ + private $account = 0; + + /** + * Audit type. + * + * @var int + * @since 1.0.0 + */ private $type = 0; + /** + * Audit subtype. + * + * @var int + * @since 1.0.0 + */ private $subtype = 0; - private $module = 0; + /** + * Audit module. + * + * @var string|null + * @since 1.0.0 + */ + private $module = null; - private $ref = ''; + /** + * Audit reference. + * + * This could be used to reference other model ids + * + * @var string|null + * @since 1.0.0 + */ + private $ref = null; - private $content = ''; + /** + * Audit content. + * + * Additional audit information + * + * @var string|null + * @since 1.0.0 + */ + private $content = null; - private $old = ''; + /** + * Old value. + * + * @var string|null + * @since 1.0.0 + */ + private $old = null; - private $new = ''; + /** + * New value. + * + * @var string|null + * @since 1.0.0 + */ + private $new = null; + /** + * Account. + * + * @var Account|int + * @since 1.0.0 + */ private $createdBy = 0; + /** + * Created at. + * + * @var \DateTime + * @since 1.0.0 + */ private $createdAt = null; + /** + * Ip of creator. + * + * @var int + * @since 1.0.0 + */ + private $ip = 0; + + /** + * Constructor. + * + * @parram Account $account Account of the creator + * @parram string $old Old value + * @parram string $new New value + * @parram int $type Type of the audit + * @parram int $subtype Subtype of the audit + * @parram string $module Module id + * @parram string $content Additional audit information + * + * @since 1.0.0 + */ public function __construct( Account $account, - string $old, - string $new, + ?string $old, + ?string $new, int $type = 0, int $subtype = 0, string $module = null, + string $ref = null, string $content = null ) { $this->createdAt = new \DateTime('now'); + $this->account = $account; + $this->old = $old; + $this->new = $new; + $this->type = $type; + $this->subtype = $subtype; + $this->module = $module; + $this->ref = $ref; + $this->content = $content; } + /** + * Get type. + * + * @return int + * + * @since 1.0.0 + */ public function getType() : int { return $this->type; } + /** + * Get subtype. + * + * @return int + * + * @since 1.0.0 + */ public function getSubType() : int { return $this->subtype; } - public function getModule() : int + /** + * Get Module. + * + * @return string|null + * + * @since 1.0.0 + */ + public function getModule() : ?string { return $this->module; } - public function getRef() : string + /** + * Get reference. + * + * If existing this can be a reference to another model + * + * @return string|null + * + * @since 1.0.0 + */ + public function getRef() : ?string { return $this->ref; } - public function getContent() : string + /** + * Get content. + * + * @return string|null + * + * @since 1.0.0 + */ + public function getContent() : ?string { return $this->content; } - public function getOld() : string + /** + * Get old value. + * + * @return string|null + * + * @since 1.0.0 + */ + public function getOld() : ?string { return $this->old; } - public function getNew() : string + /** + * Get new value. + * + * @return string|null + * + * @since 1.0.0 + */ + public function getNew() : ?string { return $this->new; } + /** + * Get created by. + * + * @return int|Account + * + * @since 1.0.0 + */ public function getCreatedBy() { return $this->createdBy; } + /** + * Get created at. + * + * @return \DateTime + * + * @since 1.0.0 + */ public function getCreatedAt() : \DateTime { return $this->createdAt; diff --git a/Models/AuditMapper.php b/Models/AuditMapper.php index a3a86b6..8b0655c 100644 --- a/Models/AuditMapper.php +++ b/Models/AuditMapper.php @@ -81,4 +81,28 @@ final class AuditMapper extends DataMapperAbstract * @since 1.0.0 */ protected static $createdAt = 'auditor_audit_created_at'; + + /** + * {@inheritdoc} + */ + public static function update($obj, int $relations = RelationType::ALL, int $depth = 1) + { + // prevent updating + } + + /** + * {@inheritdoc} + */ + public static function updateArray(array &$obj, int $relations = RelationType::ALL, int $depth = 1) + { + // prevent updating + } + + /** + * {@inheritdoc} + */ + public static function delete($obj, int $relations = RelationType::REFERENCE) + { + // prevent deleting + } }