From 9db411bbc3b886e3d097d5e527195e51d9b53eb4 Mon Sep 17 00:00:00 2001 From: anperederi Date: Tue, 14 Nov 2023 18:33:45 +0100 Subject: [PATCH] update src project for our US --- Sources/config/config.php | 1 + Sources/src/console/Console.php | 1 + Sources/src/data/core/network/IAuthService.php | 6 ++++-- Sources/src/data/model/manager/CoachManager.php | 10 ++++++++++ Sources/src/data/model/manager/DataManager.php | 1 + Sources/src/data/model/manager/UserManager.php | 7 +++++-- Sources/src/data/stub/StubData.php | 8 ++++++-- Sources/src/data/stub/service/AuthService.php | 9 ++++----- Sources/src/shared/Validation.php | 2 +- Sources/tests/DataManager/CoachManager.php | 3 +-- 10 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 Sources/src/data/model/manager/CoachManager.php diff --git a/Sources/config/config.php b/Sources/config/config.php index 322c084a..86fe85c6 100644 --- a/Sources/config/config.php +++ b/Sources/config/config.php @@ -17,3 +17,4 @@ const DB_PASSWORD = 'achanger'; const APP_ENV = 'console'; const DSN = "mysql:host=" . DB_HOST . ";dbname=" . DB_DATABASE; + diff --git a/Sources/src/console/Console.php b/Sources/src/console/Console.php index f24fe0a6..80798791 100755 --- a/Sources/src/console/Console.php +++ b/Sources/src/console/Console.php @@ -8,6 +8,7 @@ use Model\Role; use Stub\StubData; use Manager\DataManager; + $model = new StubData(); // Couche d'accès au model function clearScreen() { diff --git a/Sources/src/data/core/network/IAuthService.php b/Sources/src/data/core/network/IAuthService.php index ef991a47..aa6d6e8a 100644 --- a/Sources/src/data/core/network/IAuthService.php +++ b/Sources/src/data/core/network/IAuthService.php @@ -1,5 +1,7 @@ authService = $authService; @@ -18,10 +20,11 @@ class UserManager 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)) { + $user = $this->authService->login($loginUser, $passwordUser); + if ($user) { + $this->currentUser = $user; return true; } return false; diff --git a/Sources/src/data/stub/StubData.php b/Sources/src/data/stub/StubData.php index 3f07a70e..6cd06efd 100644 --- a/Sources/src/data/stub/StubData.php +++ b/Sources/src/data/stub/StubData.php @@ -2,13 +2,17 @@ namespace Stub; use Shared\HashPassword; use Stub\AuthService; -use Manager\DataManager; -use Manager\UserManager; +use Manager\{CoachManager, DataManager, UserManager}; use Stub\UserRepository; class StubData extends DataManager{ public function __construct(){ $this->userMgr = new UserManager(new AuthService(new UserRepository(),new HashPassword())); + if($this->userMgr->currentUser->Role) + $this->coachMgr = new CoachManager(); +// if(user.Role is instance of Coach) { +// this->CoachMgr = new CoachManager(); +// } } } diff --git a/Sources/src/data/stub/service/AuthService.php b/Sources/src/data/stub/service/AuthService.php index 6f5fa8ef..0b1e170e 100644 --- a/Sources/src/data/stub/service/AuthService.php +++ b/Sources/src/data/stub/service/AuthService.php @@ -16,17 +16,16 @@ class AuthService implements IAuthService { $this->passwordHasher = $passwordHasher; } - public function login(string $username,string $password): bool { + public function login(string $username,string $password): ?User { $user = $this->userRepository->GetItemByName($username,0,1); if ($user == null || !$user instanceof User) { throw new \Exception('Unable to find user with that name'); } if ($user->isValidPassword($password)) { - return true; - } - - return false; + return $user; + } + return null; } public function register(string $loginUser, string $password, $data): bool { diff --git a/Sources/src/shared/Validation.php b/Sources/src/shared/Validation.php index 7f523712..a435366d 100644 --- a/Sources/src/shared/Validation.php +++ b/Sources/src/shared/Validation.php @@ -26,7 +26,7 @@ final class Validation { * @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) { + if (strlen(htmlspecialchars($string, ENT_QUOTES) === 0)) { throw new Exception("$string n'est pas valide. Tentative d'injection de code (attaque sécurité)!"); } else { return true; diff --git a/Sources/tests/DataManager/CoachManager.php b/Sources/tests/DataManager/CoachManager.php index dfdd3ce4..d2b189b5 100644 --- a/Sources/tests/DataManager/CoachManager.php +++ b/Sources/tests/DataManager/CoachManager.php @@ -6,9 +6,8 @@ class CoachManager { /** - * @param \Stub\AuthService $param */ - public function __construct(\Stub\AuthService $param) + public function __construct() { } } \ No newline at end of file