From 4a44dbc1c178d6d1120dbcf02a2e1023960ac91a Mon Sep 17 00:00:00 2001 From: Juan Felipe Duarte <70235683+DuarteJFelipe@users.noreply.github.com> Date: Sat, 25 Oct 2025 09:18:56 -0500 Subject: [PATCH] Implement device and IP tracking for users Added Device and UserIp models and migrations to store device fingerprints and user IPs with expiration. Updated ShowSavingIp Livewire component and view to capture and persist both IP and device fingerprint. Introduced a scheduled command to clean expired user IPs and registered it in the console kernel. Removed the old migration for storing IPs directly on the users table. --- app/Console/Commands/CleanExpiredUserIps.php | 18 ++++++++ app/Console/Kernel.php | 10 +++-- app/Http/Livewire/ShowSavingIp.php | 41 ++++++++++++++++--- app/Models/Device.php | 13 ++++++ app/Models/UserIp.php | 20 +++++++++ ...025_10_18_114653_add_ip_to_users_table.php | 22 ---------- ...025_10_24_170544_create_user_ips_table.php | 28 +++++++++++++ ...2025_10_24_171321_create_devices_table.php | 26 ++++++++++++ .../views/livewire/show-saving-ip.blade.php | 32 ++++++++++----- 9 files changed, 168 insertions(+), 42 deletions(-) create mode 100644 app/Console/Commands/CleanExpiredUserIps.php create mode 100644 app/Models/Device.php create mode 100644 app/Models/UserIp.php delete mode 100644 database/migrations/2025_10_18_114653_add_ip_to_users_table.php create mode 100644 database/migrations/2025_10_24_170544_create_user_ips_table.php create mode 100644 database/migrations/2025_10_24_171321_create_devices_table.php diff --git a/app/Console/Commands/CleanExpiredUserIps.php b/app/Console/Commands/CleanExpiredUserIps.php new file mode 100644 index 0000000..b7f8356 --- /dev/null +++ b/app/Console/Commands/CleanExpiredUserIps.php @@ -0,0 +1,18 @@ +delete(); + $this->info("Se eliminaron {$count} IPs expiradas."); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 3ece9a7..2ab4431 100755 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -30,11 +30,13 @@ class Kernel extends ConsoleKernel protected function schedule(Schedule $schedule) { + $schedule->command('userips:clean')->hourly(); + // $schedule->exec('nohup php /home/u111286300/domains/sirpremium.com.co/public_html/app/artisan queue:work --sleep=3 --tries=3 > /dev/null 2>&1 &')->everyMinute(); - + // $schedule->exec('nohup php /var/www/sirpremium/artisan queue:work --sleep=3 --tries=3 > /dev/null 2>&1 &')->everyMinute(); $schedule->command('telescope:prune --hours=24')->daily(); - + $schedule->command('tarea:validarCompra')->everyFiveMinutes(); @@ -100,10 +102,10 @@ class Kernel extends ConsoleKernel //curl_close($curl); //return $response; // if ($err) { - // + // //} else { - // + // //} diff --git a/app/Http/Livewire/ShowSavingIp.php b/app/Http/Livewire/ShowSavingIp.php index c41b3d5..3b3b9d0 100644 --- a/app/Http/Livewire/ShowSavingIp.php +++ b/app/Http/Livewire/ShowSavingIp.php @@ -4,24 +4,53 @@ namespace App\Http\Livewire; use Livewire\Component; use Illuminate\Support\Facades\Auth; +use App\Models\UserIp; +use Carbon\Carbon; +use App\Models\Device; class ShowSavingIp extends Component { public $ip_address; + public $device_fingerprint; - protected $listeners = ['setIpAddress']; + protected $listeners = ['setDeviceData']; - public function setIpAddress($ip) + public function setDeviceData($data) { - $this->ip_address = $ip; + $this->ip_address = $data['ip'] ?? null; + $this->device_fingerprint = $data['fingerprint'] ?? null; $user = Auth::user(); - if ($user) { - $user->ip_address = $ip; - $user->save(); + if (!$user) return; + + // Guardar IP pública temporal (12h) + if ($this->ip_address) { + UserIp::updateOrCreate( + [ + 'user_id' => $user->id, + 'ip_address' => $this->ip_address, + ], + [ + 'expires_at' => Carbon::now()->addHours(12), + ] + ); + } + + // Guardar fingerprint permanente del dispositivo + if ($this->device_fingerprint) { + Device::updateOrCreate( + [ + 'fingerprint' => $this->device_fingerprint, + ], + [ + 'user_id' => $user->id, + 'last_seen_at' => now(), + ] + ); } } + public function render() { return view('livewire.show-saving-ip'); diff --git a/app/Models/Device.php b/app/Models/Device.php new file mode 100644 index 0000000..841048f --- /dev/null +++ b/app/Models/Device.php @@ -0,0 +1,13 @@ +belongsTo(User::class); + } +} diff --git a/database/migrations/2025_10_18_114653_add_ip_to_users_table.php b/database/migrations/2025_10_18_114653_add_ip_to_users_table.php deleted file mode 100644 index 4f3c3da..0000000 --- a/database/migrations/2025_10_18_114653_add_ip_to_users_table.php +++ /dev/null @@ -1,22 +0,0 @@ -string('ip_address')->nullable(); - }); - } - - public function down() - { - Schema::table('users', function (Blueprint $table) { - $table->dropColumn('ip_address'); - }); - } -}; diff --git a/database/migrations/2025_10_24_170544_create_user_ips_table.php b/database/migrations/2025_10_24_170544_create_user_ips_table.php new file mode 100644 index 0000000..83921ad --- /dev/null +++ b/database/migrations/2025_10_24_170544_create_user_ips_table.php @@ -0,0 +1,28 @@ +id(); + + $table->foreignId('user_id')->constrained()->onDelete('cascade'); + $table->string('ip_address'); + $table->timestamp('expires_at')->nullable(); + + $table->timestamps(); + + $table->index('ip_address'); + }); + } + + public function down() + { + Schema::dropIfExists('user_ips'); + } +}; diff --git a/database/migrations/2025_10_24_171321_create_devices_table.php b/database/migrations/2025_10_24_171321_create_devices_table.php new file mode 100644 index 0000000..1090924 --- /dev/null +++ b/database/migrations/2025_10_24_171321_create_devices_table.php @@ -0,0 +1,26 @@ +id(); + + $table->foreignId('user_id')->nullable()->constrained()->onDelete('cascade'); + $table->string('fingerprint')->unique(); + $table->timestamp('last_seen_at')->nullable(); + + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('devices'); + } +}; diff --git a/resources/views/livewire/show-saving-ip.blade.php b/resources/views/livewire/show-saving-ip.blade.php index d57db75..15410cf 100644 --- a/resources/views/livewire/show-saving-ip.blade.php +++ b/resources/views/livewire/show-saving-ip.blade.php @@ -1,23 +1,35 @@
@if($ip_address)
- Tu IP actual: {{ $ip_address }}
+ Tu IP actual: {{ $ip_address }}
+ ID del dispositivo: {{ $device_fingerprint }}
@else
- Obteniendo IP...
+ Obteniendo IP y dispositivo...
@endif