145 lines
3.3 KiB
PHP
145 lines
3.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Modules\Auth;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Auth Model
|
||
|
|
* User authentication model
|
||
|
|
*/
|
||
|
|
class Model
|
||
|
|
{
|
||
|
|
private \PDO $pdo;
|
||
|
|
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->pdo = $this->getConnection();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get database connection
|
||
|
|
*/
|
||
|
|
private function getConnection(): \PDO
|
||
|
|
{
|
||
|
|
$config = include __DIR__ . '/../../Config/database.php';
|
||
|
|
$connection = $config['connections'][$config['default']];
|
||
|
|
|
||
|
|
$dsn = "mysql:host={$connection['host']};port={$connection['port']};dbname={$connection['database']};charset={$connection['charset']}";
|
||
|
|
|
||
|
|
return new \PDO($dsn, $connection['username'], $connection['password'], $connection['options']);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Find user by email
|
||
|
|
*/
|
||
|
|
public function findByEmail(string $email): ?array
|
||
|
|
{
|
||
|
|
$stmt = $this->pdo->prepare("SELECT * FROM users WHERE email = ?");
|
||
|
|
$stmt->execute([$email]);
|
||
|
|
|
||
|
|
$user = $stmt->fetch();
|
||
|
|
return $user ?: null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Find user by ID
|
||
|
|
*/
|
||
|
|
public function findById(int $id): ?array
|
||
|
|
{
|
||
|
|
$stmt = $this->pdo->prepare("SELECT * FROM users WHERE id = ?");
|
||
|
|
$stmt->execute([$id]);
|
||
|
|
|
||
|
|
$user = $stmt->fetch();
|
||
|
|
return $user ?: null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create new user
|
||
|
|
*/
|
||
|
|
public function create(array $data): int
|
||
|
|
{
|
||
|
|
$stmt = $this->pdo->prepare("
|
||
|
|
INSERT INTO users (name, email, password, created_at, updated_at)
|
||
|
|
VALUES (?, ?, ?, NOW(), NOW())
|
||
|
|
");
|
||
|
|
|
||
|
|
$stmt->execute([
|
||
|
|
$data['name'],
|
||
|
|
$data['email'],
|
||
|
|
password_hash($data['password'], PASSWORD_ARGON2ID)
|
||
|
|
]);
|
||
|
|
|
||
|
|
return $this->pdo->lastInsertId();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update user
|
||
|
|
*/
|
||
|
|
public function update(int $id, array $data): bool
|
||
|
|
{
|
||
|
|
$fields = [];
|
||
|
|
$values = [];
|
||
|
|
|
||
|
|
foreach ($data as $key => $value) {
|
||
|
|
if ($key !== 'id') {
|
||
|
|
$fields[] = "{$key} = ?";
|
||
|
|
$values[] = $value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (empty($fields)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$values[] = $id;
|
||
|
|
$sql = "UPDATE users SET " . implode(', ', $fields) . ", updated_at = NOW() WHERE id = ?";
|
||
|
|
|
||
|
|
$stmt = $this->pdo->prepare($sql);
|
||
|
|
return $stmt->execute($values);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Delete user
|
||
|
|
*/
|
||
|
|
public function delete(int $id): bool
|
||
|
|
{
|
||
|
|
$stmt = $this->pdo->prepare("DELETE FROM users WHERE id = ?");
|
||
|
|
return $stmt->execute([$id]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Verify password
|
||
|
|
*/
|
||
|
|
public function verifyPassword(string $password, string $hash): bool
|
||
|
|
{
|
||
|
|
return password_verify($password, $hash);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get all users
|
||
|
|
*/
|
||
|
|
public function all(): array
|
||
|
|
{
|
||
|
|
$stmt = $this->pdo->query("SELECT id, name, email, created_at FROM users ORDER BY created_at DESC");
|
||
|
|
return $stmt->fetchAll();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if email exists
|
||
|
|
*/
|
||
|
|
public function emailExists(string $email, ?int $excludeId = null): bool
|
||
|
|
{
|
||
|
|
$sql = "SELECT COUNT(*) FROM users WHERE email = ?";
|
||
|
|
$params = [$email];
|
||
|
|
|
||
|
|
if ($excludeId) {
|
||
|
|
$sql .= " AND id != ?";
|
||
|
|
$params[] = $excludeId;
|
||
|
|
}
|
||
|
|
|
||
|
|
$stmt = $this->pdo->prepare($sql);
|
||
|
|
$stmt->execute($params);
|
||
|
|
|
||
|
|
return $stmt->fetchColumn() > 0;
|
||
|
|
}
|
||
|
|
}
|