Git Product home page Git Product logo

wn-easyaudit-plugin's Introduction

About

Easily view and manage audit logs for models within your Winter CMS projects.

Installation

To install it with Composer, run composer require luketowers/wn-easyaudit-plugin from your project root.

Documentation

To get started using this plugin just add the TrackableModel Behavior to any models that you wish to track:

class MyModel extends Model
{
    /**
     * @var array Behaviors implemented by this model class
     */
    public $implement = ['@LukeTowers.EasyAudit.Behaviors.TrackableModel'];
}

Once you've added the TrackableModel behavior to a model, any local events fired on the model that have been set up in $trackableEvents will be automatically listened to and an audit record will be generated for each event.

By default, the TrackableModel behavior will listen to the following events:

  • model.afterCreate
  • model.afterUpdate
  • model.afterDelete

In addition to the properties above, you can also add the following properties to model classes to configure the audit logging behavior:

class MyModel extends Model
{
    // ...

    /**
     * @var array The model events that are to be tracked as activities
     */
    public $trackableEvents = [
        'model.afterCreate' => ['name' => 'created', 'description' => 'The record was created'],
        'model.afterUpdate' => ['name' => 'updated', 'description' => 'The record was updated'],
        'model.afterDelete' => ['name' => 'archived', 'description' => 'The record was archived'],
    ];

    /**
     * @var bool Manually control the IP address logging on this model (default from the luketowers.easyaudit.logIpAddress config setting)
     */
    public $trackableLogIpAddress = true;

    /**
     * @var bool Manually control the User agent logging on this model (default from the luketowers.easyaudit.logUserAgent config setting)
     */
    public $trackableLogUserAgent = true;

    /**
     * @var bool Manually control the change tracking on this model (default from the luketowers.easyaudit.trackChanges config setting)
     */
    public $trackableTrackChanges = true;

    /**
     * @var bool Manually control if the activities field gets automatically injected into backend forms
     * for this model (default from the luketowers.easyaudit.autoInjectActvitiesFormWidget config setting)
     */
    public $trackableInjectActvitiesFormWidget = true;
}

You can view a model's audit log by adding a field targeting the activities relationship (added by the TrackableModel behavior) with the type of activitylog to the relevant fields.yaml files:

activities:
    tab: Audit Log
    context: [update, preview]
    type: activitylog
    span: full

Advanced Usage

It is also possible to log events directly using the LukeTowers\EasyAudit\Classes\ActivityLogger class:

To use, create a new instance of this class and then either chain the methods for the data as required or call the log() method directly. Example (all in one):

$activity = new ActivityLogger();
$activity->log('updated', 'MyModel updated', $myModel, BackendAuth::getUser(), ['maintenanceMode' => true], 'MyVendor.MyPlugin');

Or (chained):

$activity = new ActivityLogger();
$activity->inLog('MyVendor.MyPlugin')
        ->for($myModel)
        ->by(BackendAuth::getUser())
        ->description('MyModel updated')
        ->properties(['maintenanceMode' => true])
        ->log('updated');

Additionally, the ActivityLogger() class is available on models implementing the LukeTowers.EasyAudit.Behaviors.TrackableModel behavior through the activity() method. This enables you to do the following:

class Asset extends Model
{
    // ...

    public function updateInventory()
    {
        // ...
        $this->activity('updated_inventory')->description("The asset's inventory was updated")->log();
    }
}

wn-easyaudit-plugin's People

Contributors

fansaien avatar luketowers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wn-easyaudit-plugin's Issues

Call to member function is null

@LukeTowers

Excellent plugin!

However I'm having some issues when extending RainLab\Blog\Models\Category:

<?php

namespace ThePottingShed\Blog\Classes;

use Event;
use RainLab\Blog\Controllers\Categories;
use RainLab\Blog\Models\Category;

/**
 * Extend Blog Category.
 */
class ExtendBlogCategory
{
    /**
     * Create a New Instance.
     */
    public function __construct()
    {
        Category::extend(function ($model) {
            $model->implement[] = '@LukeTowers.EasyAudit.Behaviors.TrackableModel';
            $model->addDynamicProperty('trackableAllowDuplicates', false);
            $model->addDynamicProperty('trackableEvents', [
                'model.afterSave',
                'model.afterCreate',
                'model.afterFetch',
            ]);
            $model->addDynamicProperty('trackableEventNames', [
                'model.afterSave'   => 'updated',
                'model.afterCreate' => 'created',
                'model.afterFetch'  => 'viewed',
            ]);
            $model->addDynamicProperty('trackableEventDescriptions', [
                'model.afterSave'   => 'The model was updated',
                'model.afterCreate' => 'The model was created',
                'model.afterFetch'  => 'The model was viewed',
            ]);
        });
        Categories::extend(function ($controller) {
            $controller->implement[] = 'Backend.Behaviors.RelationController';
            $controller->addDynamicProperty('relationConfig', '$/luketowers/easyaudit/yaml/relation.activities.yaml');
        });
        Event::listen('backend.form.extendFields', function ($widget) {
            if ($widget->isNested) {
                return;
            }
            if (!$widget->getController() instanceof Categories) {
                return;
            }
            if (!$widget->model instanceof Category) {
                return;
            }
            $widget->addSecondaryTabFields([
                '_activities' => [
                    'type' => 'partial',
                    'path' => '$/luketowers/easyaudit/partials/field.activities.htm',
                    'tab'  => 'luketowers.easyaudit::lang.models.activity.label_plural',
                ],
            ]);
        });
    }
}

Called from plugin boot method eg:

<?php

namespace ThePottingShed\Blog;

use App;
use Event;
use System\Classes\PluginBase;
use ThePottingShed\Blog\Classes\ExtendBlogCategory;

/**
 * Blog.
 *
 * @author The Potting Shed <[email protected]>
 */
class Plugin extends PluginBase
{
    /**
     * Require.
     *
     * @var array
     */
    public $require = [
        'LukeTowers.EasyAudit',
        'RainLab.Blog',
    ];

    /**
     * Plugin Details.
     *
     * @return array
     */
    public function pluginDetails()
    {
        return [
            'name'        => 'thepottingshed.blog::lang.plugin.name',
            'description' => 'thepottingshed.blog::lang.plugin.description',
            'homepage'    => 'https://github.com/thepottingshed/octobercms-blog-plugin',
            'author'      => 'The Potting Shed',
            'icon'        => 'icon-pencil',
        ];
    }

    /**
     * Boot.
     *
     * @return void
     */
    public function boot()
    {
        new ExtendBlogCategory();
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.