Skip to content

Commit

Permalink
Merge pull request #259 from quetzyg/master
Browse files Browse the repository at this point in the history
Fast forward the master branch and initial 4.1 features/fixes
  • Loading branch information
quetzyg authored Jul 2, 2017
2 parents dca1572 + 74aadaf commit bf5dbdb
Show file tree
Hide file tree
Showing 30 changed files with 1,175 additions and 332 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
tests export-ignore
.gitattributes export-ignore
.github export-ignore
.gitignore export-ignore
.travis.yml export-ignore
CHANGELOG.md export-ignore
phpunit.xml export-ignore
README.md export-ignore
29 changes: 29 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
| Q | A
| ----------------- | ---
| Bug? | no|yes
| New Feature? | no|yes
| Framework | Laravel|Lumen
| Framework version | 5.x.y
| Package version | 4.x.y
| PHP version | 5.x.y|7.x.y

#### Actual Behaviour

Describe the behaviour you're experiencing.


#### Expected Behaviour

Describe the behaviour you're expecting.


#### Steps to Reproduce

List all the steps needed to reproduce the issue. Make sure to include the code you're using,
any screenshots and/or other resources you find pertinent.


#### Possible Solutions

If you have any ideas on how to solve the issue, add them here.
If not, omit this part.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist: trusty

language: php

