Skip to content

Commit

Permalink
Merge pull request #131 from owen-it/queue
Browse files Browse the repository at this point in the history
[3.1] 	Audit attributes corrected when queue
  • Loading branch information
anteriovieira authored Oct 19, 2016
2 parents 9e403b9 + aff298d commit 5ddc9f6
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 90 deletions.
199 changes: 109 additions & 90 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;
use Ramsey\Uuid\Uuid;

Expand Down Expand Up @@ -55,7 +54,22 @@ trait Auditable
/**
* @var string
*/
protected $typeAuditing = '';
protected $auditType = '';

/**
* @var string
*/
protected $auditUserId = '';

/**
* @var string
*/
protected $auditCurrentRoute = '';

/**
* @var string
*/
protected $auditIpAddress = '';

/**
* Init auditing.
Expand All @@ -72,37 +86,44 @@ public static function bootAuditable()
*/
public function prepareAudit()
{
// If auditing is enabled
if ($this->isAuditEnabled()) {
$this->originalData = $this->original;
$this->originalData = $this->original;

$this->updatedData = $this->attributes;
$this->updatedData = $this->attributes;

foreach ($this->updatedData as $key => $val) {
if (gettype($val) == 'object' && !method_exists($val, '__toString')) {
unset($this->originalData[$key]);
foreach ($this->updatedData as $attribute => $val) {
if (gettype($val) == 'object' && !method_exists($val, '__toString')) {
unset($this->originalData[$attribute]);

unset($this->updatedData[$key]);
unset($this->updatedData[$attribute]);

array_push($this->dontKeep, $key);
}
array_push($this->dontKeep, $attribute);
}
// Dont keep audit of
$this->dontKeep = isset($this->dontKeepAuditOf) ?
array_merge($this->dontKeepAuditOf, $this->dontKeep)
: $this->dontKeep;
}

// Keep audit of
$this->doKeep = isset($this->keepAuditOf) ?
array_merge($this->keepAuditOf, $this->doKeep)
: $this->doKeep;
// Dont keep audit of
$this->dontKeep = isset($this->dontKeepAuditOf) ?
array_merge($this->dontKeepAuditOf, $this->dontKeep)
: $this->dontKeep;

// Get changed data
$this->dirtyData = $this->getDirty();
// Keep audit of
$this->doKeep = isset($this->keepAuditOf) ?
array_merge($this->keepAuditOf, $this->doKeep)
: $this->doKeep;

// Tells whether the record exists in the database
$this->updating = $this->exists;
}
// Get user id
$this->auditUserId = $this->getLoggedInUserId();

// Get curruent route
$this->auditCurrentRoute = $this->getCurrentRoute();

// Get ip address
$this->auditIpAddress = $this->getIpAddress();

// Get changed data
$this->dirtyData = $this->getDirty();

// Tells whether the record exists in the database
$this->updating = $this->exists;
}

/**
Expand All @@ -112,14 +133,13 @@ public function prepareAudit()
*/
public function auditCreation()
{
// If auditing is enabled
// Checks if an auditable type
if ($this->isTypeAuditable('created')) {
$this->typeAuditing = 'created';

$this->newData = [];
foreach ($this->updatedData as $key => $value) {
if ($this->isAuditing($key)) {
$this->newData[$key] = $value;

foreach ($this->updatedData as $attribute => $value) {
if ($this->isAttributeAuditable($attribute)) {
$this->newData[$attribute] = $value;
}
}

Expand All @@ -134,22 +154,21 @@ public function auditCreation()
*/
public function auditUpdate()
{
// If auditing is enabled and object updated
if (($this->isTypeAuditable('saved') || $this->isTypeAuditable('updated')) && $this->updating) {
$this->typeAuditing = 'updated';

if ($this->isTypeAuditable('updated') && $this->updating) {
$changesToTecord = $this->changedAuditingFields();

if (empty($changesToTecord)) {
return;
}

$this->oldData = [];

$this->newData = [];
foreach ($changesToTecord as $key => $change) {
$this->oldData[$key] = array_get($this->originalData, $key);

$this->newData[$key] = array_get($this->updatedData, $key);
foreach ($changesToTecord as $attribute => $change) {
$this->oldData[$attribute] = array_get($this->originalData, $attribute);

$this->newData[$attribute] = array_get($this->updatedData, $attribute);
}

$this->audit();
Expand All @@ -163,13 +182,11 @@ public function auditUpdate()
*/
public function auditDeletion()
{
// If auditing is enabled
if ($this->isTypeAuditable('deleted') && $this->isAuditing('deleted_at')) {
$this->typeAuditing = 'deleted';

foreach ($this->updatedData as $key => $value) {
if ($this->isAuditing($key)) {
$this->oldData[$key] = $value;
// Checks if an auditable type
if ($this->isTypeAuditable('deleted') && $this->isAttributeAuditable('deleted_at')) {
foreach ($this->updatedData as $attribute => $value) {
if ($this->isAttributeAuditable($attribute)) {
$this->oldData[$attribute] = $value;
}
}

Expand All @@ -184,17 +201,16 @@ public function auditDeletion()
*/
public function toAudit()
{
// Auditable data
return $this->transformAudit([
'id' => (string) Uuid::uuid4(),
'old' => $this->cleanHiddenAuditAttributes($this->oldData),
'new' => $this->cleanHiddenAuditAttributes($this->newData),
'type' => $this->typeAuditing,
'type' => $this->auditType,
'auditable_id' => $this->getKey(),
'auditable_type' => $this->getMorphClass(),
'user_id' => $this->getLoggedInUserId(),
'route' => $this->getCurrentRoute(),
'ip_address' => $this->getIpAddress(),
'user_id' => $this->auditUserId,
'route' => $this->auditCurrentRoute,
'ip_address' => $this->auditIpAddress,
'created_at' => $this->freshTimestamp(),
]);
}
Expand Down Expand Up @@ -261,39 +277,39 @@ private function changedAuditingFields()
{
$changesToTecord = [];

foreach ($this->dirtyData as $key => $value) {
if ($this->isAuditing($key) && !is_array($value)) {
foreach ($this->dirtyData as $attribute => $value) {
if ($this->isAttributeAuditable($attribute) && !is_array($value)) {
// Check whether the current value is difetente the original value
if (!isset($this->originalData[$key]) ||
$this->originalData[$key] != $this->updatedData[$key]) {
$changesToTecord[$key] = $value;
if (!isset($this->originalData[$attribute]) ||
$this->originalData[$attribute] != $this->updatedData[$attribute]) {
$changesToTecord[$attribute] = $value;
}
} else {
unset($this->updatedData[$key]);
unset($this->updatedData[$attribute]);

unset($this->originalData[$key]);
unset($this->originalData[$attribute]);
}
}

return $changesToTecord;
}

/**
* Is Auditing?
* Determine whether a attribute is auditable for audit manipulation.
*
* @param $key
* @param $attribute
*
* @return bool
*/
private function isAuditing($key)
private function isAttributeAuditable($attribute)
{
// Checks if the field is in the collection of auditable
if (isset($this->doKeep) && in_array($key, $this->doKeep)) {
if (isset($this->doKeep) && in_array($attribute, $this->doKeep)) {
return true;
}

// Checks if the field is in the collection of non-auditable
if (isset($this->dontKeep) && in_array($key, $this->dontKeep)) {
if (isset($this->dontKeep) && in_array($attribute, $this->dontKeep)) {
return false;
}

Expand All @@ -302,46 +318,49 @@ private function isAuditing($key)
}

/**
* Is Auditing enabled?
* Determine whether a type is auditable.
*
* @param string $type
*
* @return bool
*/
private function isAuditEnabled()
public function isTypeAuditable($type)
{
// Check that the model has audit enabled and also check that we aren't
// running in cosole or that we want to log console too.
if ((!isset($this->auditEnabled) || $this->auditEnabled)
&& (!App::runningInConsole() || Config::get('auditing.audit_console'))) {
// Checks if the type is in the collection of type auditable
if (in_array($type, $this->getAuditableTypes())) {
$this->setAuditType($type);

return true;
}

return false;
}

/**
* Verify is type auditable.
*
* @param $key
* Set audit type.
*
* @return bool
* @param string $type;
*/
public function isTypeAuditable($key)
public function setAuditType($type)
{
// Verify if auditing enabled
if (!$this->isAuditEnabled()) {
return false;
}

// Get the auditable types
$auditableTypes = isset($this->auditableTypes) ? $this->auditableTypes
: ['created', 'updated', 'deleted', 'saved', 'restored'];
$this->auditType = $type;
}

// Checks if the type is in the collection of type auditable
if (in_array($key, $auditableTypes)) {
return true;
/**
* Get the auditable types.
*
* @return array
*/
public function getAuditableTypes()
{
if (isset($this->auditableTypes)) {
return $this->auditableTypes;
}

return false;
return [
'created', 'updated', 'deleted',
'saved', 'restored',
];
}

/**
Expand Down Expand Up @@ -371,8 +390,8 @@ public function cleanHiddenAuditAttributes(array $attributes)

// If visible is set, set to null any attributes which are not in visible
if (count($visible) > 0) {
foreach ($attributes as $key => &$value) {
if (!in_array($key, $visible)) {
foreach ($attributes as $attribute => &$value) {
if (!in_array($attribute, $visible)) {
$value = null;
}
}
Expand All @@ -382,9 +401,9 @@ public function cleanHiddenAuditAttributes(array $attributes)

// If hidden is set, set to null any attributes which are in hidden
if (count($hidden) > 0) {
foreach ($hidden as $key) {
if (array_key_exists($key, $attributes)) {
$attributes[$key] = null;
foreach ($hidden as $attribute) {
if (array_key_exists($attribute, $attributes)) {
$attributes[$attribute] = null;
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ public function testWithAuditRespectsWithHidden()
$this->assertEquals(['name' => 'Anterio', 'password' => null], $result);
}

public function testItGetAuditableTypes()
{
$model1 = new ModelAuditableTestConfigs();

$types = [
'created', 'updated', 'deleted',
'saved', 'restored',
];

$this->assertEquals($types, $model1->getAuditableTypes());

$model2 = new ModelAuditableTestCustomsValues();

$this->assertEquals(['created'], $model2->getAuditableTypes());
}

public function testItIsTypeAuditable()
{
$model = new ModelAuditableTestRaw();

$this->assertTrue($model->isTypeAuditable('created'));
$this->assertFalse($model->isTypeAuditable('foo'));
}

public function testItGetsLogCustomMessage()
{
$logCustomMessage = ModelAuditableTestCustomsValues::$logCustomMessage;
Expand All @@ -63,6 +87,8 @@ class ModelAuditableTestCustomsValues extends Model

protected $auditRespectsHidden = true;

protected $auditableTypes = ['created'];

public static $logCustomMessage = '{user.name} {type} a post {elapsed_time}';
}

Expand Down

0 comments on commit 5ddc9f6

Please sign in to comment.