├── .github └── issue_template.md ├── .styleci.yml ├── LICENSE ├── README.md ├── codesize.xml ├── composer.json ├── database └── migrations │ └── 2017_01_01_130000_create_action_logs_table.php ├── src ├── AppServiceProvider.php ├── Dynamics │ └── ActionLogs.php ├── Http │ └── Middleware │ │ └── ActionLogger.php └── Models │ └── ActionLog.php └── tests └── features └── ActionLoggerTest.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 | version: 8 5 | 6 | enabled: 7 | - strict 8 | - unalign_double_arrow 9 | - phpdoc_order 10 | - phpdoc_separation 11 | 12 | disabled: 13 | - short_array_syntax 14 | 15 | finder: 16 | exclude: 17 | - "public" 18 | - "resources" 19 | - "tests" 20 | name: 21 | - "*.php" 22 | -------------------------------------------------------------------------------- /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 | # Action Logger 2 | [![Codacy Badge](https://app.codacy.com/project/badge/Grade/2d3169454da144cdb8fb4a15b0a1e294)](https://www.codacy.com/gh/laravel-enso/action-logger?utm_source=github.com&utm_medium=referral&utm_content=laravel-enso/action-logger&utm_campaign=Badge_Grade) 3 | [![StyleCI](https://github.styleci.io/repos/85554059/shield?branch=master)](https://github.styleci.io/repos/85554059) 4 | [![License](https://poser.pugx.org/laravel-enso/action-logger/license)](https://packagist.org/packages/laravel-enso/action-logger) 5 | [![Total Downloads](https://poser.pugx.org/laravel-enso/action-logger/downloads)](https://packagist.org/packages/laravel-enso/action-logger) 6 | [![Latest Stable Version](https://poser.pugx.org/laravel-enso/action-logger/version)](https://packagist.org/packages/laravel-enso/action-logger) 7 | 8 | User actions logger dependency for [Laravel](https://laravel.com). 9 | 10 | This package works exclusively within the [Enso](https://github.com/laravel-enso/Enso) ecosystem. 11 | 12 | The front end assets that utilize this api are present in the [ui](https://github.com/enso-ui/ui) package. 13 | 14 | For live examples and demos, you may visit [laravel-enso.com](https://www.laravel-enso.com) 15 | 16 | ### Installation, Configuration & Usage 17 | 18 | Be sure to check out the full documentation for this package available at [docs.laravel-enso.com](https://docs.laravel-enso.com/backend/action-logger.html) 19 | 20 | ### Contributions 21 | 22 | are welcome. Pull requests are great, but issues are good too. 23 | 24 | ### License 25 | 26 | This package is released under the MIT license. 27 | -------------------------------------------------------------------------------- /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/action-logger", 3 | "description": "User actions logger dependency for Laravel Enso", 4 | "keywords": [ 5 | "laravel-enso", 6 | "action-logger", 7 | "action-logger", 8 | "logging" 9 | ], 10 | "homepage": "https://github.com/laravel-enso/action-logger", 11 | "type": "library", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "Adrian Ocneanu", 16 | "email": "aocneanu@gmail.com", 17 | "homepage": "https://laravel-enso.com", 18 | "role": "Developer" 19 | } 20 | ], 21 | "require": { 22 | "laravel-enso/core": "^10.0", 23 | "laravel-enso/dynamic-methods": "^3.0", 24 | "laravel-enso/permissions": "^5.0" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "LaravelEnso\\ActionLogger\\": "src/" 29 | } 30 | }, 31 | "extra": { 32 | "laravel": { 33 | "providers": [ 34 | "LaravelEnso\\ActionLogger\\AppServiceProvider" 35 | ], 36 | "aliases": {} 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /database/migrations/2017_01_01_130000_create_action_logs_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 13 | 14 | $table->integer('user_id')->unsigned()->index(); 15 | $table->foreign('user_id')->references('id')->on('users') 16 | ->onUpdate('cascade')->onDelete('cascade'); 17 | 18 | $table->string('url'); 19 | $table->string('route')->index(); 20 | $table->string('method'); 21 | 22 | $table->decimal('duration', 6, 3)->unsigned()->nullable()->index(); 23 | 24 | $table->timestamps(); 25 | 26 | $table->index('created_at'); 27 | }); 28 | } 29 | 30 | public function down() 31 | { 32 | Schema::dropIfExists('action_logs'); 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /src/AppServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton(ActionLogger::class); 13 | } 14 | 15 | public function boot(): void 16 | { 17 | $this->app['router']->aliasMiddleware('action-logger', ActionLogger::class); 18 | 19 | $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Dynamics/ActionLogs.php: -------------------------------------------------------------------------------- 1 | $user->hasMany(ActionLog::class); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Http/Middleware/ActionLogger.php: -------------------------------------------------------------------------------- 1 | $request->user()->id, 26 | 'url' => $request->url(), 27 | 'route' => $request->route()->getName(), 28 | 'method' => $request->method(), 29 | 'duration' => min(999.999, microtime(true) - LARAVEL_START), 30 | ]); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Models/ActionLog.php: -------------------------------------------------------------------------------- 1 | belongsTo(User::class); 16 | } 17 | 18 | public function permission() 19 | { 20 | return $this->belongsTo(Permission::class, 'route', 'name'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/features/ActionLoggerTest.php: -------------------------------------------------------------------------------- 1 | seed()->actingAs( 21 | $this->user = User::first() 22 | ); 23 | } 24 | 25 | /** @test */ 26 | public function logs_action() 27 | { 28 | $this->get(route(self::Route, $this->user->id, false)) 29 | ->assertStatus(200); 30 | 31 | $actionLog = ActionLog::latest()->first(); 32 | 33 | tap($this)->assertEquals($actionLog->user_id, $this->user->id) 34 | ->assertEquals($actionLog->route, self::Route); 35 | } 36 | } 37 | --------------------------------------------------------------------------------