diff --git a/src/app/EventLog.php b/src/app/EventLog.php new file mode 100644 --- /dev/null +++ b/src/app/EventLog.php @@ -0,0 +1,96 @@ + The attributes that are mass assignable */ + protected $fillable = [ + 'comment', + // extra event info (json) + 'data', + 'type', + // user, domain, etc. + 'object_id', + 'object_type', + // actor, if any + 'user_email', + ]; + + /** @var array Casts properties as type */ + protected $casts = [ + 'created_at' => 'datetime:Y-m-d H:i:s', + 'data' => 'array', + 'type' => 'integer', + ]; + + /** @var array The attributes that can be not set */ + protected $nullable = ['comment', 'data', 'user_email']; + + /** @var string Database table name */ + protected $table = 'eventlog'; + + + /** + * Principally an object such as Domain, User, Group. + * Note that it may be trashed (soft-deleted). + * + * @return mixed + */ + public function object() + { + return $this->morphTo()->withTrashed(); // @phpstan-ignore-line + } + + /** + * Get a string for use in translation tables derived from the object type. + * + * @return ?string Lower-case object type, e.g. user, domain, etc. + */ + private function objectTypeToLabelString(): ?string + { + if (empty($this->object_type)) { + return null; + } + + return strtolower(class_basename($this->object_type)); + } + + /** + * Event type mutator + * + * @throws \Exception + */ + public function setTypeAttribute($type) + { + $type = (int) $type; + + if ($type < 0 || $type > 255) { + throw new \Exception("Expecting an event type between 0 and 255"); + } + + $this->attributes['type'] = $type; + } +} diff --git a/src/app/Observers/EventLogObserver.php b/src/app/Observers/EventLogObserver.php new file mode 100644 --- /dev/null +++ b/src/app/Observers/EventLogObserver.php @@ -0,0 +1,24 @@ +user_email)) { + $eventlog->user_email = \App\Utils::userEmailOrNull(); + } + + if (empty($eventlog->event)) { + throw new \Exception("Unset event type"); + } + } +} diff --git a/src/app/PackageSku.php b/src/app/PackageSku.php --- a/src/app/PackageSku.php +++ b/src/app/PackageSku.php @@ -37,9 +37,6 @@ 'cost', ]; - /** @var string Database table name */ - protected $table = 'package_skus'; - /** @var bool Indicates if the model should be timestamped. */ public $timestamps = false; diff --git a/src/app/Providers/AppServiceProvider.php b/src/app/Providers/AppServiceProvider.php --- a/src/app/Providers/AppServiceProvider.php +++ b/src/app/Providers/AppServiceProvider.php @@ -69,6 +69,7 @@ { \App\Domain::observe(\App\Observers\DomainObserver::class); \App\Entitlement::observe(\App\Observers\EntitlementObserver::class); + \App\EventLog::observe(\App\Observers\EventLogObserver::class); \App\Group::observe(\App\Observers\GroupObserver::class); \App\GroupSetting::observe(\App\Observers\GroupSettingObserver::class); \App\Meet\Room::observe(\App\Observers\Meet\RoomObserver::class); diff --git a/src/database/migrations/2023_06_06_100000_create_eventlog_table.php b/src/database/migrations/2023_06_06_100000_create_eventlog_table.php new file mode 100644 --- /dev/null +++ b/src/database/migrations/2023_06_06_100000_create_eventlog_table.php @@ -0,0 +1,43 @@ +string('id', 36)->primary(); + $table->string('object_id', 36); + $table->string('object_type', 36); + $table->tinyInteger('type')->unsigned(); + $table->string('user_email')->nullable(); + $table->string('comment', 1024)->nullable(); + $table->text('data')->nullable(); // json + $table->timestamp('created_at')->useCurrent(); + + $table->index(['object_id', 'object_type', 'type']); + $table->index('created_at'); + } + ); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('eventlog'); + } +};