From dcf05b2249d0437d723dc758b92b50f4c38c8e46 Mon Sep 17 00:00:00 2001 From: Ryan MacKenzie Date: Wed, 16 Apr 2025 16:39:34 -0400 Subject: [PATCH 1/6] feat: ip tracking --- .../Revisionable/Revisionable.php | 15 +++++---- .../Revisionable/RevisionableTrait.php | 33 ++++++++++--------- .../2025_04_16_203449_add_ip_field.php | 32 ++++++++++++++++++ 3 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 src/migrations/2025_04_16_203449_add_ip_field.php diff --git a/src/Venturecraft/Revisionable/Revisionable.php b/src/Venturecraft/Revisionable/Revisionable.php index 59a2bd9b..5a0f958f 100644 --- a/src/Venturecraft/Revisionable/Revisionable.php +++ b/src/Venturecraft/Revisionable/Revisionable.php @@ -83,7 +83,7 @@ public static function boot() public static function newModel() { $model = \Config::get('revisionable.model', 'Venturecraft\Revisionable\Revision'); - return new $model; + return new $model(); } /** @@ -159,6 +159,7 @@ public function postSave() 'old_value' => Arr::get($this->originalData, $key), 'new_value' => $this->updatedData[$key], 'user_id' => $this->getSystemUserId(), + 'ip' => $_SERVER["REMOTE_ADDR"], 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -179,14 +180,12 @@ public function postCreate() // Check if we should store creations in our revision history // Set this value to true in your model if you want to - if(empty($this->revisionCreationsEnabled)) - { + if (empty($this->revisionCreationsEnabled)) { // We should not store creations. return false; } - if ((!isset($this->revisionEnabled) || $this->revisionEnabled)) - { + if ((!isset($this->revisionEnabled) || $this->revisionEnabled)) { $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), @@ -194,6 +193,7 @@ public function postCreate() 'old_value' => null, 'new_value' => $this->{self::CREATED_AT}, 'user_id' => $this->getSystemUserId(), + 'ip' => $_SERVER["REMOTE_ADDR"], 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -218,6 +218,7 @@ public function postDelete() 'old_value' => null, 'new_value' => $this->{$this->getDeletedAtColumn()}, 'user_id' => $this->getSystemUserId(), + 'ip' => $_SERVER["REMOTE_ADDR"], 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -357,7 +358,7 @@ public function identifiableName() */ public function getRevisionNullString() { - return isset($this->revisionNullString)?$this->revisionNullString:'nothing'; + return isset($this->revisionNullString) ? $this->revisionNullString : 'nothing'; } /** @@ -370,7 +371,7 @@ public function getRevisionNullString() */ public function getRevisionUnknownString() { - return isset($this->revisionUnknownString)?$this->revisionUnknownString:'unknown'; + return isset($this->revisionUnknownString) ? $this->revisionUnknownString : 'unknown'; } /** diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index bbc752c6..49f50c2b 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -135,7 +135,7 @@ public function preSave() $this->updatedData[$key] = json_encode($updatedData); $this->originalData[$key] = json_encode(json_decode($this->originalData[$key], true)); - } else if (gettype($val) == 'object' && !method_exists($val, '__toString')) { + } elseif (gettype($val) == 'object' && !method_exists($val, '__toString')) { unset($this->originalData[$key]); unset($this->updatedData[$key]); array_push($this->dontKeep, $key); @@ -173,10 +173,10 @@ public function postSave() } else { $LimitReached = false; } - if (isset($this->revisionCleanup)){ - $RevisionCleanup=$this->revisionCleanup; - }else{ - $RevisionCleanup=false; + if (isset($this->revisionCleanup)) { + $RevisionCleanup = $this->revisionCleanup; + } else { + $RevisionCleanup = false; } // check if the model already exists @@ -195,15 +195,16 @@ public function postSave() 'old_value' => Arr::get($this->originalData, $key), 'new_value' => $this->updatedData[$key], 'user_id' => $this->getSystemUserId(), + 'ip' => $_SERVER["REMOTE_ADDR"], 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); } if (count($revisions) > 0) { - if($LimitReached && $RevisionCleanup){ - $toDelete = $this->revisionHistory()->orderBy('id','asc')->limit(count($revisions))->get(); - foreach($toDelete as $delete){ + if ($LimitReached && $RevisionCleanup) { + $toDelete = $this->revisionHistory()->orderBy('id', 'asc')->limit(count($revisions))->get(); + foreach ($toDelete as $delete) { $delete->delete(); } } @@ -222,14 +223,12 @@ public function postCreate() // Check if we should store creations in our revision history // Set this value to true in your model if you want to - if(empty($this->revisionCreationsEnabled)) - { + if (empty($this->revisionCreationsEnabled)) { // We should not store creations. return false; } - if ((!isset($this->revisionEnabled) || $this->revisionEnabled)) - { + if ((!isset($this->revisionEnabled) || $this->revisionEnabled)) { $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), @@ -237,6 +236,7 @@ public function postCreate() 'old_value' => null, 'new_value' => $this->{self::CREATED_AT}, 'user_id' => $this->getSystemUserId(), + 'ip' => $_SERVER["REMOTE_ADDR"], 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -264,6 +264,7 @@ public function postDelete() 'old_value' => null, 'new_value' => $this->{$this->getDeletedAtColumn()}, 'user_id' => $this->getSystemUserId(), + 'ip' => $_SERVER["REMOTE_ADDR"], 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -463,10 +464,12 @@ public function disableRevisionField($field) */ private function sortJsonKeys($attribute) { - if(empty($attribute)) return $attribute; + if (empty($attribute)) { + return $attribute; + } - foreach ($attribute as $key=>$value) { - if(is_array($value) || is_object($value)){ + foreach ($attribute as $key => $value) { + if (is_array($value) || is_object($value)) { $value = $this->sortJsonKeys($value); } else { continue; diff --git a/src/migrations/2025_04_16_203449_add_ip_field.php b/src/migrations/2025_04_16_203449_add_ip_field.php new file mode 100644 index 00000000..91706c51 --- /dev/null +++ b/src/migrations/2025_04_16_203449_add_ip_field.php @@ -0,0 +1,32 @@ +string('ip')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('revisions', function ($table) { + $table->dropColumn('ip'); + }); + } +} From d504bf48fd8a1685cbf49f7eef4e9abe65d77d14 Mon Sep 17 00:00:00 2001 From: Ryan MacKenzie Date: Wed, 16 Apr 2025 17:00:35 -0400 Subject: [PATCH 2/6] feedback: rename migration --- ... => 2025_04_16_203449_add_ip_field_to_revisions_table.php} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/migrations/{2025_04_16_203449_add_ip_field.php => 2025_04_16_203449_add_ip_field_to_revisions_table.php} (84%) diff --git a/src/migrations/2025_04_16_203449_add_ip_field.php b/src/migrations/2025_04_16_203449_add_ip_field_to_revisions_table.php similarity index 84% rename from src/migrations/2025_04_16_203449_add_ip_field.php rename to src/migrations/2025_04_16_203449_add_ip_field_to_revisions_table.php index 91706c51..fb968c6f 100644 --- a/src/migrations/2025_04_16_203449_add_ip_field.php +++ b/src/migrations/2025_04_16_203449_add_ip_field_to_revisions_table.php @@ -4,7 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class AddIPField extends Migration +class AddIPFieldToRevisionsTable extends Migration { /** * Run the migrations. @@ -14,7 +14,7 @@ class AddIPField extends Migration public function up() { Schema::table('revisions', function ($table) { - $table->string('ip')->nullable(); + $table->ipAddress('ip')->nullable(); }); } From d407d0fc8042bf7731c1d3cbc2e6c3cf7a4df511 Mon Sep 17 00:00:00 2001 From: Ryan MacKenzie Date: Wed, 28 May 2025 10:07:46 -0400 Subject: [PATCH 3/6] fix: use request()->getClientIp() --- src/Venturecraft/Revisionable/Revisionable.php | 6 +++--- src/Venturecraft/Revisionable/RevisionableTrait.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Venturecraft/Revisionable/Revisionable.php b/src/Venturecraft/Revisionable/Revisionable.php index 5a0f958f..cf3911ff 100644 --- a/src/Venturecraft/Revisionable/Revisionable.php +++ b/src/Venturecraft/Revisionable/Revisionable.php @@ -159,7 +159,7 @@ public function postSave() 'old_value' => Arr::get($this->originalData, $key), 'new_value' => $this->updatedData[$key], 'user_id' => $this->getSystemUserId(), - 'ip' => $_SERVER["REMOTE_ADDR"], + 'ip' => request()->getClientIp(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -193,7 +193,7 @@ public function postCreate() 'old_value' => null, 'new_value' => $this->{self::CREATED_AT}, 'user_id' => $this->getSystemUserId(), - 'ip' => $_SERVER["REMOTE_ADDR"], + 'ip' => request()->getClientIp(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -218,7 +218,7 @@ public function postDelete() 'old_value' => null, 'new_value' => $this->{$this->getDeletedAtColumn()}, 'user_id' => $this->getSystemUserId(), - 'ip' => $_SERVER["REMOTE_ADDR"], + 'ip' => request()->getClientIp(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index 49f50c2b..db736caa 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -195,7 +195,7 @@ public function postSave() 'old_value' => Arr::get($this->originalData, $key), 'new_value' => $this->updatedData[$key], 'user_id' => $this->getSystemUserId(), - 'ip' => $_SERVER["REMOTE_ADDR"], + 'ip' => request()->getClientIp(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -236,7 +236,7 @@ public function postCreate() 'old_value' => null, 'new_value' => $this->{self::CREATED_AT}, 'user_id' => $this->getSystemUserId(), - 'ip' => $_SERVER["REMOTE_ADDR"], + 'ip' => request()->getClientIp(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -264,7 +264,7 @@ public function postDelete() 'old_value' => null, 'new_value' => $this->{$this->getDeletedAtColumn()}, 'user_id' => $this->getSystemUserId(), - 'ip' => $_SERVER["REMOTE_ADDR"], + 'ip' => request()->getClientIp(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); From f659f2517449796a1e70731738503d8d47450934 Mon Sep 17 00:00:00 2001 From: Ryan MacKenzie Date: Wed, 28 May 2025 10:09:06 -0400 Subject: [PATCH 4/6] fix: whoops forgot to pull --- .../2025_04_16_203449_add_ip_field_to_revisions_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/migrations/2025_04_16_203449_add_ip_field_to_revisions_table.php b/src/migrations/2025_04_16_203449_add_ip_field_to_revisions_table.php index fb968c6f..b72b220e 100644 --- a/src/migrations/2025_04_16_203449_add_ip_field_to_revisions_table.php +++ b/src/migrations/2025_04_16_203449_add_ip_field_to_revisions_table.php @@ -14,7 +14,7 @@ class AddIPFieldToRevisionsTable extends Migration public function up() { Schema::table('revisions', function ($table) { - $table->ipAddress('ip')->nullable(); + $table->ipAddress('ip')->nullable()->after('new_value'); }); } From 86ca025da72044e44db98021032e0cd7719b6ce2 Mon Sep 17 00:00:00 2001 From: Tymotheos Szulc Date: Wed, 12 Nov 2025 11:32:33 -0500 Subject: [PATCH 5/6] refactor: reduce code duplication https://github.com/RoomRoster/revisionable/pull/3/files#r2487698480 --- src/Venturecraft/Revisionable/RevisionableTrait.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index 8f676861..927ace0e 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -197,7 +197,6 @@ public function postSave() 'old_value' => Arr::get($this->originalData, $key), 'new_value' => $this->updatedData[$key], 'user_id' => $this->getSystemUserId(), - 'ip' => request()->getClientIp(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -240,7 +239,6 @@ public function postCreate() 'old_value' => null, 'new_value' => $this->{self::CREATED_AT}, 'user_id' => $this->getSystemUserId(), - 'ip' => request()->getClientIp(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -272,7 +270,6 @@ public function postDelete() 'old_value' => null, 'new_value' => $this->{$this->getDeletedAtColumn()}, 'user_id' => $this->getSystemUserId(), - 'ip' => request()->getClientIp(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -344,7 +341,10 @@ public function getSystemUserId() public function getAdditionalFields() { - $additional = []; + $additional = [ + 'ip' => request()->getClientIp(), + ]; + //Determine if there are any additional fields we'd like to add to our model contained in the config file, and //get them into an array. $fields = config('revisionable.additional_fields', []); From 63c8e467025483edb9c3b973701078249b64f624 Mon Sep 17 00:00:00 2001 From: Tymotheos Szulc Date: Wed, 12 Nov 2025 11:53:33 -0500 Subject: [PATCH 6/6] style: cleanup & spacing --- src/Venturecraft/Revisionable/Revisionable.php | 8 +++++--- .../Revisionable/RevisionableTrait.php | 14 +++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Venturecraft/Revisionable/Revisionable.php b/src/Venturecraft/Revisionable/Revisionable.php index a51b4d94..693e4b55 100644 --- a/src/Venturecraft/Revisionable/Revisionable.php +++ b/src/Venturecraft/Revisionable/Revisionable.php @@ -77,6 +77,7 @@ public static function boot() $model->postForceDelete(); }); } + /** * Instance the revision model * @return \Illuminate\Database\Eloquent\Model @@ -179,8 +180,8 @@ public function postSave() } /** - * Called after record successfully created - */ + * Called after record successfully created + */ public function postCreate() { @@ -254,6 +255,7 @@ public function postForceDelete() 'old_value' => $this->{self::CREATED_AT}, 'new_value' => null, 'user_id' => $this->getSystemUserId(), + 'ip' => request()->getClientIp(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); @@ -267,7 +269,7 @@ public function postForceDelete() /** * Attempt to find the user id of the currently logged in user * Supports Cartalyst Sentry/Sentinel based authentication, as well as stock Auth - **/ + */ private function getSystemUserId() { try { diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index 927ace0e..e3b298b3 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -113,10 +113,10 @@ public static function classRevisionHistory($limit = 100, $order = 'desc') } /** - * Invoked before a model is saved. Return false to abort the operation. - * - * @return bool - */ + * Invoked before a model is saved. Return false to abort the operation. + * + * @return bool + */ public function preSave() { if (!isset($this->revisionEnabled) || $this->revisionEnabled) { @@ -219,8 +219,8 @@ public function postSave() } /** - * Called after record successfully created - */ + * Called after record successfully created + */ public function postCreate() { @@ -317,7 +317,7 @@ public function postForceDelete() /** * Attempt to find the user id of the currently logged in user * Supports Cartalyst Sentry/Sentinel based authentication, as well as stock Auth - **/ + */ public function getSystemUserId() { try {