-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2620 Now tracking user ips that access the site - I can then fight b…
…ack against abusers of the site.
- Loading branch information
Showing
11 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use App\Models\User; | ||
use App\Models\UserIpAddress; | ||
use Auth; | ||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\DB; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class TracksUserIpAddress | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
*/ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
// Maybe this should be handled differently? Idk how heavy these queries will be | ||
if (!$request->ajax() && Auth::check()) { | ||
/** @var User $user */ | ||
$user = Auth::user(); | ||
UserIpAddress::upsert( | ||
[ | ||
'user_id' => $user->id, | ||
'ip_address' => $request->ip(), | ||
'count' => 1, // Default value for new rows | ||
'updated_at' => now(), // Example of tracking when a row is updated | ||
], | ||
['user_id', 'ip_address'], | ||
['count' => DB::raw('count + 1'), 'updated_at'] // Update these columns if a conflict occurs | ||
); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Eloquent; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Support\Carbon; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $user_id | ||
* @property string $ip_address | ||
* @property int $count | ||
* | ||
* @property Carbon $updated_at | ||
* @property Carbon $created_at | ||
* | ||
* @property User $user | ||
* | ||
* @mixin Eloquent | ||
*/ | ||
class UserIpAddress extends Model | ||
{ | ||
protected $fillable = [ | ||
'user_id', | ||
'ip_address', | ||
'count', | ||
'updated_at', | ||
'created_at', | ||
]; | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class, 'user_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
database/migrations/2024_11_20_151928_create_user_ip_addresses_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('user_ip_addresses', function (Blueprint $table) { | ||
$table->id(); | ||
$table->integer('user_id'); | ||
$table->integer('count'); | ||
$table->string('ip_address'); | ||
$table->timestamps(); | ||
|
||
$table->index(['user_id', 'ip_address']); | ||
$table->index(['ip_address']); | ||
$table->unique(['user_id', 'ip_address']); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('user_ip_addresses'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters