├── .gitignore ├── .travis.yml ├── LICENSE.txt ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── Controller │ └── BlameTrait.php ├── Event │ └── LoggedInUserListener.php └── Model │ └── Behavior │ └── BlameBehavior.php └── tests ├── Fixture └── empty ├── TestCase ├── Controller │ └── Component │ │ └── empty ├── Model │ └── Behavior │ │ ├── BlameBehaviorTest.php │ │ └── empty └── View │ └── Helper │ └── empty └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | /composer.lock 2 | /vendor 3 | /phpunit.xml 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | 8 | env: 9 | matrix: 10 | - DB=mysql db_class='Cake\Database\Driver\Mysql' db_dsn='mysql:host=0.0.0.0;dbname=cakephp_test' db_database='cakephp_test' db_login='travis' db_password='' 11 | global: 12 | - DEFAULT=1 13 | 14 | matrix: 15 | allow_failures: 16 | - php: 5.4 17 | env: COVERALLS=1 DEFAULT=0 18 | 19 | fast_finish: true 20 | 21 | include: 22 | - php: 5.4 23 | env: DB=pgsql db_class='Cake\Database\Driver\Postgres' db_dsn='pgsql:host=127.0.0.1;dbname=cakephp_test' db_database="cakephp_test" db_login='postgres' db_password='' 24 | 25 | - php: 5.4 26 | env: DB=sqlite db_class='Cake\Database\Driver\Sqlite' db_dsn='sqlite::memory:' 27 | 28 | - php: 5.4 29 | env: PHPCS=1 DEFAULT=0 30 | 31 | - php: 5.4 32 | env: COVERALLS=1 DEFAULT=0 33 | 34 | before_script: 35 | - composer self-update 36 | - composer install --prefer-dist --no-interaction --dev 37 | 38 | - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi" 39 | 40 | - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi" 41 | 42 | - sh -c "if [ '$PHPCS' = '1' ]; then pear channel-discover pear.cakephp.org; fi" 43 | - sh -c "if [ '$PHPCS' = '1' ]; then pear install --alldeps cakephp/CakePHP_CodeSniffer; fi" 44 | 45 | - sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev satooshi/php-coveralls:dev-master; fi" 46 | - sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi" 47 | 48 | - phpenv rehash 49 | - set +H 50 | - cp phpunit.xml.dist phpunit.xml 51 | 52 | script: 53 | - sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --stderr --coverage-clover build/logs/clover.xml; fi" 54 | - sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -c .coveralls.yml -v; fi" 55 | - sh -c "if [ '$DEFAULT' = '1' ]; then phpunit --stderr; fi" 56 | - sh -c "if [ '$PHPCS' = '1' ]; then phpcs -p --extensions=php --standard=CakePHP --ignore=vendor --ignore=docs . ; fi" 57 | 58 | notifications: 59 | email: false 60 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 M. Ypes, aka Ceeram 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Blame 2 | 3 | [![Build Status](https://api.travis-ci.org/ceeram/blame.png)](https://travis-ci.org/ceeram/blame) 4 | [![Latest Stable Version](https://poser.pugx.org/ceeram/cakephp-blame/v/stable.svg)](https://packagist.org/packages/ceeram/cakephp-blame) 5 | [![Total Downloads](https://poser.pugx.org/ceeram/cakephp-blame/downloads.svg)](https://packagist.org/packages/ceeram/cakephp-blame) 6 | [![Latest Unstable Version](https://poser.pugx.org/ceeram/cakephp-blame/v/unstable.svg)](https://packagist.org/packages/ceeram/cakephp-blame) 7 | [![License](https://poser.pugx.org/ceeram/cakephp-blame/license.svg)](https://packagist.org/packages/ceeram/cakephp-blame) 8 | 9 | CakePHP 3.0 plugin to update `created_by` and `modified_by` fields. 10 | 11 | # Abandoned 12 | This package is abandoned and no longer maintained. The author suggests using the [muffin/footprint](https://github.com/usemuffin/footprint) package instead. 13 | 14 | ## Installation 15 | 16 | 17 | Add the following lines to your application's `composer.json`: 18 | 19 | ``` 20 | "require": { 21 | "ceeram/cakephp-blame": "~1.0" 22 | } 23 | ``` 24 | 25 | followed by the command: 26 | 27 | `composer update` 28 | 29 | Or run the following command directly without changing your `composer.json`: 30 | 31 | `composer require ceeram/cakephp-blame:~1.0` 32 | 33 | ## Usage 34 | 35 | In your app's `config/bootstrap.php` add: `Plugin::load('Ceeram/Blame')`; 36 | 37 | ## Configuration 38 | 39 | Add the following line to your AppController: 40 | 41 | ``` 42 | use \Ceeram\Blame\Controller\BlameTrait; 43 | ``` 44 | 45 | Add the following inside your AppController Class 46 | 47 | ``` 48 | class AppController extends Controller 49 | { 50 | use BlameTrait; 51 | } 52 | ``` 53 | 54 | Attach the behavior in the models you want with: 55 | 56 | ``` 57 | public function initialize(array $config) { 58 | $this->addBehavior('Ceeram/Blame.Blame'); 59 | } 60 | ``` 61 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ceeram/cakephp-blame", 3 | "description": "CakePHP plugin to update created_by and modified_by fields", 4 | "license": "MIT", 5 | "type": "cakephp-plugin", 6 | "authors": [ 7 | { 8 | "name": "Ceeram", 9 | "email": "c33ram@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0", 14 | "cakephp/plugin-installer": "*" 15 | }, 16 | "require-dev": { 17 | "cakephp/cakephp": "~3.0", 18 | "phpunit/phpunit": "4.1.*" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "Ceeram\\Blame\\": "src" 23 | } 24 | }, 25 | "autoload-dev": { 26 | "psr-4": { 27 | "Ceeram\\Blame\\Test\\": "tests" 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | ./tests/TestCase 17 | 18 | 19 | 20 | 21 | 22 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/Controller/BlameTrait.php: -------------------------------------------------------------------------------- 1 | Auth); 21 | $model->eventManager()->attach($listener); 22 | return $model; 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /src/Event/LoggedInUserListener.php: -------------------------------------------------------------------------------- 1 | _Auth = $Auth; 30 | } 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | public function implementedEvents() { 36 | return [ 37 | 'Model.beforeSave' => [ 38 | 'callable' => 'beforeSave', 39 | 'priority' => -100 40 | ] 41 | ]; 42 | } 43 | 44 | /** 45 | * Before save listener. 46 | * 47 | * @param \Cake\Event\Event $event The beforeSave event that was fired 48 | * @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved 49 | * @param \ArrayObject $options the options passed to the save method 50 | * @return void 51 | */ 52 | public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) { 53 | if (empty($options['loggedInUser'])) { 54 | $options['loggedInUser'] = $this->_Auth->user('id'); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Model/Behavior/BlameBehavior.php: -------------------------------------------------------------------------------- 1 | isNew()) { 25 | $entity->set('created_by', $options['loggedInUser']); 26 | } 27 | 28 | $entity->set('modified_by', $options['loggedInUser']); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/Fixture/empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceeram/blame/550bdd7ae91dc11704eb4acc1b34f92ba2642cb3/tests/Fixture/empty -------------------------------------------------------------------------------- /tests/TestCase/Controller/Component/empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceeram/blame/550bdd7ae91dc11704eb4acc1b34f92ba2642cb3/tests/TestCase/Controller/Component/empty -------------------------------------------------------------------------------- /tests/TestCase/Model/Behavior/BlameBehaviorTest.php: -------------------------------------------------------------------------------- 1 | getMock('Cake\ORM\Table'); 42 | $this->Behavior = new BlameBehavior($table); 43 | 44 | $event = new Event('Model.beforeSave'); 45 | $entity = new Entity(['name' => 'Foo']); 46 | $entity->isNew(false); 47 | 48 | $options = new ArrayObject(['loggedInUser' => null]); 49 | $this->Behavior->beforeSave($event, $entity, $options); 50 | 51 | $this->assertNull($entity->created_by, 'created_by is expected NOT to be updated'); 52 | $this->assertNull($entity->modified_by, 'modified_by is expected NOT to be updated'); 53 | 54 | $options = new ArrayObject(['loggedInUser' => 5]); 55 | $this->Behavior->beforeSave($event, $entity, $options); 56 | 57 | $this->assertNull($entity->created_by, 'created_by is expected NOT to be updated'); 58 | $this->assertSame(5, $entity->modified_by, 'modified_by is expected to be updated'); 59 | 60 | $entity->isNew(true); 61 | $this->Behavior->beforeSave($event, $entity, $options); 62 | 63 | $this->assertSame(5, $entity->created_by, 'modified_by is expected to be updated'); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /tests/TestCase/Model/Behavior/empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceeram/blame/550bdd7ae91dc11704eb4acc1b34f92ba2642cb3/tests/TestCase/Model/Behavior/empty -------------------------------------------------------------------------------- /tests/TestCase/View/Helper/empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceeram/blame/550bdd7ae91dc11704eb4acc1b34f92ba2642cb3/tests/TestCase/View/Helper/empty -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | register(); 23 | 24 | $loader->addNamespace('Cake\Test\Fixture', ROOT . '/vendor/cakephp/cakephp/tests/Fixture'); 25 | 26 | require_once CORE_PATH . 'config/bootstrap.php'; 27 | 28 | date_default_timezone_set('UTC'); 29 | mb_internal_encoding('UTF-8'); 30 | 31 | Configure::write('debug', true); 32 | Configure::write('App', [ 33 | 'namespace' => 'App', 34 | 'encoding' => 'UTF-8', 35 | 'base' => false, 36 | 'baseUrl' => false, 37 | 'dir' => 'src', 38 | 'webroot' => 'webroot', 39 | 'www_root' => APP . 'webroot', 40 | 'fullBaseUrl' => 'http://localhost', 41 | 'imageBaseUrl' => 'img/', 42 | 'jsBaseUrl' => 'js/', 43 | 'cssBaseUrl' => 'css/', 44 | 'paths' => [ 45 | 'plugins' => [APP . 'Plugin' . DS], 46 | 'templates' => [APP . 'Template' . DS] 47 | ] 48 | ]); 49 | Configure::write('Session', [ 50 | 'defaults' => 'php' 51 | ]); 52 | 53 | Cache::config([ 54 | '_cake_core_' => [ 55 | 'engine' => 'File', 56 | 'prefix' => 'cake_core_', 57 | 'serialize' => true 58 | ], 59 | '_cake_model_' => [ 60 | 'engine' => 'File', 61 | 'prefix' => 'cake_model_', 62 | 'serialize' => true 63 | ], 64 | 'default' => [ 65 | 'engine' => 'File', 66 | 'prefix' => 'default_', 67 | 'serialize' => true 68 | ] 69 | ]); 70 | 71 | // Ensure default test connection is defined 72 | if (!getenv('db_class')) { 73 | putenv('db_class=Cake\Database\Driver\Sqlite'); 74 | putenv('db_dsn=sqlite::memory:'); 75 | } 76 | 77 | ConnectionManager::config('test', [ 78 | 'className' => 'Cake\Database\Connection', 79 | 'driver' => getenv('db_class'), 80 | 'dsn' => getenv('db_dsn'), 81 | 'database' => getenv('db_database'), 82 | 'username' => getenv('db_login'), 83 | 'password' => getenv('db_password'), 84 | 'timezone' => 'UTC' 85 | ]); 86 | 87 | Log::config([ 88 | 'debug' => [ 89 | 'engine' => 'Cake\Log\Engine\FileLog', 90 | 'levels' => ['notice', 'info', 'debug'], 91 | 'file' => 'debug', 92 | ], 93 | 'error' => [ 94 | 'engine' => 'Cake\Log\Engine\FileLog', 95 | 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], 96 | 'file' => 'error', 97 | ] 98 | ]); 99 | 100 | Plugin::load('Ceeram/Blame', ['path' => ROOT]); 101 | --------------------------------------------------------------------------------