├── .gitignore ├── Config └── Tracert.php ├── LICENSE ├── README.md ├── composer.json ├── database └── Migrations │ └── 2015_05_12_121200_create_tracert_history_table.php └── src ├── Facades └── Tracert.php ├── Helpers.php ├── Models └── History.php ├── Tracert.php └── TracertServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | /bootstrap/compiled.php 2 | .env.*.php 3 | .env.php 4 | .env 5 | -------------------------------------------------------------------------------- /Config/Tracert.php: -------------------------------------------------------------------------------- 1 | 'history', 12 | 13 | /** 14 | * Available char sets for this package 15 | * 16 | */ 17 | 'char_sets' => [ 18 | 'hash' => 'ABCDEFGHIJKLMNOPQRSTUVWabcdefghijklmnopqrstuvw0123456789', 19 | ], 20 | 21 | ]; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Joren Van Hocht 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tracert 2 | 3 | Tracert is a package for Laravel to log actions of your users to your database. 4 | 5 | ## Installation 6 | 7 | ### Composer 8 | You can require this package through composer, just run the following line in your terminal. 9 | 10 | ```php 11 | composer require jorenvanhocht/tracert 1.3-beta 12 | ``` 13 | 14 | ### Service Provider 15 | Add the following line to the providers array in config/app.php 16 | 17 | ```php 18 | 'jorenvanhocht\Tracert\TracertServiceProvider', 19 | ``` 20 | 21 | If you want you can also add the facade to the aliases array in config/app.php 22 | 23 | >**Note**: this is not required, you can make use of available helper method wich is faster. 24 | 25 | ```php 26 | 'Tracert' => 'jorenvanhocht\Blogify\Facades\Tracert', 27 | ``` 28 | 29 | ### Composer update 30 | To be sure everything is loaded properly run ```composer update``` from your terminal. 31 | 32 | ### Publish config file 33 | If you want you can publish the config file by running the following command from your terminal 34 | 35 | ```php 36 | php artisan vendor:publish --tag="config" 37 | ``` 38 | 39 | ### Migrations 40 | This package contains a migration file to create the table where all actions will be logged into. Run it by the following command: 41 | 42 | ```php 43 | php artisan migrate --path="vendor/jorenvanhocht/Tracert/database/Migrations" 44 | ``` 45 | 46 | ## Configuration 47 | When you have published the config file you can change the name of the database table where all actions will be logged into. 48 | 49 | The config file is located at config/Tracert.php. 50 | 51 | ## Usage 52 | 53 | ### Log an action 54 | 55 | ```php 56 | tracert()->log('Model', 'row', 'user_id', 'Action'); 57 | ``` 58 | 59 | ### Retrieve actions for your activity feed 60 | This package contains a model so you can just retrieve data like you would always do using Eloquent. 61 | 62 | ```php 63 | use jorenvanhocht\Tracert\Models\History; 64 | 65 | History::all(); 66 | History::whereUserId(1); 67 | History::whereTable('table_name'); 68 | 69 | ... 70 | 71 | ``` 72 | 73 | ## Issues 74 | 75 | If you find any issues pleas report them so I can fix them. 76 | 77 | 78 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jorenvanhocht/tracert", 3 | "description": "Laravel package to log the users actions in the database", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Joren Van Hocht", 8 | "email": "vanhochtjoren@gmail.com" 9 | } 10 | ], 11 | "require": {}, 12 | "autoload": { 13 | "psr-4": { 14 | "jorenvanhocht\\Tracert\\": "src/" 15 | }, 16 | "files": [ 17 | "src/Helpers.php" 18 | ] 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /database/Migrations/2015_05_12_121200_create_tracert_history_table.php: -------------------------------------------------------------------------------- 1 | config = objectify(config('Tracert')); 13 | } 14 | 15 | /** 16 | * Run the migrations. 17 | * 18 | * @return void 19 | */ 20 | public function up() 21 | { 22 | Schema::create($this->config->table, function($table) 23 | { 24 | $table->increments('id'); 25 | $table->string('hash', 80)->unique(); 26 | $table->string('crud_action', 10); 27 | $table->string('table', 30); 28 | $table->integer('row'); 29 | $table->boolean('last'); 30 | $table->integer('user_id')->unsigned(); 31 | $table->timestamps(); 32 | }); 33 | } 34 | 35 | /** 36 | * Reverse the migrations. 37 | * 38 | * @return void 39 | */ 40 | public function down() 41 | { 42 | Schema::dropIfExists($this->config->table); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/Facades/Tracert.php: -------------------------------------------------------------------------------- 1 | table = config('Tracert')['table']; 30 | } 31 | 32 | /* 33 | |-------------------------------------------------------------------------- 34 | | Scopes 35 | |-------------------------------------------------------------------------- 36 | | 37 | | For more information pleas check out the official Laravel docs at 38 | | http://laravel.com/docs/5.0/eloquent#query-scopes 39 | | 40 | */ 41 | 42 | /** 43 | * @param $query 44 | * @param $user_id 45 | * @return mixed 46 | */ 47 | public function scopeOnUser($query, $user_id) 48 | { 49 | return $query->whereUserId($user_id); 50 | } 51 | 52 | /** 53 | * @param $query 54 | * @param $table 55 | * @param $row 56 | * @return mixed 57 | */ 58 | public function scopeOnTableRow($query, $table, $row) 59 | { 60 | return $query->whereTable($table) 61 | ->whereRow($row); 62 | } 63 | 64 | /** 65 | * @param $query 66 | * @param $from 67 | * @param $to 68 | * @return mixed 69 | */ 70 | public function scopeBetweenDates($query, $from, $to) 71 | { 72 | return $query->where('created_at', '>=', $from) 73 | ->where('created_at', '<=', $to); 74 | } 75 | 76 | /* 77 | |-------------------------------------------------------------------------- 78 | | Accessors & Mutators 79 | |-------------------------------------------------------------------------- 80 | | 81 | | For more information pleas check out the official Laravel docs at 82 | | http://laravel.com/docs/5.0/eloquent#accessors-and-mutators 83 | | 84 | */ 85 | 86 | public function getCreatedAtAttribute($value) 87 | { 88 | return Carbon::createFromTimestamp(strtotime($value))->diffForHumans(); 89 | } 90 | } -------------------------------------------------------------------------------- /src/Tracert.php: -------------------------------------------------------------------------------- 1 | db = $db->connection(); 31 | $this->history = $history; 32 | $this->config = objectify(config('Tracert')); 33 | } 34 | 35 | 36 | /** 37 | * Log an action to the database 38 | * 39 | * @param $model 40 | * @param $row 41 | * @param $user_id 42 | * @param string $action 43 | */ 44 | public function log($model, $row, $user_id, $action = 'created') 45 | { 46 | $history = new History; 47 | 48 | $history->hash = $this->makeUniqueHash($this->config->table, 'hash'); 49 | $history->table = $model; 50 | $history->row = $row; 51 | $history->user_id = $user_id; 52 | $history->crud_action = $action; 53 | 54 | $history->save(); 55 | } 56 | 57 | /////////////////////////////////////////////////////////////////////////// 58 | // Helper methods 59 | /////////////////////////////////////////////////////////////////////////// 60 | 61 | /** 62 | * @param $table 63 | * @param $field 64 | * @param int $min_length 65 | * @param int $max_length 66 | * @return string 67 | */ 68 | public function makeUniqueHash( $table, $field, $min_length = 5, $max_length = 20 ) 69 | { 70 | $hash = ''; 71 | $minus = 0; 72 | 73 | // Generate a random length for the hash between the given min and max length 74 | $rand = rand($min_length, $max_length); 75 | 76 | for ( $i = 0; $i < $rand; $i++ ) 77 | { 78 | $char = rand( 0, strlen( $this->config->char_sets->hash)); 79 | 80 | // When it's not the first char from the char_set make $minus equal to 1 81 | if( $char != 0 ? $minus = 1 : $minus = 0 ); 82 | 83 | // Add the character to the hash 84 | $hash .= $this->config->char_sets->hash[ $char - $minus ]; 85 | } 86 | 87 | // Check if the hash doest not exist in the given table and column 88 | if ( ! $this->db->table($table)->where($field, '=', $hash)->get() ) 89 | { 90 | return $hash; 91 | } 92 | 93 | $this->makeUniqueHash($table, $field, $min_length, $max_length); 94 | } 95 | 96 | } -------------------------------------------------------------------------------- /src/TracertServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind('jorenvanhocht.tracert', function() 17 | { 18 | $db = $this->app['db']; 19 | $model = new History; 20 | return new Tracert($db, $model); 21 | }); 22 | } 23 | 24 | /** 25 | * Load the resources 26 | * 27 | */ 28 | public function boot() 29 | { 30 | // Publish the config file 31 | $this->publishes([ 32 | __DIR__.'/../Config' => config_path(), 33 | ], 'config'); 34 | 35 | // Make the config file accessible even when the files are not published 36 | $this->mergeConfigFrom(__DIR__.'/../Config/Tracert.php', 'Tracert'); 37 | } 38 | 39 | } --------------------------------------------------------------------------------