Edit user data
This commit is contained in:
parent
3b1a3dee9a
commit
9dd820d47f
|
|
@ -7,6 +7,7 @@ parameters:
|
|||
aws_url: '%env(AWS_ENDPOINT)%'
|
||||
aws_public_url: '%env(AWS_ENDPOINT)%'
|
||||
logos_directory: '%kernel.project_dir%/public/uploads/logos'
|
||||
profile_directory: '%kernel.project_dir%/public/uploads/profile'
|
||||
|
||||
services:
|
||||
# default configuration for services in *this* file
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ use App\Service\UserService;
|
|||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Asset\Packages;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
|
@ -25,9 +26,9 @@ class UserController extends AbstractController
|
|||
private const ACCESS_DENIED = 'Access denied';
|
||||
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly UserService $userService,
|
||||
private readonly ActionService $actionService, private readonly UserOrganizationAppService $userOrganizationAppService,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly UserService $userService,
|
||||
private readonly ActionService $actionService, private readonly UserOrganizationAppService $userOrganizationAppService,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
|
@ -72,30 +73,29 @@ class UserController extends AbstractController
|
|||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_USER');
|
||||
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
|
||||
if($this->userService->hasAccessTo($actingUser)){
|
||||
if ($this->userService->hasAccessTo($actingUser)) {
|
||||
$user = $this->entityManager->getRepository(User::class)->find($id);
|
||||
try{
|
||||
try {
|
||||
$orgId = $request->query->get('organizationId');
|
||||
if($orgId){
|
||||
$orgs = $this->entityManager->getRepository(Organizations::class)->findBy(['id' =>$orgId]);
|
||||
if ($orgId) {
|
||||
$orgs = $this->entityManager->getRepository(Organizations::class)->findBy(['id' => $orgId]);
|
||||
$uo = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $user, 'organization' => $orgs, 'isActive' => true]);
|
||||
if(!$uo){
|
||||
if (!$uo) {
|
||||
throw $this->createNotFoundException(self::NOT_FOUND);
|
||||
}
|
||||
}
|
||||
else{
|
||||
$uo = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users'=> $user, 'isActive' => true]);
|
||||
foreach ($uo as $u){
|
||||
} else {
|
||||
$uo = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $user, 'isActive' => true]);
|
||||
foreach ($uo as $u) {
|
||||
$orgs[] = $u->getOrganization();
|
||||
}
|
||||
}
|
||||
$uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)->findBy(['userOrganization'=> $uo, 'isActive' => true]);
|
||||
$uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)->findBy(['userOrganization' => $uo, 'isActive' => true]);
|
||||
$uoa = $this->userOrganizationAppService->groupUserOrganizationAppsByApplication($uoa);
|
||||
$this->actionService->createAction("View user information", $user, null, $user->getUserIdentifier());
|
||||
}catch(\Exception $e){
|
||||
} catch (\Exception $e) {
|
||||
//ignore
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
throw $this->createAccessDeniedException(self::ACCESS_DENIED);
|
||||
}
|
||||
|
||||
|
|
@ -105,4 +105,41 @@ class UserController extends AbstractController
|
|||
'orgs' => $orgs ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/edit/{id}', name: 'edit', methods: ['GET', 'POST'])]
|
||||
public function edit(int $id, Request $request): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_USER');
|
||||
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
|
||||
if ($this->userService->hasAccessTo($actingUser)) {
|
||||
$user = $this->entityManager->getRepository(User::class)->find($id);
|
||||
if (!$user) {
|
||||
throw $this->createNotFoundException(self::NOT_FOUND);
|
||||
}
|
||||
$form = $this->createForm(UserForm::class, $user);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
// Handle file upload
|
||||
$logoFile = $form->get('pictureUrl')->getData();
|
||||
|
||||
if ($logoFile) {
|
||||
$this->userService->handleProfilePicture($user, $logoFile);
|
||||
|
||||
}
|
||||
$user->setModifiedAt(new \DateTimeImmutable('now'));
|
||||
$this->entityManager->persist($user);
|
||||
$this->entityManager->flush();
|
||||
$this->actionService->createAction("Edit user information", $user, null, $user->getUserIdentifier());
|
||||
|
||||
return $this->redirectToRoute('user_show', ['id' => $user->getId()]);
|
||||
}
|
||||
|
||||
return $this->render('user/edit.html.twig', [
|
||||
'user' => $user,
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
throw $this->createAccessDeniedException(self::ACCESS_DENIED);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ namespace App\Form;
|
|||
|
||||
use App\Entity\User;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
|
@ -17,7 +19,13 @@ class UserForm extends AbstractType
|
|||
->add('email', EmailType::class, ['required' => true, 'label' => 'Email*'])
|
||||
->add('name', TextType::class, ['required' => true, 'label' => 'Prénom*'])
|
||||
->add('surname', TextType::class, ['required' => true, 'label' => 'Nom*'])
|
||||
->add('phoneNumber', TextType::class, ['required' => false, 'label' => 'Numéro de téléphone']);
|
||||
->add('phoneNumber', TextType::class, ['required' => false, 'label' => 'Numéro de téléphone'])
|
||||
->add('pictureUrl', FileType::class, [
|
||||
'required' => false,
|
||||
'label' => 'Logo',
|
||||
'mapped' => false, // Important if the entity property is not directly mapped
|
||||
'attr' => ['accept' => 'image/*'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
|
|
|
|||
|
|
@ -13,18 +13,22 @@ use Doctrine\ORM\EntityNotFoundException;
|
|||
use Exception;
|
||||
use League\Bundle\OAuth2ServerBundle\Model\AccessToken;
|
||||
use Random\RandomException;
|
||||
use SebastianBergmann\CodeCoverage\Util\DirectoryCouldNotBeCreatedException;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
|
||||
class UserService
|
||||
{
|
||||
|
||||
public const NOT_FOUND = 'Entity not found';
|
||||
private string $profileDirectory;
|
||||
|
||||
public function __construct(private readonly EntityManagerInterface $entityManager,
|
||||
private readonly Security $security,
|
||||
string $profileDirectory
|
||||
)
|
||||
{
|
||||
// Constructor logic if needed
|
||||
$this->profileDirectory = $profileDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -232,4 +236,32 @@ class UserService
|
|||
// Use a fixed key (e.g., 0 or 'none') to avoid collisions with real org IDs
|
||||
return ['none' => $group];
|
||||
}
|
||||
|
||||
public function handleProfilePicture(User $user, $logoFile): void
|
||||
{
|
||||
// Get file extension
|
||||
$extension = $logoFile->guessExtension();
|
||||
|
||||
// Create custom filename: userNameUserSurname_ddmmyyhhmmss
|
||||
$customFilename = $user->getName() . $user->getSurname() . '_' . date('dmyHis') . '.' . $extension;
|
||||
|
||||
// Define upload directory
|
||||
$uploadDirectory = $this->profileDirectory;
|
||||
// Create directory if it doesn't exist
|
||||
if (!is_dir($uploadDirectory) && !mkdir($uploadDirectory, 0755, true) && !is_dir($uploadDirectory)) {
|
||||
throw new DirectoryCouldNotBeCreatedException(sprintf('Directory "%s" was not created', $uploadDirectory));
|
||||
}
|
||||
try {
|
||||
|
||||
// Move the file to the upload directory
|
||||
$logoFile->move($uploadDirectory, $customFilename);
|
||||
|
||||
// Update user entity with the file path (relative to public directory)
|
||||
$user->setPictureUrl('uploads/profile/' . $customFilename);
|
||||
|
||||
} catch (FileException $e) {
|
||||
// Handle upload error
|
||||
throw new FileException('File upload failed: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
<div class="card">
|
||||
<div class="card-title shadow-sm p-3 d-flex justify-content-between align-items-center">
|
||||
<h2>Modifier l'utilisateur</h2>
|
||||
<a href="{{ path('user_delete', {'id': user.id}) }}" class="btn btn-danger">Supprimer</a>
|
||||
{# <a href="{{ path('user_delete', {'id': user.id}) }}" class="btn btn-danger">Supprimer</a>#}
|
||||
</div>
|
||||
<div class="card-body">
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
<div class="card border-0">
|
||||
<div class="card-title shadow-sm p-3 d-flex justify-content-between align-items-center">
|
||||
<h2>{{ user.surname|capitalize }} {{ user.name|capitalize }}</h2>
|
||||
{# <a href="{{ path('user_edit', {'id': user.id}) }}" class="btn btn-primary">Modifier</a>#}
|
||||
<a href="{{ path('user_edit', {'id': user.id}) }}" class="btn btn-primary">Modifier</a>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<p> <b>Email: </b>{{ user.email }}</p>
|
||||
|
|
|
|||
Loading…
Reference in New Issue