Skip to content

Commit

Permalink
Merge pull request #50 from kiloutyg/49-bug-where-the-account-creatio…
Browse files Browse the repository at this point in the history
…n-disappears

bug fixed with control in the account service, added required to each…
  • Loading branch information
kiloutyg authored Feb 1, 2024
2 parents bbfae0d + a4595ac commit 7ac2d47
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 42 deletions.
16 changes: 0 additions & 16 deletions assets/controllers/hello_controller.js

This file was deleted.

17 changes: 17 additions & 0 deletions assets/controllers/username_validation_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
static targets = ["username", "message"];

validateUsername() {
const regex = /^[a-zA-Z]+\.[a-zA-Z]+$/;
const isValid = regex.test(this.usernameTarget.value);

if (isValid) {
this.messageTarget.textContent = "";
} else {
this.messageTarget.textContent = "Format invalide. Veuillez saisir sous la forme prénom.nom.";
this.messageTarget.style.color = "red"; // Display the message in red color.
}
}
}
22 changes: 11 additions & 11 deletions src/Controller/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ public function createAccount(Request $request): Response
$this->addFlash('danger', 'Le compte ne peut être créé');
return $this->redirectToRoute('app_base');
}
$error = null;
$result = $this->accountService->createAccount(
$request,
$error
);
if ($result) {
$this->addFlash('success', 'Le compte a bien été créé.');
}
if ($error) {
$this->addFlash('error', $error);
try {
$result = $this->accountService->createAccount($request);
if ($result) {
$this->addFlash('success', 'Le compte a bien été créé.');
}
} catch (\Exception $e) {
// Catch and handle the exception.
// Log it, add a flash message, etc.
$error = $e->getMessage();
$this->addFlash('danger', $error);
}
return $this->redirectToRoute('app_base');
}
Expand Down Expand Up @@ -95,4 +95,4 @@ public function delete_account_basic(int $userId): Response
return $this->redirectToRoute('app_base');
}
}
}
}
10 changes: 9 additions & 1 deletion src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
namespace App\Entity;

use App\Repository\UserRepository;

use Doctrine\ORM\Mapping as ORM;

use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: UserRepository::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
Expand All @@ -16,6 +20,9 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
private ?int $id = null;

#[ORM\Column(length: 180, unique: true)]
#[Assert\NotBlank]
#[Assert\Length(min: 3, max: 180)]
#[Assert\Regex(pattern: '/^[a-zA-Z]+\.[a-zA-Z]+$/', message: 'Le nom d\'utilisateur doit être au format prénom.nom')]
private ?string $username = null;

#[ORM\Column]
Expand Down Expand Up @@ -71,6 +78,7 @@ public function getRoles(): array

public function setRoles(array $roles): static
{

$this->roles = $roles;

return $this;
Expand Down Expand Up @@ -111,4 +119,4 @@ public function setEmailAddress(?string $emailAddress): static

return $this;
}
}
}
22 changes: 18 additions & 4 deletions src/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace App\Service;

use Doctrine\ORM\EntityManagerInterface;

use Psr\Log\LoggerInterface;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\User\UserInterface;
Expand All @@ -20,29 +23,40 @@ class AccountService
private $userRepository;
private $manager;
private $entityDeletionService;
private $logger;

public function __construct(
UserPasswordHasherInterface $passwordHasher,
UserRepository $userRepository,
EntityManagerInterface $manager,
EntityDeletionService $entityDeletionService
EntityDeletionService $entityDeletionService,
LoggerInterface $logger
) {
$this->passwordHasher = $passwordHasher;
$this->userRepository = $userRepository;
$this->manager = $manager;
$this->entityDeletionService = $entityDeletionService;
$this->logger = $logger;
}

// This function is responsible for creating a new user account and persisting it to the database
public function createAccount(Request $request, $error)
public function createAccount(Request $request)
{
$this->logger->info('full request: ' . json_encode($request->request->all()));
if ($request->getMethod() == 'POST') {
$name = $request->request->get('username');
$password = $request->request->get('password');
$role = $request->request->get('role');
$emailAddress = $request->request->get('emailAddress');

if ($request->request->get('role') == '') {
$role = 'ROLE_USER';
} else {
$role = $request->request->get('role');
};

if ($role == 'ROLE_ADMIN' && $password == '') {
throw new \Exception('Le mot de passe ne peut pas être vide.');
}
// check if the username is already in use
$user = $this->userRepository->findOneBy(['username' => $name]);
if ($user) {
Expand Down Expand Up @@ -152,4 +166,4 @@ public function updateUserEmail(User $user)
$this->manager->flush();
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,19 @@
Nom d'utilisateur

</label>
<input type="text" name="username" placeholder="prenom.nom" required class="form-control" id="username">
</div>
<div data-controller="username-validation">
<input type="text"
class="form-control"
data-username-validation-target="username"
data-action="keyup->username-validation#validateUsername"
placeholder="prénom.nom"
id="username"
name="username"
required>

<div data-username-validation-target="message"
class="username-message"></div>
</div> </div>
<div class="form-control mb-3">
<label for="emailAddress">
Adresse Mail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<div>
<label>Selectionnez le role du nouveau compte:</label>
<select class="form-control " required name="role" id="role">
<option value="">--Choisissez un role--</option>
<select class="form-control " required name="role" id="role">
<option value="" disabled selected>--Choisissez un role--</option>
<option value="ROLE_SUPER_ADMIN">Super Admin</option>
</select>
</div>
Expand All @@ -12,7 +12,7 @@
<div>
<label>Selectionnez le role du nouveau compte:</label>
<select class="form-control " required name="role" id="role">
<option value="">--Choisissez un role--</option>
<option value="" disabled selected>--Choisissez un role--</option>
<option value="ROLE_ADMIN">Administrateur</option>
<option value="ROLE_USER">User</option>
</select>
Expand All @@ -21,9 +21,9 @@
{% elseif is_granted('ROLE_ADMIN') %}
<div>
<label>Selectionnez le role du nouveau compte:</label>
<select class="form-control " name="role" id="role">
<option value="">--Choisissez un role--</option>
<option value="ROLE_USER">User</option>
<select class="form-control " required name="role" id="role">
<option value="" disabled selected >--Choisissez un role--</option>
<option value="ROLE_USER" selected>User</option>
</select>
</div>
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@
Nom d'utilisateur

</label>
<input type="text" name="username" placeholder="prenom.nom" required class="form-control" id="username">
<div data-controller="username-validation">
<input type="text"
class="form-control"
data-username-validation-target="username"
data-action="keyup->username-validation#validateUsername"
placeholder="prénom.nom"
id="username"
name="username"
required>

<div data-username-validation-target="message"
class="username-message"></div>
</div>

</div>
<div class="form-control mb-3">
<label for="floatingPassword">
Mot de passe

</label>
<input type="password" id="floatingPassword" placeholder="Mot de passe" name="password" required class="form-control">
<input type="password" id="floatingPassword" placeholder="Mot de passe" name="password" class="form-control">
</div>
<div class="form-control mb-3">
<label for="emailAddress">
Expand Down

0 comments on commit 7ac2d47

Please sign in to comment.