From 0222274a179c7b1a91614d67befdb05ab904a6d3 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 13 Oct 2025 15:41:41 +0200 Subject: [PATCH] Added S3 bucket for users --- migrations/Version20251013133256.php | 32 ++++++++++++++++++++ src/Controller/UserController.php | 10 +++++-- src/Entity/User.php | 2 +- src/Service/UserService.php | 38 ++++++++++++++---------- templates/elements/navbar.html.twig | 9 ++++-- templates/user/userInformation.html.twig | 6 ++-- templates/user/userList.html.twig | 2 +- templates/user/userListSmall.html.twig | 2 +- 8 files changed, 76 insertions(+), 25 deletions(-) create mode 100644 migrations/Version20251013133256.php diff --git a/migrations/Version20251013133256.php b/migrations/Version20251013133256.php new file mode 100644 index 0000000..57b356a --- /dev/null +++ b/migrations/Version20251013133256.php @@ -0,0 +1,32 @@ +addSql('ALTER TABLE "user" ALTER picture_url DROP NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE "user" ALTER picture_url SET NOT NULL'); + } +} diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index f8ea70f..d27a371 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -10,6 +10,7 @@ use App\Entity\UserOrganizatonApp; use App\Entity\UsersOrganizations; use App\Form\UserForm; use App\Service\ActionService; +use App\Service\AwsService; use App\Service\UserOrganizationAppService; use App\Service\UserOrganizationService; use App\Service\UserService; @@ -30,7 +31,9 @@ class UserController extends AbstractController public function __construct( private readonly EntityManagerInterface $entityManager, private readonly UserService $userService, - private readonly ActionService $actionService, private readonly UserOrganizationAppService $userOrganizationAppService, private readonly UserOrganizationService $userOrganizationService, + private readonly ActionService $actionService, + private readonly UserOrganizationAppService $userOrganizationAppService, + private readonly UserOrganizationService $userOrganizationService, ) { } @@ -172,9 +175,10 @@ class UserController extends AbstractController if ($picture) { $this->userService->handleProfilePicture($user, $picture); - } else { - $user->setPictureUrl(""); } +// else { +// $user->setPictureUrl(""); +// } //FOR TEST PURPOSES, SETTING A DEFAULT RANDOM PASSWORD $user->setPassword($this->userService->generateRandomPassword()); if ($orgId) { diff --git a/src/Entity/User.php b/src/Entity/User.php index 910ccb2..6d3ad6e 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -46,7 +46,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\Column(options: ['default' => 'CURRENT_TIMESTAMP'])] private ?\DateTimeImmutable $createdAt = null; - #[ORM\Column(length: 255)] + #[ORM\Column(length: 255, nullable: true)] private ?string $pictureUrl = null; #[ORM\Column(options: ['default' => 'CURRENT_TIMESTAMP'])] diff --git a/src/Service/UserService.php b/src/Service/UserService.php index 6297c14..6620c46 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -8,6 +8,7 @@ use App\Entity\Roles; use App\Entity\User; use App\Entity\UserOrganizatonApp; use App\Entity\UsersOrganizations; +use App\Service\AwsService; use DateTimeImmutable; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityNotFoundException; @@ -26,7 +27,7 @@ class UserService public function __construct(private readonly EntityManagerInterface $entityManager, private readonly Security $security, - string $profileDirectory + string $profileDirectory, private readonly AwsService $awsService ) { $this->profileDirectory = $profileDirectory; @@ -246,25 +247,32 @@ class UserService // Create custom filename: userNameUserSurname_ddmmyyhhmmss $customFilename = $user->getName() . $user->getSurname() . '_' . date('dmyHis') . '.' . $extension; + try{ + $this->awsService->PutDocObj($_ENV['S3_PORTAL_BUCKET'], $picture, $customFilename , $extension, 'profile/'); - // 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 - $picture->move($uploadDirectory, $customFilename); - - // Update user entity with the file path (relative to public directory) - $user->setPictureUrl('uploads/profile/' . $customFilename); - + $user->setPictureUrl('profile/'.$customFilename); } catch (FileException $e) { // Handle upload error throw new FileException('File upload failed: ' . $e->getMessage()); } + + + +// // 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 +// $picture->move($uploadDirectory, $customFilename); +// +// // Update user entity with the file path (relative to public directory) +// $user->setPictureUrl('uploads/profile/' . $customFilename); +// +// } /** diff --git a/templates/elements/navbar.html.twig b/templates/elements/navbar.html.twig index b202cdc..e467cf8 100644 --- a/templates/elements/navbar.html.twig +++ b/templates/elements/navbar.html.twig @@ -64,7 +64,12 @@
{% if app.user %} -

{{ app.user.email|first|capitalize }}

+{# {% if app.user.pictureUrl is defined %}#} +{# User profile pic#} +{# {% else %}#} +

{{ app.user.email|first|capitalize }}

+{# {% endif %}#} {% endif %}
@@ -88,7 +93,7 @@ - {{ ux_icon('material-symbols:logout', {height: '20px', width: '20px'}) }} + {{ ux_icon('material-symbols:logout', {height: '20px', width: '20px'}) }} Deconnexion diff --git a/templates/user/userInformation.html.twig b/templates/user/userInformation.html.twig index 96bf868..5c1115d 100644 --- a/templates/user/userInformation.html.twig +++ b/templates/user/userInformation.html.twig @@ -3,8 +3,10 @@
- user + {% if user.pictureUrl is not empty %} + user + {% endif %} +

{{ user.surname|capitalize }} {{ user.name|capitalize }}

diff --git a/templates/user/userList.html.twig b/templates/user/userList.html.twig index 29aceb8..4dd9a2d 100644 --- a/templates/user/userList.html.twig +++ b/templates/user/userList.html.twig @@ -33,7 +33,7 @@ {% if user.entity.pictureUrl %} - User profile pic {% else %} diff --git a/templates/user/userListSmall.html.twig b/templates/user/userListSmall.html.twig index 684146b..dfc72ca 100644 --- a/templates/user/userListSmall.html.twig +++ b/templates/user/userListSmall.html.twig @@ -26,7 +26,7 @@ {% if user.pictureUrl %} - User profile pic {% endif %}