2017 © Pedro Peláez
 

cakephp-plugin footprint

CakePHP 3.0+ plugin to allow passing currently logged in user to model layer

image

muffin/footprint

CakePHP 3.0+ plugin to allow passing currently logged in user to model layer

  • Monday, July 16, 2018
  • by ADmad
  • Repository
  • 11 Watchers
  • 58 Stars
  • 64,153 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 15 Forks
  • 5 Open issues
  • 6 Versions
  • 15 % Grown

The README.md

Footprint

Build Status Coverage Total Downloads License, (*1)

This plugin allows you to pass the currently logged in user info to the model layer of a CakePHP application., (*2)

It comes bundled with the FootprintBehavior to allow you control over columns such as user_id, created_by, company_id similar to the core's TimestampBehavior., (*3)

Install

Using Composer:, (*4)

composer require muffin/footprint

You then need to load the plugin by running console command:, (*5)

bin/cake plugin load Muffin/Footprint

The Footprint plugin must be loaded before the Authentication plugin, so you should updated your config/plugins.php or Application::bootstrap() accordingly., (*6)

Usage

Middleware

Add the FootprintMiddleware to the middleware queue in your Application::middleware() method:, (*7)

$middleware->add('Muffin/Footprint.Footprint');

It must be added after AuthenticationMiddleware to ensure that it can read the identify info after authentication is done., (*8)

If you don't have direct access to the place where AuthenticationMiddleware is added then check here., (*9)

Behavior

To use the included behavior to automatically update the created_by and modified_by fields of a record for example, add the following to your table's initialize() method:, (*10)

$this->addBehavior('Muffin/Footprint.Footprint');

You can customize that like so:, (*11)

$this->addBehavior('Muffin/Footprint.Footprint', [
    'events' => [
        'Model.beforeSave' => [
            'user_id' => 'new',
            'company_id' => 'new',
            'modified_by' => 'always'
        ]
    ],
    'propertiesMap' => [
        'company_id' => '_footprint.company.id',
    ],
]);

This will insert the currently logged in user's primary key in user_id and modified_by fields when creating a record, on the modified_by field again when updating the record and it will use the associated user record's company id in the company_id field when creating a record., (*12)

You can also provide a closure that accepts an EntityInterface and returns a bool:, (*13)

$this->addBehavior('Muffin/Footprint.Footprint', [
    'events' => [
        'Model.beforeSave' => [
            'user_id' => 'new',
            'company_id' => 'new',
            'modified_by' => 'always',
            'deleted_by' => function ($entity): bool {
                return $entity->deleted !== null;
            },
        ]
    ],
]);

Adding middleware via event

In some cases you don't have direct access to the place where the AuthenticationMiddleware is added. Then you will have to add this to your src/Application.php, (*14)

use Authentication\Middleware\AuthenticationMiddleware;
use Cake\Event\EventInterface;
use Cake\Http\MiddlewareQueue;
use Muffin\Footprint\Middleware\FootprintMiddleware;

// inside the bootstrap() method
$this->getEventManager()->on(
    'Server.buildMiddleware',
    function (EventInterface $event, MiddlewareQueue $middleware) {
        $middleware->insertAfter(AuthenticationMiddleware::class, FootprintMiddleware::class);
    }
);

Patches & Features

  • Fork
  • Mod, fix
  • Test - this is important, so it's not unintentionally broken
  • Commit - do not mess with license, todo, version, etc. (if you do change any, bump them into commits of their own that I can ignore when I pull)
  • Pull request - bonus point for topic branches

Bugs & Feedback

http://github.com/usemuffin/footprint/issues, (*15)

License

Copyright (c) 2015-Present, Use Muffin and licensed under The MIT License., (*16)

The Versions

16/07 2018

dev-master

9999999-dev https://github.com/usemuffin/footprint

CakePHP 3.0+ plugin to allow passing currently logged in user to model layer

  Sources   Download

MIT

The Requires

 

The Development Requires

cakephp footprint muffin

15/04 2018

1.2.0

1.2.0.0 https://github.com/usemuffin/footprint

CakePHP 3.0+ plugin to allow passing currently logged in user to model layer

  Sources   Download

MIT

The Requires

 

The Development Requires

cakephp footprint muffin

15/04 2018

dev-cake-update

dev-cake-update https://github.com/usemuffin/footprint

CakePHP 3.0+ plugin to allow passing currently logged in user to model layer

  Sources   Download

MIT

The Requires

 

The Development Requires

cakephp footprint muffin

12/03 2018

dev-cake.next

dev-cake.next https://github.com/usemuffin/footprint

CakePHP 3.0+ plugin to allow passing currently logged in user to model layer

  Sources   Download

MIT

The Development Requires

cakephp footprint muffin

21/04 2017

1.1.0

1.1.0.0 https://github.com/usemuffin/footprint

CakePHP 3.0+ plugin to allow passing currently logged in user to model layer

  Sources   Download

MIT

The Development Requires

cakephp footprint muffin

02/06 2016

1.0.0

1.0.0.0 https://github.com/usemuffin/footprint

CakePHP 3.0+ plugin to allow passing currently logged in user to model layer

  Sources   Download

MIT

The Development Requires

cakephp footprint muffin