diff --git a/project/src/index.php b/project/src/index.php index 81dc0eb..521985c 100644 --- a/project/src/index.php +++ b/project/src/index.php @@ -4,6 +4,7 @@ test 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']; + } +} \ No newline at end of file diff --git a/project/src/model/gateways/UserGateway.php b/project/src/model/gateways/UserGateway.php new file mode 100644 index 0000000..7db3876 --- /dev/null +++ b/project/src/model/gateways/UserGateway.php @@ -0,0 +1,100 @@ +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']); + } +} diff --git a/project/src/model/metier/Admin.php b/project/src/model/metier/Admin.php new file mode 100644 index 0000000..30a40cb --- /dev/null +++ b/project/src/model/metier/Admin.php @@ -0,0 +1,36 @@ +email = $email; + } + + /** + * @return string + */ + public function getEmail(): string + { + return parent::getUsername(); + } + + /** + * @param string $email + */ + public function setEmail(string $email): void + { + parent::setUsername($email); + } +} \ No newline at end of file diff --git a/project/src/model/metier/Theme.php b/project/src/model/metier/Theme.php index 5900456..e28e6ea 100644 --- a/project/src/model/metier/Theme.php +++ b/project/src/model/metier/Theme.php @@ -7,4 +7,5 @@ enum Theme case Maths; case Physics; case Chemistry; + case Biology; } \ No newline at end of file diff --git a/project/src/model/metier/User.php b/project/src/model/metier/User.php new file mode 100644 index 0000000..5974c9e --- /dev/null +++ b/project/src/model/metier/User.php @@ -0,0 +1,39 @@ +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; + } +} \ No newline at end of file