From f6155b595940809648e1b32a9309184547ce400d Mon Sep 17 00:00:00 2001 From: Julian Prieber <60265788+JulianPrieber@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:00:46 +0100 Subject: [PATCH] Now generating new user IDs at random MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Default is length is 6 digits. This can be changed in the linkstack.php config file in /config with the key user_id_length. Sequential numbering can still be used by setting disable_random_user_ids to true. --- app/Models/User.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 8d7e618..cc56e1d 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Str; class User extends Authenticatable implements MustVerifyEmail { @@ -25,7 +26,6 @@ class User extends Authenticatable implements MustVerifyEmail 'provider_id', 'email_verified_at', 'littlelink_name', - ]; /** @@ -51,8 +51,29 @@ class User extends Authenticatable implements MustVerifyEmail { return visits($this)->relation(); } + public function socialAccounts() { - return $this->hasMany(socialAccount::class); + return $this->hasMany(SocialAccount::class); + } + + protected static function boot() + { + parent::boot(); + + static::creating(function ($user) { + if (config('linkstack.disable_random_user_ids') != 'true') { + $numberOfDigits = config('linkstack.user_id_length') ?? 6; + + $minIdValue = 10**($numberOfDigits - 1); + $maxIdValue = 10**$numberOfDigits - 1; + + do { + $randomId = rand($minIdValue, $maxIdValue); + } while (User::find($randomId)); + + $user->id = $randomId; + } + }); } }