php:
Expand Down
72 changes: 72 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
## v4.1.0 (2017-07-??)
### Added
- Implemented Audit contract, enabling classes to extend other model types ([#211](https://github.com/owen-it/laravel-auditing/issues/211))

### Fixed
- Allow the User primary and foreign key to be specified in the configuration ([#251](https://github.com/owen-it/laravel-auditing/issues/251))

## v4.0.7 (2017-06-04)
### Added
- GitHub issue template file

### Fixed
- Properly fixed issue ([#233](https://github.com/owen-it/laravel-auditing/issues/233))

## v4.0.6 (2017-05-21)
### Fixed
- Calling a member function on null ([#244](https://github.com/owen-it/laravel-auditing/issues/244))

## v4.0.5 (2017-05-03)
### Fixed
- Removed problematic ORDER BY from the audits() relation method in the Auditable trait

## 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

### Fixed
- Removal count in Database driver ([#215](https://github.com/owen-it/laravel-auditing/issues/215))

## v4.0.2 (2017-03-18)
### Added
- `OwenIt\Auditing\Contracts\UserResolver` interface
- More `Auditable` tests

### Fixed
- Non auditable events cause a `RuntimeException` to be thrown ([#212](https://github.com/owen-it/laravel-auditing/issues/212))
- `Callable` values prevent the configuration from being cached ([#213](https://github.com/owen-it/laravel-auditing/issues/213))

## v4.0.1 (2017-03-15)
### Added
- Dynamic attribute getters
- More `Auditable` tests

### Fixed
- Trait attributes can't be overridden by class implementing `Auditable` ([#205](https://github.com/owen-it/laravel-auditing/issues/205))
- Branch alias

## v4.0.0 (2017-03-11)
### Changed
- Cleaner codebase
- Better test coverage
- `Auditable` attribute mutators and casts will be honoured

### Fixed
- Only modified attributes are stored in the `Audit`
- Lumen support

### Removed
- Queue support
- `Auditable` model custom messages/fields
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<a href="https://packagist.org/packages/owen-it/laravel-auditing"><img src="https://poser.pugx.org/owen-it/laravel-auditing/v/unstable.svg" alt="Unstable Version"></a>
<a href="http://laravel.com"><img src="https://img.shields.io/badge/Laravel-5.2.x-brightgreen.svg?style=flat-square" alt="Laravel 5.2.x"></a>
<a href="http://laravel.com"><img src="https://img.shields.io/badge/Laravel-5.3.x-brightgreen.svg?style=flat-square" alt="Laravel 5.3.x"></a>
<a href="http://laravel.com"><img src="https://img.shields.io/badge/Laravel-5.4.x-brightgreen.svg?style=flat-square" alt="Laravel 5.4.x"></a>
<a href="https://packagist.org/packages/owen-it/laravel-auditing"><img src="https://poser.pugx.org/owen-it/laravel-auditing/d/total.svg" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/owen-it/laravel-auditing"><img src="https://poser.pugx.org/owen-it/laravel-auditing/license.svg" alt="License"></a>

Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "owen-it/laravel-auditing",
"description": "Audit changes of your Eloquent models in Laravel/Lumen",
"keywords": [
"activity",
"accountability",
"audit",
"auditing",
"change",
"changes",
"eloquent",
"history",
"log",
Expand All @@ -14,7 +14,8 @@
"laravel",
"lumen",
"record",
"track"
"revision",
"tracking"
],
"homepage": "https://laravel-auditing.com",
"type": "package",
Expand Down Expand Up @@ -63,7 +64,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
"dev-master": "4.1-dev"
}
}
}
11 changes: 7 additions & 4 deletions config/audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@

/*
|--------------------------------------------------------------------------
| User Model & Resolver
| User Keys, Model & Resolver
|--------------------------------------------------------------------------
|
| Define the User model class and how to resolve a logged User ID.
| Define the User primary and foreign keys, Eloquent model and ID resolver
| class.
|
*/

'user' => [
'model' => App\User::class,
'resolver' => function () {
'primary_key' => 'id',
'foreign_key' => 'user_id',
'model' => App\User::class,
'resolver' => function () {
return Auth::check() ? Auth::user()->getAuthIdentifier() : null;
},
],
Expand Down
181 changes: 181 additions & 0 deletions src/Audit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php
/**
* This file is part of the Laravel Auditing package.
*
* @author Antério Vieira <[email protected]>
* @author Quetzy Garcia <[email protected]>
* @author Raphael França <[email protected]>
* @copyright 2015-2017
*
* For the full copyright and license information,
* please view the LICENSE.md file that was distributed
* with this source code.
*/

namespace OwenIt\Auditing;

use Illuminate\Support\Facades\Config;

trait Audit
{
/**
* Audit data.
*
* @var array
*/
protected $data = [];

/**
* The Audit attributes that belong to the metadata.
*
* @var array
*/
protected $metadata = [];

/**
* The Auditable attributes that were modified.
*
* @var array
*/
protected $modified = [];

/**
* {@inheritdoc}
*/
public function getConnection()
{
return static::resolveConnection(Config::get('audit.drivers.database.connection'));
}

/**
* {@inheritdoc}
*/
public function getTable()
{
return Config::get('audit.drivers.database.table', parent::getTable());
}

/**
* {@inheritdoc}
*/
public function auditable()
{
return $this->morphTo();
}

/**
* {@inheritdoc}
*/
public function user()
{
return $this->belongsTo(
Config::get('audit.user.model'),
Config::get('audit.user.foreign_key', 'user_id'),
Config::get('audit.user.primary_key', 'id')
);
}

/**
* {@inheritdoc}
*/
public function resolveData()
{
// Metadata
$this->data = [
'audit_id' => $this->id,
'audit_event' => $this->event,
'audit_url' => $this->url,
'audit_ip_address' => $this->ip_address,
'audit_user_agent' => $this->user_agent,
'audit_created_at' => $this->serializeDate($this->created_at),
'user_id' => $this->user_id,
];

if ($this->relationLoaded('user')) {
foreach ($this->user->attributesToArray() as $attribute => $value) {
$this->data['user_'.$attribute] = $value;
}
}

$this->metadata = array_keys($this->data);

// Modified Auditable attributes
foreach ($this->new_values as $key => $value) {
$this->data['new_'.$key] = $value;
}

foreach ($this->old_values as $key => $value) {
$this->data['old_'.$key] = $value;
}

$this->modified = array_diff_key(array_keys($this->data), $this->metadata);

return $this->data;
}

/**
* {@inheritdoc}
*/
public function getDataValue($key)
{
if (!array_key_exists($key, $this->data)) {
return;
}

$value = $this->data[$key];

// Apply a mutator or a cast the Auditable model may have defined
if ($this->auditable && starts_with($key, ['new_', 'old_'])) {
$originalKey = substr($key, 4);

if ($this->auditable->hasGetMutator($originalKey)) {
return $this->auditable->mutateAttribute($originalKey, $value);
}

if ($this->auditable->hasCast($originalKey)) {
return $this->auditable->castAttribute($originalKey, $value);
}
}

return $value;
}

/**
* {@inheritdoc}
*/
public function getMetadata($json = false, $options = 0, $depth = 512)
{
if (empty($this->data)) {
$this->resolveData();
}

$metadata = [];

foreach ($this->metadata as $key) {
$metadata[$key] = $this->getDataValue($key);
}

return $json ? json_encode($metadata, $options, $depth) : $metadata;
}

/**
* {@inheritdoc}
*/
public function getModified($json = false, $options = 0, $depth = 512)
{
if (empty($this->data)) {
$this->resolveData();
}

$modified = [];

foreach ($this->modified as $key) {
$attribute = substr($key, 4);
$state = substr($key, 0, 3);

$modified[$attribute][$state] = $this->getDataValue($key);
}

return $json ? json_encode($modified, $options, $depth) : $modified;
}
}
Loading

0 comments on commit bf5dbdb

Please sign in to comment.