Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

[V7] Updates for next version #62

Merged
merged 1 commit into from
May 11, 2018
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
31 changes: 12 additions & 19 deletions audit-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,43 @@ While the package comes with a pretty standard migration file which covers most

With that in mind, here are a few tweaks that can be performed.

## User ID foreign key relation
Some developers like to have the relation between tables properly set, by enforcing foreign keys.
At the end of the **up()** method, add the following:

```php
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade')
->onUpdate('cascade');
```

## Using a different column name for the User ID
## Using a different column name for the User ID/Type
Instead of the typical `user_id` column, a different name can be used:

```php
$table->unsignedInteger('owner_id')->nullable();
$table->nullableMorphs('owner');
```

Just make sure the `foreign_key` value in the configuration is also updated, to reflect the change:
Just make sure the `morph_prefix` value in the configuration is also updated, to reflect the change:

```php
return [
'user' = [
'foreign_key' => 'owner_id',
'morph_prefix' => 'owner',
],
];
```

> {tip} Read more about this and the `User` model ID in the [General Configuration](general-configuration) section.
> {tip} Read more about this in the [General Configuration](general-configuration) section.

## UUID over auto-incrementing ids
Some developers prefer to use a [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) instead of auto-incrementing ids.
If that's the case, make sure to update the **up()** method like so:

For the `User`, change from
```php
$table->unsignedInteger('user_id')->nullable();
$table->nullableMorphs('user');
```

to

```php
$table->uuid('user_id')->nullable();
$table->string('user_type')->nullable();
$table->index([
'auditable_id',
'auditable_type',
]);
```

