Commit 125a17cb authored by Moreau Elise's avatar Moreau Elise
Browse files

add location when create a new user

parent 3fff463b
......@@ -6,6 +6,7 @@ namespace App\Application\Actions\User;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Domain\User\User;
use App\Domain\Location\Location;
use GeoIp2\Database\Reader;
use DateTime;
......@@ -16,9 +17,8 @@ class CreateUserAction extends UserAction
*/
protected function action(): Response
{
$parsedRequestBody = (array)$this->request->getParsedBody();
$username = $this->checkvalue($parsedRequestBody['username']);
$firstname = $this->checkvalue($parsedRequestBody['firstname']);
$lastname = $this->checkvalue($parsedRequestBody['lastname']);
......@@ -26,6 +26,31 @@ class CreateUserAction extends UserAction
$password = $this->checkvalue($parsedRequestBody['password']);
$confirmPassword = $this->checkvalue($parsedRequestBody['confirmpassword']);
$contaminated = isset($parsedRequestBody['contaminated']) ? true : false;
$hasLatitude = $this->checkFloat($parsedRequestBody['latitude'], 90);
$hasLongitude = $this->checkFloat($parsedRequestBody['longitude'], 180);
if ((!$hasLatitude) or (!$hasLongitude))
{
return $this->twig->render(
$this->response, "/user/create_user.twig",
array(
"username" => $username,
"firstname" => $firstname,
"lastname" => $lastname,
"mail" => $mail,
"contaminated" => $contaminated,
"password" => null,
"confirmpassword" => null,
"formstatus" => "error",
"flashMessages" => [['Please allow the geolocation.']]
)
);
}
$latitude = (float) $parsedRequestBody['latitude'];
$longitude = (float) $parsedRequestBody['longitude'];
if ($password != $confirmPassword) {
return $this->twig->render(
......@@ -74,7 +99,6 @@ class CreateUserAction extends UserAction
"password" => null,
"confirmpassword" => null,
"flashMessages" => [["Mail already used"]],
"formstatus" => "error"
)
);
}
......@@ -84,6 +108,10 @@ class CreateUserAction extends UserAction
$date = new DateTime('now');
$user = new User(null, $username, $firstname, $lastname, $mail, $password, $date, $contaminated);
$location = new Location(null, $latitude, $longitude);
$user->setLocation($location);
$this->em->persist($location);
$this->em->persist($user);
$this->em->flush();
......@@ -106,4 +134,15 @@ class CreateUserAction extends UserAction
return $value;
}
protected function checkFloat($value, $number): bool
{
if (empty($value)) { return false; }
if (! is_numeric($value)) { return false; }
if (( (float) $value > $number) or ( (float) $value < $number * -1))
{
return false;
}
return true;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment