You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Web/Sources/src/data/stub/service/AuthService.php

82 lines
2.4 KiB

<?php
namespace Stub;
use Model\Coach;
use Model\Athlete;
use Model\User;
use Shared\Exception\NotImplementedException;
use Network\IAuthService;
use Shared\IHashPassword;
use Stub\UserRepository;
class AuthService implements IAuthService {
private $userRepository;
private $passwordHasher;
public function __construct(UserRepository $userRepository, IHashPassword $passwordHasher) {
$this->userRepository = $userRepository;
$this->passwordHasher = $passwordHasher;
}
public function login(string $username,string $password): bool {
$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;
}
public function register(string $loginUser, string $password, $data): bool
{
$hashedPassword = $this->passwordHasher->hashPassword($password);
$existingUser = $this->userRepository->getItemByEmail($loginUser);
if ($existingUser != null || $existingUser instanceof User ) {
throw new \Exception('User already exists');
}
$prenom = $data['prenom'];
$nom = $data['nom'];
$email = $data['email'];
$sexe = $data['sexe'];
$taille = $data['taille'];
$poids = $data['poids'];
$dateNaissance = $data['dateNaissance'] ;
$roleName = $data['roleName'];
$role = null;
if($roleName == "Coach"){
$role = new Coach();
}
else if($roleName == "Athlete"){
$role = new Athlete();
}
$user = new User(
random_int(0, 100),
$nom,
$prenom,
$email,
$hashedPassword,
$sexe,
$taille,
$poids,
$dateNaissance,
//should use reflexion
$role
);
$this->userRepository->addItem($user);
return true;
}
public function logoutUser(): void
{
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! logout method not implemented !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
return;
}
}