diff --git a/src/Venturecraft/Revisionable/Revisionable.php b/src/Venturecraft/Revisionable/Revisionable.php index 62942e06..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 @@ -165,6 +166,7 @@ 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(), ); @@ -178,21 +180,19 @@ public function postSave() } /** - * Called after record successfully created - */ + * Called after record successfully created + */ 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(), @@ -200,6 +200,7 @@ 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(), ); @@ -224,6 +225,7 @@ 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(), ); @@ -253,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(), ); @@ -266,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 { @@ -396,7 +399,7 @@ public function identifiableName() */ public function getRevisionNullString() { - return isset($this->revisionNullString)?$this->revisionNullString:'nothing'; + return isset($this->revisionNullString) ? $this->revisionNullString : 'nothing'; } /** @@ -409,7 +412,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 3b5db206..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) { @@ -137,7 +137,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); @@ -175,10 +175,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 @@ -205,9 +205,9 @@ public function postSave() } 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(); } } @@ -219,21 +219,19 @@ public function postSave() } /** - * Called after record successfully created - */ + * Called after record successfully created + */ 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(), @@ -319,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 { @@ -343,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', []); @@ -524,10 +525,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_to_revisions_table.php b/src/migrations/2025_04_16_203449_add_ip_field_to_revisions_table.php new file mode 100644 index 00000000..b72b220e --- /dev/null +++ b/src/migrations/2025_04_16_203449_add_ip_field_to_revisions_table.php @@ -0,0 +1,32 @@ +ipAddress('ip')->nullable()->after('new_value'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('revisions', function ($table) { + $table->dropColumn('ip'); + }); + } +}