uggly push
continuous-integration/drone/push Build is passing Details

issue_021_Auth
David D'ALMEIDA 2 years ago
parent 62ddaa6359
commit 7a551b1e65

@ -3,7 +3,9 @@
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Hearttrack\\": "src/", "Hearttrack\\": "src/",
"Console\\":"src/Console" "Console\\":"src/console",
"Stub\\":"src/data/stub"
} }
}, },
"require": { "require": {

@ -0,0 +1,111 @@
<?php
namespace Utils;
use \Exception;
final class Validation {
/**
* Valide une action passée en paramètre.
*
* @param string $action L'action à valider.
* @return string L'action validée si elle est conforme.
* @throws Exception Si l'action n'est pas valide.
*/
public static function val_action($action) {
if (!isset($action) || !Validation::val_string($action)) {
throw new Exception("Pas d'action spécifiée.");
} else {
return $action;
}
}
/**
* Valide une chaîne de caractères.
*
* @param string $string La chaîne à valider.
* @return bool True si la chaîne est valide, sinon false.
* @throws Exception Si la chaîne n'est pas valide (tentative d'injection de code).
*/
public static function val_string(string $string) : bool {
if (filter_var($string, FILTER_SANITIZE_STRING) !== $string) {
throw new Exception("$string n'est pas valide. Tentative d'injection de code (attaque sécurité)!");
} else {
return true;
}
}
/**
* Valide un entier.
*
* @param int $int L'entier à valider.
* @return bool True si l'entier est valide, sinon false.
* @throws Exception Si l'entier n'est pas valide (tentative d'injection de code).
*/
public static function val_int(int $int) : bool {
if (filter_var($int, FILTER_SANITIZE_NUMBER_INT) !== $int) {
throw new Exception("$int n'est pas valide. Tentative d'injection de code (attaque sécurité)!");
} else {
return true;
}
}
/**
* Valide un mot de passe.
*
* @param string $password Le mot de passe à valider.
* @return bool True si le mot de passe est valide, sinon false.
* @throws Exception Si le mot de passe n'est pas valide.
*/
public static function val_password(string $password) : bool {
if ($password === null) {
throw new Exception("Le mot de passe ne peut être vide.");
} else {
if (!preg_match('/^.{6,}$/', $password)) {
throw new Exception("Le mot de passe n'est pas valide : au moins 6 caractères requis.");
}
return Validation::val_string($password);
}
}
/**
* Valide un booléen.
*
* @param bool $done La valeur booléenne à valider.
* @return bool True si la valeur booléenne est définie, sinon false.
*/
public static function val_bool(bool $done) : bool {
return isset($done);
}
/**
* Nettoie une chaîne de caractères.
*
* @param string $string La chaîne à nettoyer.
* @return string La chaîne nettoyée.
*/
public static function clean_string(string $string) : string {
return filter_var($string, FILTER_SANITIZE_STRING);
}
/**
* Nettoie un entier.
*
* @param int $int L'entier à nettoyer.
* @return int L'entier nettoyé.
*/
public static function clean_int(int $int) : int {
return filter_var($int, FILTER_SANITIZE_NUMBER_INT);
}
/**
* Nettoie une valeur booléenne.
*
* @param bool $bool La valeur booléenne à nettoyer.
* @return bool La valeur booléenne nettoyée.
*/
public static function clean_bool(bool $bool) : bool {
return filter_var($bool, FILTER_VALIDATE_BOOLEAN);
}
}
?>

@ -0,0 +1,8 @@
<?php
namespace Exception;
final class NotImplementedException extends \Exception
{
public $message = "Not implemented method call";
}

@ -1,3 +1,7 @@
/*You can add global styles in this files and also import other files. */
/* @import url("path/to/syles/files"); */
/* Here tailwind reserved config */
@tailwind base; @tailwind base;
@tailwind components; @tailwind components;
@tailwind utilities; @tailwind utilities;

@ -1,7 +1,9 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
use Manager\DataManager;
use Stub\StubData;
$model = new StubData();// Couche d'accès au model
function clearScreen() function clearScreen()
{ {
system('clear || cls'); system('clear || cls');
@ -94,8 +96,28 @@ function displaySettingsMenu() {
echo "0. Retour au menu principal\n"; echo "0. Retour au menu principal\n";
echo "Choisissez une option: "; echo "Choisissez une option: ";
} }
function loginUser(DataManager $model) {
echo "\nEntrez votre nom d'utilisateur: ";
$username = trim(fgets(STDIN));
echo "Entrez votre mot de passe: ";
$password = trim(fgets(STDIN));
if ($model->userMgr->login($username, $password)) {
return true;
} else {
echo "Erreur de connexion. Essayez encore.\n";
return false;
}
}
$tmpRep = true; function registerUser(DataManager $model) {
echo "\nEntrez votre nom d'utilisateur: ";
$username = trim(fgets(STDIN));
echo "Entrez votre mot de passe: ";
$password = trim(fgets(STDIN));
$model->userMgr->register($username, $password);
echo "Utilisateur enregistré avec succès!\n";
return true;
}
while (true) { while (true) {
displayAuthMenu(); displayAuthMenu();
@ -103,7 +125,7 @@ while (true) {
switch ($choice) { switch ($choice) {
case '1': // Se connecter case '1': // Se connecter
if ($tmpRep) { if (loginUser()) {
while (true) { while (true) {
displayMainMenu(); displayMainMenu();
$mainChoice = trim(fgets(STDIN)); $mainChoice = trim(fgets(STDIN));
@ -159,6 +181,7 @@ while (true) {
break; break;
case '2': // S'inscrire case '2': // S'inscrire
registerUser();
break; break;
case '0': // Quitter case '0': // Quitter

@ -0,0 +1,37 @@
<?php
namespace Service;
/**
* Interface IAuthService
* Adding more methods here may violate the Single Responsibility Principle (SRP).
* It's recommended to keep this interface focused on authentication-related functionality.
*/
interface IAuthService {
/**
* Authenticate a user.
*
* @param string $username The username of the user.
* @param string $password The password of the user.
*
* @return bool True if authentication is successful, false otherwise.
*/
public function login(string $username, string $password): bool;
/**
* Register a new user.
*
* @param string $username The username of the new user.
* @param string $password The password of the new user.
* @param string $data other data {undefined} for the moment.
*
* @return bool True if registration is successful, false otherwise.
*/
public function register(string $username, string $password, ...$data): bool;
/**
* Logout the currently authenticated user.
*
* @return void
*/
public function logoutUser(): void;
}

@ -0,0 +1,9 @@
<?php
namespace Model;
class Athlete extends Role {
// Attributs spécifiques a l'athlete si nécessaire
}
?>

@ -0,0 +1,8 @@
<?php
use Model\Athlete;
class Coach extends Athlete {
// Attributs spécifiques au Coach si nécessaire
}
?>

@ -0,0 +1,10 @@
<?php
namespace Model;
abstract class Role {
// Attributs spécifiques au Coach si nécessaire
}
?>

@ -0,0 +1,10 @@
<!-- It's like a DI Container for Manager -->
<?php
namespace Manager;
abstract class DataManager {
}
?>

@ -0,0 +1,39 @@
<?php
namespace Manager;
use Manager\IGenericDataManager;
use Service\IAuthService;
use Utils\Validation;
// c'est le modéle
class UserManager {
private IAuthService $authService;
public function __construct(IAuthService $authService){}
public function login($loginUser,$passwordUser): bool{
if(!Validation::val_string($passwordUser) || !Validation::val_string($loginUser)) throw new \Exception(" some wrong with cred !!!!!");
if($this->authService->login($loginUser,$passwordUser)){
return true;
}
return false;
}
public function resgister($loginUser,$passwordUser): bool{
if(!Validation::val_string($passwordUser) || !Validation::val_string($loginUser)) throw new \Exception(" some wrong with cred !!!!!");
if($this->authService->register($loginUser,$passwordUser)){
return true;
}
return false;
}
public function deconnecter():bool{
try {
$this->authService->logoutUser();
return true;
} catch (\Exception $e) {
return false;
}
}
}
?>

@ -0,0 +1,16 @@
<?php
namespace Model;
interface IGenericRepository
{
public function getItemById();
public function GetNbItems(): int;
public function GetItems(int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array;
public function GetItemsByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array;
public function GetItemByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array;
public function UpdateItem($oldItem, $newItem);
public function AddItem($item);
public function DeleteItem($item): bool;
}
?>

@ -0,0 +1,12 @@
<?php
namespace Stub;
use Manager\DataManager;
use Manager\UserManager;
class StubData extends DataManager{
public $userMgr;
public function __construct(){
$userMgr = new UserManager(new AuthService(new UserRepository()));
}
}
?>

@ -0,0 +1,71 @@
<?php
namespace Stub;
use Model\IGenericRepository;
use Model\IUserRepository;
use Model\Role;
use Model\User;
class UserRepository implements IGenericRepository,IUserRepository {
private array $users = [];
public function __construct() {
// Ajout de 5 utilisateurs pour l'exemple
$this->users[] = new User(1, "Doe", "John", "john.doe@example.com", "password123", new Role("Coach"));
$this->users[] = new User(2, "Smith", "Jane", "jane.smith@example.com", "secure456", new Role("User"));
$this->users[] = new User(3, "Martin", "Paul", "paul.martin@example.com", "super789", new Role("Coach"));
$this->users[] = new User(4, "Brown", "Anna", "anna.brown@example.com", "test000", new Role("User"));
$this->users[] = new User(5, "Lee", "Bruce", "bruce.lee@example.com", "hello321", new Role("User"));
}
public function getItemById(int $id): ?User {
foreach ($this->users as $user) {
if ($user->id === $id) {
return $user;
}
}
return null;
}
public function GetNbItems(): int {
return count($this->users);
}
public function GetItems(int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array {
// Cette méthode est un exemple simple, on ne gère pas l'ordonnancement ici
return array_slice($this->users, $index, $count);
}
public function GetItemsByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array {
$filteredUsers = array_filter($this->users, function ($user) use ($substring) {
return strpos(strtolower($user->nom), strtolower($substring)) !== false || strpos(strtolower($user->prenom), strtolower($substring)) !== false;
});
return array_slice($filteredUsers, $index, $count);
}
public function GetItemByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false):User {
return $this->GetItemsByName($substring, $index, $count, $orderingPropertyName, $descending)[0];
}
public function UpdateItem(User $oldUser, User $newUser): void {
$index = array_search($oldUser, $this->users);
if ($index !== false) {
$this->users[$index] = $newUser;
}
}
public function AddItem(User $user): void {
$this->users[] = $user;
}
public function DeleteItem(User $user): bool {
$index = array_search($user, $this->users);
if ($index !== false) {
unset($this->users[$index]);
return true;
}
return false;
}
}
?>

@ -0,0 +1,36 @@
<?php
namespace Stub;
use Exception\NotImplementedException;
use Model\IUserRepository;
use Service\IAuthService;
class AuthService implements IAuthService {
private $userRepository;
public function __construct(UserRepository $userRepository) {
$this->userRepository = $userRepository;
}
public function login($username, $password) {
$user = $this->userRepository->getUserByUsername($username);
if (!$user) {
throw new \Exception('Unable to find user with that name');
}
if ($user->isValidPassword($password)) {
return true;
}
return false;
}
public function register(string $username, string $password, ...$data): bool
{
throw new NotImplementedException("register method not implemented");
}
public function logoutUser(): void
{
throw new NotImplementedException("logout method not implemented");
}
}
Loading…
Cancel
Save