Fix: index.php Ajout des classes du model Admin, AdminGateway ainsi que User et UserGateway.correction_routeur
parent
0877094043
commit
01b52cd301
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace model;
|
||||
|
||||
class AdminGateway
|
||||
{
|
||||
private \PDO $con;
|
||||
|
||||
public function __construct(\PDO $con)
|
||||
{
|
||||
$this->con=$con;
|
||||
}
|
||||
|
||||
public function login(string $username, string $password): bool
|
||||
{
|
||||
$sql = "SELECT * FROM admin WHERE username=:username";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->bindValue(':username', $username);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch();
|
||||
if ($result && password_verify($password, $result['password'])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public function getHashedPassword(string $username): string
|
||||
{
|
||||
$sql = "SELECT password FROM user WHERE username=:username";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->bindValue(':username', $username);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch();
|
||||
return $result['password'];
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace model;
|
||||
|
||||
class UserGateway
|
||||
{
|
||||
private \PDO $con;
|
||||
private \PDOStatement $stmt;
|
||||
public function __construct(\PDO $con, \PDOStatement $stmt)
|
||||
{
|
||||
$this->con=$con;
|
||||
$this->stmt=$stmt;
|
||||
}
|
||||
|
||||
public function login(string $username, string $password): bool
|
||||
{
|
||||
$sql = "SELECT * FROM user WHERE username=:username";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->bindValue(':username', $username);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch();
|
||||
if ($result && password_verify($password, $result['password'])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public function addUser(string $username, string $password): void
|
||||
{
|
||||
$sql = "INSERT INTO user (username, password) VALUES (:username, :password)";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->bindValue(':username', $username);
|
||||
$stmt->bindValue(':password', $password);
|
||||
$stmt->execute();
|
||||
}
|
||||
public function deleteUser(int $id): void
|
||||
{
|
||||
$sql = "DELETE FROM user WHERE id=:id";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->bindValue(':id', $id);
|
||||
$stmt->execute();
|
||||
}
|
||||
public function updateUser(int $id, string $username, string $password): void
|
||||
{
|
||||
$sql = "UPDATE user SET username=:username, password=:password WHERE id=:id";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->bindValue(':id', $id);
|
||||
$stmt->bindValue(':username', $username);
|
||||
$stmt->bindValue(':password', $password);
|
||||
$stmt->execute();
|
||||
}
|
||||
public function getUser(int $id): User
|
||||
{
|
||||
$sql = "SELECT * FROM user WHERE id=:id";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->bindValue(':id', $id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch();
|
||||
return new User($result['id'], $result['username'], $result['password']);
|
||||
}
|
||||
public function getUsers(): array
|
||||
{
|
||||
$sql = "SELECT * FROM user";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetchAll();
|
||||
$users = [];
|
||||
foreach ($result as $user) {
|
||||
$users[] = new User($user['id'], $user['username'], $user['password']);
|
||||
}
|
||||
return $users;
|
||||
}
|
||||
public function getHashedPasswordById(int $id): string
|
||||
{
|
||||
$sql = "SELECT password FROM user WHERE id=:id";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->bindValue(':id', $id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch();
|
||||
return $result['password'];
|
||||
}
|
||||
public function getUserId(string $username): int
|
||||
{
|
||||
$sql = "SELECT id FROM user WHERE username=:username";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->bindValue(':username', $username);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch();
|
||||
return $result['id'];
|
||||
}
|
||||
public function getUserByUsernameAndPassword(string $username, string $password): User
|
||||
{
|
||||
$sql = "SELECT * FROM user WHERE username=:username AND password=:password";
|
||||
$stmt = $this->con->prepare($sql);
|
||||
$stmt->bindValue(':username', $username);
|
||||
$stmt->bindValue(':password', $password);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch();
|
||||
return new User($result['id'], $result['username'], $result['password']);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace model;
|
||||
|
||||
class Admin extends User
|
||||
{
|
||||
private string $email;
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param string $email
|
||||
*/
|
||||
public function __construct(int $id, string $username, string $password, string $email)
|
||||
{
|
||||
parent::__construct($id, $username, $password);
|
||||
$this->email = $email;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail(): string
|
||||
{
|
||||
return parent::getUsername();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
*/
|
||||
public function setEmail(string $email): void
|
||||
{
|
||||
parent::setUsername($email);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace model;
|
||||
|
||||
class User
|
||||
{
|
||||
private int $id;
|
||||
private string $username;
|
||||
private string $password;
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
*/
|
||||
|
||||
public function __construct(int $id, string $username, string $password)
|
||||
{
|
||||
$this->id=$id;
|
||||
$this->username=$username;
|
||||
$this->password=$password;
|
||||
}
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
public function getUsername(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
public function setUsername(string $username): void
|
||||
{
|
||||
$this->username=$username;
|
||||
}
|
||||
public function setPassword(string $password): void
|
||||
{
|
||||
$this->password=$password;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue