124 lines
3.4 KiB
PHP
124 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
use App\Models\Traits\TenantScopedModel;
|
|
|
|
class UserModel extends Model
|
|
{
|
|
use TenantScopedModel;
|
|
|
|
protected $table = 'users';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = false;
|
|
protected $protectFields = true;
|
|
protected $allowedFields = [
|
|
'email',
|
|
'password',
|
|
'name',
|
|
'phone',
|
|
'avatar',
|
|
// Поля для верификации email
|
|
'verification_token',
|
|
'email_verified',
|
|
'verified_at',
|
|
// Поля для восстановления пароля
|
|
'reset_token',
|
|
'reset_expires_at',
|
|
//системная роль
|
|
'system_role',
|
|
'status',
|
|
];
|
|
|
|
// Dates
|
|
protected $useTimestamps = true;
|
|
protected $dateFormat = 'datetime';
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
|
|
// Хеширование пароля перед вставкой
|
|
protected $beforeInsert = ['hashPassword'];
|
|
protected $beforeUpdate = ['hashPassword'];
|
|
|
|
protected function hashPassword(array $data)
|
|
{
|
|
if (isset($data['data']['password'])) {
|
|
$data['data']['password'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Генерация токена для сброса пароля
|
|
*
|
|
* @param int $userId ID пользователя
|
|
* @param int $expiresInHours Срок действия токена в часах (по умолчанию 24 часа)
|
|
* @return string Сгенерированный токен
|
|
*/
|
|
public function generateResetToken(int $userId, int $expiresInHours = 24): string
|
|
{
|
|
$token = bin2hex(random_bytes(32));
|
|
$expiresAt = date('Y-m-d H:i:s', strtotime("+{$expiresInHours} hours"));
|
|
|
|
$this->update($userId, [
|
|
'reset_token' => $token,
|
|
'reset_expires_at' => $expiresAt,
|
|
]);
|
|
|
|
return $token;
|
|
}
|
|
|
|
/**
|
|
* Проверка токена сброса пароля
|
|
*
|
|
* @param string $token Токен сброса
|
|
* @return array|null Данные пользователя или null если токен недействителен
|
|
*/
|
|
public function verifyResetToken(string $token): ?array
|
|
{
|
|
$user = $this->where('reset_token', $token)->first();
|
|
|
|
if (!$user) {
|
|
return null;
|
|
}
|
|
|
|
// Проверяем срок действия токена
|
|
if (empty($user['reset_expires_at'])) {
|
|
return null;
|
|
}
|
|
|
|
if (strtotime($user['reset_expires_at']) < time()) {
|
|
return null;
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* Очистка токена сброса пароля
|
|
*
|
|
* @param int $userId ID пользователя
|
|
* @return bool
|
|
*/
|
|
public function clearResetToken(int $userId): bool
|
|
{
|
|
return $this->update($userId, [
|
|
'reset_token' => null,
|
|
'reset_expires_at' => null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Поиск пользователя по email
|
|
*
|
|
* @param string $email Email адрес
|
|
* @return array|null
|
|
*/
|
|
public function findByEmail(string $email): ?array
|
|
{
|
|
return $this->where('email', $email)->first();
|
|
}
|
|
} |