190 lines
6.0 KiB
PHP
190 lines
6.0 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../UserProviderInterface.php';
|
|
require_once __DIR__ . '/../User.php';
|
|
|
|
/**
|
|
* SqliteUserProvider stores users in SQLite database
|
|
*/
|
|
class SqliteUserProvider implements UserProviderInterface
|
|
{
|
|
private PDO $db;
|
|
private string $table;
|
|
|
|
public function __construct(string $databasePath, string $table = 'users')
|
|
{
|
|
$this->table = $table;
|
|
|
|
try {
|
|
$this->db = new PDO("sqlite:$databasePath");
|
|
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
$this->createTable();
|
|
} catch (PDOException $e) {
|
|
throw new Exception("Failed to connect to SQLite database: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create users table if it doesn't exist
|
|
*/
|
|
private function createTable(): void
|
|
{
|
|
$sql = "CREATE TABLE IF NOT EXISTS {$this->table} (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username VARCHAR(255) UNIQUE,
|
|
email VARCHAR(255) UNIQUE,
|
|
password VARCHAR(255) NOT NULL,
|
|
remember_token VARCHAR(255),
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
attributes TEXT
|
|
)";
|
|
|
|
$this->db->exec($sql);
|
|
}
|
|
|
|
/**
|
|
* Map database row to User object
|
|
*/
|
|
private function mapToUser(array $row): User
|
|
{
|
|
$attributes = isset($row['attributes']) ? json_decode($row['attributes'], true) ?? [] : [];
|
|
|
|
return new User([
|
|
'id' => $row['id'],
|
|
'username' => $row['username'],
|
|
'email' => $row['email'],
|
|
'password' => $row['password'],
|
|
'rememberToken' => $row['remember_token'],
|
|
'createdAt' => $row['created_at'],
|
|
'updatedAt' => $row['updated_at'],
|
|
'attributes' => $attributes
|
|
]);
|
|
}
|
|
|
|
public function findById(int|string $id): ?User
|
|
{
|
|
$stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE id = :id");
|
|
$stmt->execute(['id' => $id]);
|
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
return $row ? $this->mapToUser($row) : null;
|
|
}
|
|
|
|
public function findByEmail(string $email): ?User
|
|
{
|
|
$stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE email = :email");
|
|
$stmt->execute(['email' => $email]);
|
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
return $row ? $this->mapToUser($row) : null;
|
|
}
|
|
|
|
public function findByUsername(string $username): ?User
|
|
{
|
|
$stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE username = :username");
|
|
$stmt->execute(['username' => $username]);
|
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
return $row ? $this->mapToUser($row) : null;
|
|
}
|
|
|
|
public function findByCredentials(string $identifier): ?User
|
|
{
|
|
$stmt = $this->db->prepare(
|
|
"SELECT * FROM {$this->table} WHERE email = :identifier OR username = :identifier"
|
|
);
|
|
$stmt->execute(['identifier' => $identifier]);
|
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
return $row ? $this->mapToUser($row) : null;
|
|
}
|
|
|
|
public function findByRememberToken(string $token): ?User
|
|
{
|
|
$stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE remember_token = :token");
|
|
$stmt->execute(['token' => $token]);
|
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
return $row ? $this->mapToUser($row) : null;
|
|
}
|
|
|
|
public function create(array $data): User
|
|
{
|
|
$attributes = $data['attributes'] ?? [];
|
|
unset($data['attributes']);
|
|
|
|
$stmt = $this->db->prepare(
|
|
"INSERT INTO {$this->table} (username, email, password, remember_token, attributes, created_at, updated_at)
|
|
VALUES (:username, :email, :password, :remember_token, :attributes, :created_at, :updated_at)"
|
|
);
|
|
|
|
$stmt->execute([
|
|
'username' => $data['username'] ?? null,
|
|
'email' => $data['email'] ?? null,
|
|
'password' => $data['password'],
|
|
'remember_token' => $data['remember_token'] ?? null,
|
|
'attributes' => json_encode($attributes),
|
|
'created_at' => $data['created_at'] ?? date('Y-m-d H:i:s'),
|
|
'updated_at' => $data['updated_at'] ?? date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
$data['id'] = $this->db->lastInsertId();
|
|
$data['attributes'] = $attributes;
|
|
|
|
return new User($data);
|
|
}
|
|
|
|
public function update(User $user, array $data): bool
|
|
{
|
|
$attributes = array_merge($user->attributes, $data['attributes'] ?? []);
|
|
unset($data['attributes']);
|
|
|
|
$fields = [];
|
|
$params = ['id' => $user->id];
|
|
|
|
foreach (['username', 'email', 'password'] as $field) {
|
|
if (isset($data[$field])) {
|
|
$fields[] = "$field = :$field";
|
|
$params[$field] = $data[$field];
|
|
$user->$field = $data[$field];
|
|
}
|
|
}
|
|
|
|
if (!empty($attributes)) {
|
|
$fields[] = "attributes = :attributes";
|
|
$params['attributes'] = json_encode($attributes);
|
|
$user->attributes = $attributes;
|
|
}
|
|
|
|
if (empty($fields)) {
|
|
return true;
|
|
}
|
|
|
|
$fields[] = "updated_at = :updated_at";
|
|
$params['updated_at'] = date('Y-m-d H:i:s');
|
|
|
|
$sql = "UPDATE {$this->table} SET " . implode(', ', $fields) . " WHERE id = :id";
|
|
$stmt = $this->db->prepare($sql);
|
|
|
|
return $stmt->execute($params);
|
|
}
|
|
|
|
public function updateRememberToken(User $user, ?string $token): bool
|
|
{
|
|
$stmt = $this->db->prepare(
|
|
"UPDATE {$this->table} SET remember_token = :token, updated_at = :updated_at WHERE id = :id"
|
|
);
|
|
|
|
return $stmt->execute([
|
|
'id' => $user->id,
|
|
'token' => $token,
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
}
|
|
|
|
public function verifyPassword(User $user, string $password): bool
|
|
{
|
|
return password_verify($password, $user->password);
|
|
}
|
|
} |