For the `Auditable` model, change from
Expand All @@ -65,7 +58,7 @@ $table->index([
]);
```

> {note} Make sure the `user_id` and/or `auditable_id` column types match the ones used in their respective tables.
> {note} Make sure the `user_*` and/or `auditable_*` column types match the ones used in their respective tables.

## URL/User Agent values with more than 255 characters
Sometimes, the URL and/or User Agent being audited may be longer than 255 characters, so the corresponding columns should be updated from `string`
Expand Down
22 changes: 11 additions & 11 deletions audit-presentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@ array(11) {
["audit_user_agent"]=>
string(68) "Mozilla/5.0 (X11; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
["audit_tags"]=>
array(2) {
[0] =>
string(3) "foo"
[1] =>
string(3) "bar"
}
string(7) "foo,bar"
["audit_created_at"]=>
string(19) "2017-01-01 01:02:03"
["audit_updated_at"]=>
string(19) "2017-01-01 01:02:03"
["user_id"]=>
string(1) "1"
["user_id"]=>
string(8) "App\User"
["user_email"]=>
string(15) "[email protected]"
["user_name"]=>
Expand All @@ -59,13 +56,11 @@ echo $audit->getMetadata(true, JSON_PRETTY_PRINT);
"audit_url": "http:\/\/example.com\/articles\/1",
"audit_ip_address": "127.0.0.1",
"audit_user_agent": "Mozilla/5.0 (X11; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0",
"audit_tags": [
"foo",
"bar"
],
"audit_tags": "foo,bar",
"audit_created_at": "2017-01-01 01:02:03",
"audit_updated_at": "2017-01-01 01:02:03",
"user_id": "1",
"user_type": "App\\User",
"user_email":"[email protected]",
"user_name":"Bob"
}
Expand All @@ -81,10 +76,12 @@ echo $audit->event.PHP_EOL;
echo $audit->url.PHP_EOL;
echo $audit->ip_address.PHP_EOL;
echo $audit->user_agent.PHP_EOL;
echo implode(',', $audit->tags).PHP_EOL;
echo $audit->tags.PHP_EOL;
echo implode(',', $audit->getTags()).PHP_EOL;
echo $audit->created_at->toDateTimeString().PHP_EOL;
echo $audit->updated_at->toDateTimeString().PHP_EOL;
echo $audit->user_id.PHP_EOL;
echo $audit->user_type.PHP_EOL;
echo $audit->user->email.PHP_EOL;
echo $audit->user->name.PHP_EOL;
```
Expand All @@ -97,8 +94,11 @@ http://example.com/articles/1
127.0.0.1
Mozilla/5.0 (X11; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0
foo,bar
foo,bar
2017-01-01 01:02:03
2017-01-01 01:02:03
1
App\User
[email protected]
Bob
```
Expand Down
4 changes: 2 additions & 2 deletions audit-resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ return [
## User Resolver
Out of the box, this resolver uses the Laravel `Auth` facade.

The `resolve()` method must return the **ID** of the currently logged `User`, or `null` if the `User` cannot be resolved.
The `resolve()` method must return the `Model` instance of the currently logged user, or `null` if the user cannot be resolved.

When using other authentication mechanisms like [Sentinel](https://github.com/cartalyst/sentinel), a different resolver must be implemented.

Expand All @@ -153,7 +153,7 @@ class UserResolver implements \OwenIt\Auditing\Contracts\UserResolver
*/
public static function resolve()
{
return Sentinel::check() ? Sentinel::getUser()->getUserId() : null;
return Sentinel::check() ? Sentinel::getUser() : null;
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion auditor.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ class ArticleController extends Illuminate\Routing\Controller
}
```

> {note} Business logic shouldn't go into a Controller! These are _just_ examples on how to manually call the `Auditor`. Also keep in mind that, unless you set the model's `$auditEvents` property to an empty `array`, you'll get duplicate `Audit` records this way.
> {note} These are _just_ examples on how to manually call the `Auditor`. Also keep in mind that, unless you set the model's `$auditEvents` property to an empty `array`, you'll get duplicate `Audit` records this way.
25 changes: 14 additions & 11 deletions general-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,40 @@ return [

Read more about it in the [Audit Implementation](audit-implementation) section.

## User Keys & Model
Specify the `User` keys and model.
## User
Version **7.0.0** brings significant changes to the `User` configuration.
The `primary_key`, `foreign_key` and `model` are no longer necessary, giving way to `morph_prefix` and `guards`.

### Primary and Foreign Keys
Sometimes, for whatever reason (legacy database, etc), a different key needs to be used.
The default primary and foreign key values are `id` and `user_id`, respectively.
### Morph prefix
The `User` relation has been modified from `BelongsTo` to `MorphTo`, allowing for multiple user types.
By default, the column names used are `user_id` and `user_type`. For different column names, change the prefix value and update the [migration](audit-migration) accordingly.

```php
return [
// ...

'user' = [
'primary_key' => 'id',
'foreign_key' => 'user_id',
'morph_prefix' => 'user',
],

// ...
];
```

> {note} The `Audit` **resolveData()** method will still use `user_id` and other `user_` prefixed keys for any `User` data, regardless of the foreign key that was set.
> {note} The `Audit` **resolveData()** method will still use `user_id` and other `user_` prefixed keys for any `User` data, regardless of the morph prefix set.

### Model
The model value should be set to the [FQCN](http://php.net/manual/en/language.namespaces.rules.php) of the `User` class used by the application.
### Auth Guards
Specify which authentication guards the `UserResolver` should check in order to resolve a `User`.

```php
return [
// ...

'user' = [
'model' => App\User::class,
'guards' => [
'web',
'api',
],
],

// ...
Expand Down
13 changes: 5 additions & 8 deletions getting-audits.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $audit = $article->audits()->find(4);
$all = $article->audits()->with('user')->get();
```

> {tip} Remember to properly set the `User` **keys**, **model** and **resolver** in the `config/audit.php` file.
> {tip} Remember to properly set the `User` **morph_prefix** and **guards** in the `config/audit.php` file.

## Getting the Audit metadata
Retrieve an `array` with the `Audit` metadata.
Expand All @@ -44,7 +44,7 @@ var_dump($audit->getMetadata());

**Output:**
```php
array(9) {
array(10) {
["audit_id"]=>
string(1) "1"
["audit_event"]=>
Expand All @@ -56,18 +56,15 @@ array(9) {
["audit_user_agent"]=>
string(68) "Mozilla/5.0 (X11; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
["audit_tags"]=>
array(2) {
[0] =>
string(3) "foo"
[1] =>
string(3) "bar"
}
string(7) "foo,bar"
["audit_created_at"]=>
string(19) "2017-01-01 01:02:03"
["audit_updated_at"]=>
string(19) "2017-01-01 01:02:03"
["user_id"]=>
string(1) "1"
["user_type"]=>
string(8) "App\User"
}
```

Expand Down
8 changes: 3 additions & 5 deletions installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ composer require owen-it/laravel-auditing
# Upgrading
If you're [upgrading](upgrading), make sure the `audits` table schema is up to date!

>{tip} Changes to the table schema are needed when upgrading from versions **2.4.x**, **3.1.x** or **4.1.x**!
>{tip} Changes to the table schema are needed when upgrading from any of the previous versions!

# Configuration
The Laravel and Lumen configurations vary slightly, so here are the instructions for each of the frameworks.
Expand All @@ -29,7 +29,7 @@ Edit the `config/app.php` file and add the following line to register the servic
],
```

> {tip} If you're on version **5.5** or higher, you can skip this part of the setup in favour of Laravel's Auto-Discovery feature.
> {tip} If you're on Laravel version **5.5** or higher, you can skip this part of the setup in favour of the Auto-Discovery feature.

## Lumen
Edit the `bootstrap/app.php` file and add the following line to register the service provider:
Expand Down Expand Up @@ -98,6 +98,4 @@ php artisan migrate
This is where the `Audit` records will be stored, by default.

# Resolvers
Version **6.0.0** brings more resolvers, which can be replaced with custom ones.

Find out more about them in the [Audit Resolvers](audit-resolvers) section!
Find out more about resolvers in the [Audit Resolvers](audit-resolvers) section!
10 changes: 5 additions & 5 deletions troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ Please refer to the [General Configuration](general-configuration) for more info

## Attributes are considered modified, when they're not
False positives cause Audit records to be created.
This happens when model attributes are updated with **BOOLEAN** and/or **DATE** values.
This happens when a model with boolean/date attributes gets updated, regardless of change in those attributes.

{tip} This behaviour has been [fixed](https://github.com/laravel/framework/pull/18400) in Laravel 5.5+, but it's still present in older versions.

The internal data of the Eloquent model will be as follows:

Expand All @@ -42,11 +44,9 @@ In the **$original** array attribute:

This makes the `getDirty()` and `isDirty()` methods to consider wrongful attribute changes when comparing data.

According to a [maintainer](https://github.com/laravel/framework/issues/16823#issuecomment-267573840), this is an **expected behaviour**.

As a workaround, consider passing `1` and `0`, instead of `true` and `false`. For date values, append ` 00:00:00`.
{tip} For Laravel versions prior to 5.5, use this [trait](https://gist.github.com/crashkonijn/7d581e55770d2379494067d8b0ce0f6d), courtesy of [Peter Klooster](https://github.com/crashkonijn)!

At the time of writing, [this](https://github.com/laravel/internals/issues/349) is the only open issue about this subject.
Other discussions about this [subject](https://github.com/laravel/internals/issues/349).

## Argument 1 passed to Illuminate\Database\Eloquent\Model::serializeDate() must implement interface DateTimeInterface, null given
This might happen in version **4.1.x**, because the `updated_at` column values in the `audits` table are set to `NULL`.
Expand Down
Loading