Skip to content

Commit f6155b5

Browse files
committed
Now generating new user IDs at random
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.
1 parent 9e980fe commit f6155b5

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

app/Models/User.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
9+
use Illuminate\Support\Str;
910

1011
class User extends Authenticatable implements MustVerifyEmail
1112
{
@@ -25,7 +26,6 @@ class User extends Authenticatable implements MustVerifyEmail
2526
'provider_id',
2627
'email_verified_at',
2728
'littlelink_name',
28-
2929
];
3030

3131
/**
@@ -51,8 +51,29 @@ public function visits()
5151
{
5252
return visits($this)->relation();
5353
}
54+
5455
public function socialAccounts()
5556
{
56-
return $this->hasMany(socialAccount::class);
57+
return $this->hasMany(SocialAccount::class);
58+
}
59+
60+
protected static function boot()
61+
{
62+
parent::boot();
63+
64+
static::creating(function ($user) {
65+
if (config('linkstack.disable_random_user_ids') != 'true') {
66+
$numberOfDigits = config('linkstack.user_id_length') ?? 6;
67+
68+
$minIdValue = 10**($numberOfDigits - 1);
69+
$maxIdValue = 10**$numberOfDigits - 1;
70+
71+
do {
72+
$randomId = rand($minIdValue, $maxIdValue);
73+
} while (User::find($randomId));
74+
75+
$user->id = $randomId;
76+
}
77+
});
5778
}
5879
}

0 commit comments

Comments
 (0)