├── .github └── issue_template.md ├── .styleci.yml ├── LICENSE ├── README.md ├── codesize.xml ├── composer.json ├── src └── Traits │ ├── CreatedBy.php │ ├── DeletedBy.php │ └── UpdatedBy.php └── tests └── features ├── CreatedByTest.php ├── DeletedByTest.php └── UpdatedByTest.php /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | 2 | This is a **bug | feature request**. 3 | 4 | 5 | ### Prerequisites 6 | * [ ] Are you running the latest version? 7 | * [ ] Are you reporting to the correct repository? 8 | * [ ] Did you check the documentation? 9 | * [ ] Did you perform a cursory search? 10 | 11 | ### Description 12 | 13 | 14 | ### Steps to Reproduce 15 | 20 | 21 | ### Expected behavior 22 | 23 | 24 | ### Actual behavior 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | risky: true 2 | 3 | preset: laravel 4 | 5 | enabled: 6 | - strict 7 | - unalign_double_arrow 8 | 9 | disabled: 10 | - short_array_syntax 11 | 12 | finder: 13 | exclude: 14 | - "public" 15 | - "resources" 16 | - "tests" 17 | name: 18 | - "*.php" 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 laravel-enso 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Track Who 2 | 3 | [![Codacy Badge](https://app.codacy.com/project/badge/Grade/b499044a68b84f339b905576570c84ab)](https://www.codacy.com/gh/laravel-enso/track-who?utm_source=github.com&utm_medium=referral&utm_content=laravel-enso/track-who&utm_campaign=Badge_Grade) 4 | [![StyleCI](https://github.styleci.io/repos/85499255/shield?branch=master)](https://github.styleci.io/repos/85499255) 5 | [![License](https://poser.pugx.org/laravel-enso/track-who/license)](https://packagist.org/packages/laravel-enso/track-who) 6 | [![Total Downloads](https://poser.pugx.org/laravel-enso/track-who/downloads)](https://packagist.org/packages/laravel-enso/track-who) 7 | [![Latest Stable Version](https://poser.pugx.org/laravel-enso/track-who/version)](https://packagist.org/packages/laravel-enso/track-who) 8 | 9 | Create, update and delete authoring tracking dependency for Laravel. 10 | 11 | This package can work independently of the [Enso](https://github.com/laravel-enso/Enso) ecosystem. 12 | 13 | For live examples and demos, you may visit [laravel-enso.com](https://www.laravel-enso.com) 14 | 15 | ### Installation, Configuration & Usage 16 | 17 | Be sure to check out the full documentation for this package available at [docs.laravel-enso.com](https://docs.laravel-enso.com/backend/track-who.html) 18 | 19 | ## Contributions 20 | 21 | are welcome. Pull requests are great, but issues are good too. 22 | 23 | ## License 24 | 25 | This package is released under the MIT license. 26 | -------------------------------------------------------------------------------- /codesize.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | custom rules 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-enso/track-who", 3 | "description": "Create, update and delete authoring tracking dependency for Laravel Enso", 4 | "keywords": [ 5 | "laravel-enso", 6 | "track-who" 7 | ], 8 | "homepage": "https://github.com/laravel-enso/track-who", 9 | "type": "library", 10 | "license": "MIT", 11 | "authors": [{ 12 | "name": "Adrian Ocneanu", 13 | "email": "aocneanu@gmail.com", 14 | "homepage": "https://laravel-enso.com", 15 | "role": "Developer" 16 | }], 17 | "require": { 18 | "laravel/framework": "^7.0|^8.0|^9.0|^10.0|^11.0", 19 | "php": "^8.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "LaravelEnso\\TrackWho\\": "src/" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Traits/CreatedBy.php: -------------------------------------------------------------------------------- 1 | $model->setCreatedBy()); 14 | } 15 | 16 | public function createdBy(): Relation 17 | { 18 | $userModel = Config::get('auth.providers.users.model'); 19 | 20 | return $this->belongsTo($userModel, 'created_by'); 21 | } 22 | 23 | private function setCreatedBy() 24 | { 25 | if (Auth::check()) { 26 | $this->forceFill(['created_by' => Auth::id()]); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Traits/DeletedBy.php: -------------------------------------------------------------------------------- 1 | $model->setDeletedBy()); 14 | } 15 | 16 | public function deletedBy(): Relation 17 | { 18 | $userModel = Config::get('auth.providers.users.model'); 19 | 20 | return $this->belongsTo($userModel, 'deleted_by'); 21 | } 22 | 23 | private function setDeletedBy() 24 | { 25 | if (! Auth::check()) { 26 | return; 27 | } 28 | 29 | $events = $this->getEventDispatcher(); 30 | 31 | tap($this)->unsetEventDispatcher() 32 | ->forceFill(['deleted_by' => Auth::id()] + $this->getOriginal()) 33 | ->save(); 34 | 35 | $this->setEventDispatcher($events); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Traits/UpdatedBy.php: -------------------------------------------------------------------------------- 1 | $model->setUpdatedBy()); 14 | 15 | self::updating(fn ($model) => $model->setUpdatedBy()); 16 | } 17 | 18 | public function updatedBy(): Relation 19 | { 20 | $userModel = Config::get('auth.providers.users.model'); 21 | 22 | return $this->belongsTo($userModel, 'updated_by'); 23 | } 24 | 25 | private function setUpdatedBy() 26 | { 27 | if (Auth::check()) { 28 | $this->forceFill(['updated_by' => Auth::id()]); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/features/CreatedByTest.php: -------------------------------------------------------------------------------- 1 | seed() 21 | ->createTestModelsTable() 22 | ->actingAs($userModel::first()); 23 | } 24 | 25 | /** @test */ 26 | public function adds_created_by_when_creating_model() 27 | { 28 | $testModel = CreatedByTestModel::create(); 29 | 30 | $this->assertEquals(Auth::id(), $testModel->created_by); 31 | } 32 | 33 | private function createTestModelsTable() 34 | { 35 | Schema::create('created_by_test_models', function ($table) { 36 | $table->increments('id'); 37 | $table->integer('created_by')->unsigned()->nullable(); 38 | $table->timestamps(); 39 | }); 40 | 41 | return $this; 42 | } 43 | } 44 | 45 | class CreatedByTestModel extends Model 46 | { 47 | use CreatedBy; 48 | } 49 | -------------------------------------------------------------------------------- /tests/features/DeletedByTest.php: -------------------------------------------------------------------------------- 1 | seed() 22 | ->createTestModelsTable() 23 | ->actingAs($userModel::first()); 24 | } 25 | 26 | /** @test */ 27 | public function adds_deleted_by_when_deleting_model() 28 | { 29 | DeletedByTestModel::create() 30 | ->delete(); 31 | 32 | $testModel = DeletedByTestModel::withTrashed()->first(); 33 | 34 | $this->assertEquals(Auth::id(), $testModel->deleted_by); 35 | } 36 | 37 | private function createTestModelsTable() 38 | { 39 | Schema::create('deleted_by_test_models', function ($table) { 40 | $table->increments('id'); 41 | $table->integer('deleted_by')->nullable(); 42 | $table->softDeletes(); 43 | $table->timestamps(); 44 | }); 45 | 46 | return $this; 47 | } 48 | } 49 | 50 | class DeletedByTestModel extends Model 51 | { 52 | use SoftDeletes, DeletedBy; 53 | } 54 | -------------------------------------------------------------------------------- /tests/features/UpdatedByTest.php: -------------------------------------------------------------------------------- 1 | seed() 21 | ->createTestModelsTable() 22 | ->actingAs($userModel::first()); 23 | } 24 | 25 | /** @test */ 26 | public function adds_updated_by_when_updating_model() 27 | { 28 | $testModel = UpdatedByTestModel::create(['name' => 'initial']); 29 | 30 | $testModel->update(['name' => 'changed']); 31 | 32 | $this->assertEquals(Auth::id(), $testModel->updated_by); 33 | } 34 | 35 | private function createTestModelsTable() 36 | { 37 | Schema::create('updated_by_test_models', function ($table) { 38 | $table->increments('id'); 39 | $table->string('name'); 40 | $table->integer('updated_by')->unsigned()->nullable(); 41 | $table->timestamps(); 42 | }); 43 | 44 | return $this; 45 | } 46 | } 47 | 48 | class UpdatedByTestModel extends Model 49 | { 50 | use UpdatedBy; 51 | 52 | protected $fillable = ['name']; 53 | } 54 | --------------------------------------------------------------------------------