Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/audit-restore #233

Merged
merged 5 commits into from
May 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## v4.0.4 (2017-05-01)
### Added
- Log the user agent string ([#224](https://github.com/owen-it/laravel-auditing/issues/224))

### Changed
- Updated migration stub to use the DB driver ([#220](https://github.com/owen-it/laravel-auditing/issues/220))

### Fixed
- Wrong class name for custom audit drivers ([#226](https://github.com/owen-it/laravel-auditing/issues/226))
- Use standards compliant SQL ([#225](https://github.com/owen-it/laravel-auditing/issues/225))
- Prevent creating an updated audit when restoring a model ([#233](https://github.com/owen-it/laravel-auditing/issues/233))

## v4.0.3 (2017-03-21)
### Added
- Changelog file
Expand Down
2 changes: 1 addition & 1 deletion src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function bootAuditable()
public function audits()
{
return $this->morphMany(AuditModel::class, 'auditable')
->groupBy('created_at')
->distinct('created_at')
->orderBy('created_at', 'DESC');
}

Expand Down
31 changes: 30 additions & 1 deletion src/AuditableObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@

class AuditableObserver
{
/**
* Is the model being restored?
*
* @var bool
*/
public static $restoring = false;

/**
* Handle the created event for the model.
*
Expand All @@ -40,7 +47,10 @@ public function created(AuditableContract $model)
*/
public function updated(AuditableContract $model)
{
Auditor::execute($model->setAuditEvent('updated'));
// Ignore the updated event when restoring
if (!static::$restoring || true) {
Auditor::execute($model->setAuditEvent('updated'));
}
}

/**
Expand All @@ -55,6 +65,21 @@ public function deleted(AuditableContract $model)
Auditor::execute($model->setAuditEvent('deleted'));
}

/**
* Handle the restoring event for the model.
*
* @param \OwenIt\Auditing\Contracts\Auditable $model
*
* @return void
*/
public function restoring(AuditableContract $model)
{
// When restoring a model, an updated event is also fired.
// By keeping track of the main event that took place,
// we avoid creating a second audit with wrong values
static::$restoring = true;
}

/**
* Handle the restored event for the model.
*
Expand All @@ -65,5 +90,9 @@ public function deleted(AuditableContract $model)
public function restored(AuditableContract $model)
{
Auditor::execute($model->setAuditEvent('restored'));

// Once the model is restored, we need to put everything back
// as before, in case an legitimate update event is fired
static::$restoring = false;
}
}
28 changes: 28 additions & 0 deletions tests/AuditableObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public function testAuditableObserverCreatedPass(AuditableObserver $observer, Au
->andReturn($model);

$observer->created($model);

$this->assertFalse($observer::$restoring);
}

/**
Expand All @@ -109,6 +111,8 @@ public function testAuditableObserverUpdatedPass(AuditableObserver $observer, Au
->andReturn($model);

$observer->updated($model);

$this->assertFalse($observer::$restoring);
}

/**
Expand All @@ -134,6 +138,28 @@ public function testAuditableObserverDeletedPass(AuditableObserver $observer, Au
->andReturn($model);

$observer->deleted($model);

$this->assertFalse($observer::$restoring);
}

/**
* Test AuditableObserver restoring method to PASS.
*
* @depends testAuditableObserverInstantiation
* @depends testAuditableMock
*
* @param AuditableObserver $observer
* @param Auditable $model
*
* @return void
*/
public function testAuditableObserverRestoringPass(AuditableObserver $observer, Auditable $model)
{
$this->assertFalse($observer::$restoring);

$observer->restoring($model);

$this->assertTrue($observer::$restoring);
}

/**
Expand All @@ -159,5 +185,7 @@ public function testAuditableObserverRestoredPass(AuditableObserver $observer, A
->andReturn($model);

$observer->restored($model);

$this->assertFalse($observer::$restoring);
}
}