Files
portal/app/app/Models/EmailVerification.php
T

72 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;
/**
* Код подтверждения почты при самозаписи клиента (G1/SP1).
*
* 6-значный код, bcrypt-хеш в code_hash, plain уходит письмом. TTL 15 мин,
* 5 попыток. Механика зеркалит ImpersonationToken. Таблица RLS-изолирована
* (через user_id→users.tenant_id) — на публичном роуте читается/пишется через
* BYPASSRLS pgsql_supplier.
*
* @property int $id
* @property int $user_id
* @property string $email
* @property string $token
* @property string|null $code_hash
* @property int $failed_attempts
* @property Carbon $expires_at
* @property Carbon|null $verified_at
* @property Carbon $created_at
*/
class EmailVerification extends Model
{
/** schema-таблица не имеет updated_at. */
public const UPDATED_AT = null;
protected $fillable = [
'user_id',
'email',
'token',
'code_hash',
'failed_attempts',
'expires_at',
'verified_at',
];
protected function casts(): array
{
return [
'failed_attempts' => 'integer',
'expires_at' => 'datetime',
'verified_at' => 'datetime',
'created_at' => 'datetime',
];
}
public function isExpired(): bool
{
return $this->expires_at->isPast();
}
public function isUsable(): bool
{
return $this->verified_at === null
&& $this->failed_attempts < 5
&& ! $this->isExpired();
}
/** @return BelongsTo<User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}