Merge branch 'issue_16_statistics_coach' of https://codefirst.iut.uca.fr/git/FitDev/Projet_fit_web into issue_16_statistics_coach
continuous-integration/drone/push Build is passing Details

issue_023_User_Gateway
Kevin MONTEIRO 1 year ago
commit 5b970ccd74

@ -17,3 +17,4 @@ const DB_PASSWORD = 'achanger';
const APP_ENV = 'console'; const APP_ENV = 'console';
const DSN = "mysql:host=" . DB_HOST . ";dbname=" . DB_DATABASE; const DSN = "mysql:host=" . DB_HOST . ";dbname=" . DB_DATABASE;

@ -8,6 +8,7 @@ use Model\Role;
use Stub\StubData; use Stub\StubData;
use Manager\DataManager; use Manager\DataManager;
$model = new StubData(); // Couche d'accès au model $model = new StubData(); // Couche d'accès au model
function clearScreen() function clearScreen()
{ {

@ -1,5 +1,7 @@
<?php <?php
namespace Network; namespace Network;
use Model\User;
/** /**
* Interface IAuthService * Interface IAuthService
* Adding more methods here may violate the Single Responsibility Principle (SRP). * Adding more methods here may violate the Single Responsibility Principle (SRP).
@ -12,9 +14,9 @@ interface IAuthService {
* @param string $username The username of the user. * @param string $username The username of the user.
* @param string $password The password of the user. * @param string $password The password of the user.
* *
* @return bool True if authentication is successful, false otherwise. * @return ?User True if authentication is successful, false otherwise.
*/ */
public function login(string $username, string $password): bool; public function login(string $username, string $password): ?User;
/** /**
* Register a new user. * Register a new user.

@ -0,0 +1,10 @@
<?php
namespace Manager;
class CoachManager
{
function __construct() {
}
}

@ -3,6 +3,7 @@ namespace Manager;
abstract class DataManager { abstract class DataManager {
public $userMgr; public $userMgr;
public $coachMgr;
} }

@ -3,6 +3,7 @@ namespace Manager;
use Model\Athlete; use Model\Athlete;
use Model\Coach; use Model\Coach;
use Model\User;
use Network\IAuthService; use Network\IAuthService;
use Shared\Validation; use Shared\Validation;
@ -11,6 +12,7 @@ use Shared\Validation;
class UserManager class UserManager
{ {
private IAuthService $authService; private IAuthService $authService;
public User $currentUser;
public function __construct(IAuthService $authService) public function __construct(IAuthService $authService)
{ {
$this->authService = $authService; $this->authService = $authService;
@ -18,10 +20,11 @@ class UserManager
public function login($loginUser, $passwordUser): bool public function login($loginUser, $passwordUser): bool
{ {
if (!Validation::val_string($passwordUser) || !Validation::val_string($loginUser)) if (!Validation::val_string($passwordUser) || !Validation::val_string($loginUser))
throw new \Exception(" some wrong with cred !!!!!"); 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 true;
} }
return false; return false;

@ -2,13 +2,17 @@
namespace Stub; namespace Stub;
use Shared\HashPassword; use Shared\HashPassword;
use Stub\AuthService; use Stub\AuthService;
use Manager\DataManager; use Manager\{CoachManager, DataManager, UserManager};
use Manager\UserManager;
use Stub\UserRepository; use Stub\UserRepository;
class StubData extends DataManager{ class StubData extends DataManager{
public function __construct(){ public function __construct(){
$this->userMgr = new UserManager(new AuthService(new UserRepository(),new HashPassword())); $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();
// }
} }
} }

@ -16,17 +16,16 @@ class AuthService implements IAuthService {
$this->passwordHasher = $passwordHasher; $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); $user = $this->userRepository->GetItemByName($username,0,1);
if ($user == null || !$user instanceof User) { if ($user == null || !$user instanceof User) {
throw new \Exception('Unable to find user with that name'); throw new \Exception('Unable to find user with that name');
} }
if ($user->isValidPassword($password)) { if ($user->isValidPassword($password)) {
return true; return $user;
} }
return null;
return false;
} }
public function register(string $loginUser, string $password, $data): bool public function register(string $loginUser, string $password, $data): bool
{ {

@ -26,7 +26,7 @@ final class Validation {
* @throws Exception Si la chaîne n'est pas valide (tentative d'injection de code). * @throws Exception Si la chaîne n'est pas valide (tentative d'injection de code).
*/ */
public static function val_string(string $string) : bool { 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é)!"); throw new Exception("$string n'est pas valide. Tentative d'injection de code (attaque sécurité)!");
} else { } else {
return true; return true;

@ -0,0 +1,13 @@
<?php
namespace DataManager;
class CoachManager
{
/**
*/
public function __construct()
{
}
}
Loading…
Cancel
Save