├── .styleci.yml ├── config └── manageable.php ├── CHANGELOG.md ├── LICENSE.md ├── composer.json ├── src ├── ManageableServiceProvider.php └── Manageable.php └── README.md /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /config/manageable.php: -------------------------------------------------------------------------------- 1 | true, 5 | 'foreign_table' => 'users', 6 | 'foreign_key' => 'id', 7 | ]; -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-manageable` will be documented in this file 4 | 5 | ## v2.3.1 - 2020-09-11 6 | 7 | - Add support for Laravel 8 8 | 9 | ## v2.3.0 - 2020-03-03 10 | - Add support for Laravel 7 11 | - Drop support for Laravel 5.x 12 | 13 | ## v2.2.1 - 2019-12-05 14 | - Foreign keys no longer cascade data on delete 15 | 16 | ## v2.2.0 - 2019-09-10 17 | - Add support for Laravel 6.0 18 | 19 | ## v2.1.0 - 2019-08-22 20 | - Bugfix: The `editor` is now updated on every update as intended 21 | - Test improvements 22 | 23 | ## v2.0.1 - 2019-05-28 24 | - Ignore all test and documentation with "export-ignore" 25 | 26 | ## v2.0.0 - 2019-03-21 27 | - Fix issue #8 (breaking change) 28 | - Fix compatibility with `TestCase` 29 | 30 | ## 1.2.0 - 2019-03-06 31 | - Added support for Laravel 5.8 32 | 33 | ## 1.1.0 - 2018-09-27 34 | - Added support for Laravel 5.7 35 | 36 | ## 1.0.2 - 2018-01-29 37 | 38 | - Allow to customize foreign key `id` on `users` table from macro. 39 | 40 | ## 1.0.1 - 2018-01-29 41 | 42 | - Add changelog 43 | - Allow to customize `users` table from macro. 44 | 45 | ## 1.0.0 - 2018-01-28 46 | 47 | - Initial release! 48 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Signifly 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 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "signifly/laravel-manageable", 3 | "description": "Track users who manages models in your Laravel app", 4 | "homepage": "https://github.com/signifly/laravel-manageable", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Morten Poul Jensen", 9 | "email": "mpj@signifly.com", 10 | "role": "Developer" 11 | } 12 | ], 13 | "require": { 14 | "php": "^7.4|^8.0", 15 | "illuminate/database": "^8.0|^9.0|^10.0", 16 | "illuminate/support": "^8.0|^9.0|^10.0" 17 | }, 18 | "require-dev": { 19 | "phpunit/phpunit": "^9.0", 20 | "orchestra/testbench": "^6.0|^7.0|^8.0" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Signifly\\Manageable\\": "src" 25 | } 26 | }, 27 | "autoload-dev": { 28 | "psr-4": { 29 | "Signifly\\Manageable\\Test\\": "tests" 30 | } 31 | }, 32 | "scripts": { 33 | "test": "vendor/bin/phpunit" 34 | }, 35 | "config": { 36 | "sort-packages": true 37 | }, 38 | "extra": { 39 | "laravel": { 40 | "providers": [ 41 | "Signifly\\Manageable\\ManageableServiceProvider" 42 | ] 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/ManageableServiceProvider.php: -------------------------------------------------------------------------------- 1 | unsignedBigInteger('created_by')->nullable()->index() 25 | : $this->unsignedInteger('created_by')->nullable()->index(); 26 | $bigIntegers 27 | ? $this->unsignedBigInteger('updated_by')->nullable()->index() 28 | : $this->unsignedInteger('updated_by')->nullable()->index(); 29 | 30 | $this->foreign('created_by') 31 | ->references($foreignKey) 32 | ->on($foreignTable) 33 | ->onDelete('set null'); 34 | 35 | $this->foreign('updated_by') 36 | ->references($foreignKey) 37 | ->on($foreignTable) 38 | ->onDelete('set null'); 39 | }); 40 | 41 | $this->publishes([ 42 | __DIR__.'/../config/manageable.php' => config_path('manageable.php') 43 | ], 'config'); 44 | } 45 | 46 | /** 47 | * Register the service provider. 48 | * 49 | * @return void 50 | */ 51 | public function register() 52 | { 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Manageable.php: -------------------------------------------------------------------------------- 1 | setManageable('created_by', 'creator'); 15 | }); 16 | 17 | static::updating(function (Model $model) { 18 | $model->setManageable('updated_by', 'editor'); 19 | }); 20 | } 21 | 22 | public function creator(): BelongsTo 23 | { 24 | return $this->belongsTo($this->getManageableUsersModel(), 'created_by'); 25 | } 26 | 27 | public function editor(): BelongsTo 28 | { 29 | return $this->belongsTo($this->getManageableUsersModel(), 'updated_by'); 30 | } 31 | 32 | public function hasCreator(): bool 33 | { 34 | return ! is_null($this->created_by); 35 | } 36 | 37 | public function hasEditor(): bool 38 | { 39 | return ! is_null($this->updated_by); 40 | } 41 | 42 | protected function setManageable(string $type, string $relation): void 43 | { 44 | $guard = $this->getManageableGuard(); 45 | 46 | if ($guard->check()) { 47 | $this->{$type} = $guard->id(); 48 | $this->setRelation($relation, $guard->user()); 49 | } 50 | } 51 | 52 | protected function getManageableUsersModel() 53 | { 54 | return method_exists($this, 'manageableUsersModel') 55 | ? $this->manageableUsersModel() 56 | : ($this->manageableUsersModel ?? config('auth.providers.users.model')); 57 | } 58 | 59 | protected function getManageableGuard() 60 | { 61 | $manageableGuardName = method_exists($this, 'manageableGuardName') 62 | ? $this->manageableGuardName() 63 | : ($this->manageableGuardName ?? config('auth.defaults.guard')); 64 | 65 | return Auth::guard($manageableGuardName); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Track users who manage models in your Laravel app 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/signifly/laravel-manageable.svg?style=flat-square)](https://packagist.org/packages/signifly/laravel-manageable) 4 | [![Build Status](https://img.shields.io/travis/signifly/laravel-manageable/master.svg?style=flat-square)](https://travis-ci.org/signifly/laravel-manageable) 5 | [![StyleCI](https://styleci.io/repos/119214202/shield?branch=master)](https://styleci.io/repos/119214202) 6 | [![Quality Score](https://img.shields.io/scrutinizer/g/signifly/laravel-manageable.svg?style=flat-square)](https://scrutinizer-ci.com/g/signifly/laravel-manageable) 7 | [![Total Downloads](https://img.shields.io/packagist/dt/signifly/laravel-manageable.svg?style=flat-square)](https://packagist.org/packages/signifly/laravel-manageable) 8 | 9 | The `signifly/laravel-manageable` package allows you to easily track who creates/updates your models. 10 | 11 | All you have to do to get started is: 12 | 13 | ```php 14 | // 1. Add required columns to your table by using our macro manageable 15 | Schema::create('orders', function (Blueprint $table) { 16 | // ... 17 | $table->manageable(); 18 | 19 | // params: $bigIntegers (default: true), $foreignTable (default: 'users'), $foreignKey (default: 'id') 20 | $table->manageable(false, 'some_users_table', 'u_id'); 21 | }); 22 | 23 | // 2. Add the Manageable trait to your model 24 | class Order extends Model 25 | { 26 | use Manageable; 27 | } 28 | ``` 29 | 30 | The macro `manageable` adds the following to your table: 31 | ```php 32 | $this->unsignedBigInteger('created_by')->nullable()->index(); 33 | $this->unsignedBigInteger('updated_by')->nullable()->index(); 34 | 35 | $this->foreign('created_by') 36 | ->references('id') 37 | ->on('users') 38 | ->onDelete('set null'); 39 | 40 | $this->foreign('updated_by') 41 | ->references('id') 42 | ->on('users') 43 | ->onDelete('set null'); 44 | ``` 45 | 46 | ## Documentation 47 | Until further documentation is provided, please have a look at the tests. 48 | 49 | ## Installation 50 | 51 | You can install the package via composer: 52 | 53 | ```bash 54 | composer require signifly/laravel-manageable 55 | ``` 56 | 57 | The package will automatically register itself. 58 | 59 | You can publish the config with: 60 | ```bash 61 | php artisan vendor:publish --provider="Signifly\Manageable\ManageableServiceProvider" 62 | ``` 63 | 64 | ## Testing 65 | ```bash 66 | composer test 67 | ``` 68 | 69 | ## Security 70 | 71 | If you discover any security issues, please email dev@signifly.com instead of using the issue tracker. 72 | 73 | ## Credits 74 | 75 | - [Morten Poul Jensen](https://github.com/pactode) 76 | - [All contributors](../../contributors) 77 | 78 | ## License 79 | 80 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 81 | --------------------------------------------------------------------------------