Skip to content

Commit

Permalink
feat(Auditable):
Browse files Browse the repository at this point in the history
- make the User resolver more flexible
- update the auditing configuration
- remove methods and attributes that were only used once
  • Loading branch information
quetzyg committed Nov 27, 2016
1 parent 0658aab commit 46a753e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 59 deletions.
13 changes: 8 additions & 5 deletions config/auditing.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@

/*
|--------------------------------------------------------------------------
| Authentication Model
| User Model & Resolver
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
| Define the User model class and how to resolve a logged User ID.
|
*/

'model' => App\User::class,
'user' => [
'model' => App\User::class,
'resolver' => function () {
return auth()->check() ? auth()->user()->getAuthIdentifier() : null;
},
],

/*
|--------------------------------------------------------------------------
Expand Down
71 changes: 18 additions & 53 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace OwenIt\Auditing;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;
use OwenIt\Auditing\Contracts\Dispatcher;
use OwenIt\Auditing\Models\Audit as AuditModel;
use OwenIt\Auditing\Observers\Audit as AuditObserver;
use Ramsey\Uuid\Uuid;
use UnexpectedValueException;

trait Auditable
{
Expand Down Expand Up @@ -58,21 +58,6 @@ trait Auditable
*/
protected $auditEvent;

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

/**
* @var string
*/
protected $auditCurrentUrl;

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

/**
* Init auditing.
*/
Expand Down Expand Up @@ -105,7 +90,7 @@ public function prepareAudit()
$this->updatedData = $this->attributes;

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

$this->dontKeep[] = $attribute;
Expand All @@ -122,15 +107,6 @@ public function prepareAudit()
array_merge($this->keepAuditOf, $this->doKeep)
: $this->doKeep;

// Get User ID
$this->auditUserId = $this->getLoggedInUserId();

// Get current URL
$this->auditCurrentUrl = $this->getCurrentUrl();

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

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

Expand Down Expand Up @@ -207,9 +183,7 @@ public function auditDeletion()
}

/**
* Audit model.
*
* @return array
* {@inheritdoc}
*/
public function toAudit()
{
Expand All @@ -220,9 +194,9 @@ public function toAudit()
'event' => $this->auditEvent,
'auditable_id' => $this->getKey(),
'auditable_type' => $this->getMorphClass(),
'user_id' => $this->auditUserId,
'url' => $this->auditCurrentUrl,
'ip_address' => $this->auditIpAddress,
'user_id' => $this->resolveUserId(),
'url' => $this->resolveUrl(),
'ip_address' => Request::ip(),
'created_at' => $this->freshTimestamp(),
]);
}
Expand All @@ -236,27 +210,28 @@ public function transformAudit(array $data)
}

/**
* Get user id.
* Resolve the ID of the logged User
*
* @return null
* @throws UnexpectedValueException
* @return mixed|null
*/
protected function getLoggedInUserId()
protected function resolveUserId()
{
try {
if (Auth::check()) {
return Auth::user()->getAuthIdentifier();
}
} catch (\Exception $e) {
return;
$resolver = Config::get('auditing.user.resolver');

if (!is_callable($resolver)) {
throw new UnexpectedValueException('Invalid User resolver, expecting callable');
}

return $resolver();
}

/**
* Get the current request's route if available.
* Resolve the current request URL if available.
*
* @return string
*/
protected function getCurrentUrl()
protected function resolveUrl()
{
if (App::runningInConsole()) {
return 'console';
Expand All @@ -265,16 +240,6 @@ protected function getCurrentUrl()
return Request::fullUrl();
}

/**
* Get IP Address.
*
* @return mixed
*/
public function getIpAddress()
{
return Request::ip();
}

/**
* Fields Changed.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function auditable()
*/
public function user()
{
return $this->belongsTo(Config::get('auditing.model'));
return $this->belongsTo(Config::get('auditing.user.model'));
}

/**
Expand Down

0 comments on commit 46a753e

Please sign in to comment.