allow uploading profile image

This commit is contained in:
Ryan Prather 2025-01-03 14:01:54 +00:00
parent 577dee227d
commit 368b4fbcd3

View File

@ -20,6 +20,7 @@ use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\String\Slugger\SluggerInterface;
#[IsGranted('ROLE_ADMIN')]
class AdminController extends AbstractController
@ -96,8 +97,11 @@ class AdminController extends AbstractController
}
#[Route('/add-user', name: 'app_add_user')]
public function addUser(Request $request, #[CurrentUser()] User $admin): Response
{
public function addUser(
Request $request,
#[CurrentUser()] User $admin,
SluggerInterface $slugger
): Response {
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($admin);
$this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($admin);
@ -143,6 +147,19 @@ class AdminController extends AbstractController
->setCompany($admin->getCompany())
;
if ($form->get('imageName')->getData()) {
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = $form['imageName']->getData();
$destination = $this->getParameter('kernel.project_dir').'/public/uploads/user_images/';
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$newFilename = $slugger->slug($originalFilename).'-'.uniqid().'.'.$file->guessExtension();
$file->move(
$destination,
$newFilename
);
$user->setImageName($newFilename);
}
$msg = MessageFactory::createUser($admin, $user, 'Welcome', "Welcome to CM Tracker");