DONE: digital card with profile display: WIP: membership condition on letter, is_first_time column in users table
This commit is contained in:
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Spatie\Activitylog\LogOptions;
|
||||
use Spatie\Activitylog\Traits\LogsActivity;
|
||||
@@ -51,6 +52,7 @@ class User extends Authenticatable
|
||||
'marriage_status',
|
||||
'member_number',
|
||||
'member_type',
|
||||
'public_profile_token',
|
||||
'join_date',
|
||||
'birth_date',
|
||||
'birth_place',
|
||||
@@ -67,8 +69,55 @@ class User extends Authenticatable
|
||||
'two_factor_secret',
|
||||
'two_factor_recovery_codes',
|
||||
'two_factor_confirmed_at',
|
||||
'public_profile_token',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(function (User $user) {
|
||||
$expiryDays = self::publicProfileTokenTtlDays();
|
||||
|
||||
if (empty($user->public_profile_token)) {
|
||||
$user->public_profile_token = Str::random(48);
|
||||
}
|
||||
|
||||
if (empty($user->public_profile_token_expires_at)) {
|
||||
$user->public_profile_token_expires_at = now()->addDays($expiryDays);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function publicProfileTokenTtlDays(): int
|
||||
{
|
||||
return (int) config('user.public_profile_token_ttl_days', 90);
|
||||
}
|
||||
|
||||
public function isPublicProfileTokenExpired(): bool
|
||||
{
|
||||
return $this->public_profile_token_expires_at !== null
|
||||
&& $this->public_profile_token_expires_at->isPast();
|
||||
}
|
||||
|
||||
public function ensurePublicProfileToken(): string
|
||||
{
|
||||
$expiryDays = self::publicProfileTokenTtlDays();
|
||||
|
||||
if (blank($this->public_profile_token) || $this->isPublicProfileTokenExpired()) {
|
||||
$this->forceFill([
|
||||
'public_profile_token' => Str::random(48),
|
||||
'public_profile_token_expires_at' => now()->addDays($expiryDays),
|
||||
])->save();
|
||||
|
||||
return $this->public_profile_token;
|
||||
}
|
||||
|
||||
$this->forceFill([
|
||||
'public_profile_token_expires_at' => now()->addDays($expiryDays),
|
||||
])->save();
|
||||
|
||||
return $this->public_profile_token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
@@ -84,6 +133,7 @@ class User extends Authenticatable
|
||||
'marriage_status' => 'string',
|
||||
'member_number' => 'integer',
|
||||
'member_type' => 'string',
|
||||
'public_profile_token_expires_at' => 'datetime',
|
||||
'join_date' => 'date',
|
||||
'birth_date' => 'date',
|
||||
'birth_place' => 'string',
|
||||
|
||||
Reference in New Issue
Block a user