working version of edit profile
This commit is contained in:
parent
481849b530
commit
f48529b743
|
@ -6,6 +6,8 @@ use App\Entity\User;
|
|||
use App\Form\EditProfileFormType;
|
||||
use App\Repository\UserRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Flasher\Prime\FlasherInterface;
|
||||
use Flasher\SweetAlert\Prime\SweetAlertFactory;
|
||||
use Sunrise\Slugger\Slugger;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
@ -24,50 +26,6 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|||
*/
|
||||
class UserController extends BaseController
|
||||
{
|
||||
|
||||
#[Route(path: '/profile/edit/{username}', name: 'app_profile_edit')]
|
||||
public function editProfile(Request $request, UserRepository $userRepository, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, string $username = ''): Response
|
||||
{
|
||||
if ($username !== '') {
|
||||
if ($this->isGranted(attribute: 'ROLE_ADMIN')) {
|
||||
$user = $userRepository->findOneBy([
|
||||
'username' => $username,
|
||||
]);
|
||||
} else {
|
||||
throw new AccessDeniedException(message: 'Only admins are allowed to edit foreign profiles.');
|
||||
}
|
||||
} else {
|
||||
$user = $this->getUser();
|
||||
}
|
||||
|
||||
$form = $this->createForm(type: EditProfileFormType::class, data: $user);
|
||||
$form->handleRequest(request: $request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$user = $form->getData();
|
||||
// if there's a new password, use it
|
||||
|
||||
if ($form->get(name: 'newPassword')->getData())
|
||||
$user->setPassword(
|
||||
password: $userPasswordHasher->hashPassword(
|
||||
user: $user,
|
||||
plainPassword: $form->get(name: 'newPassword')->getData()
|
||||
)
|
||||
);
|
||||
|
||||
$entityManager->persist(entity: $user);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute(route: 'app_main');
|
||||
};
|
||||
|
||||
return $this->renderForm(view: '@default/user/edit_profile.html.twig', parameters: [
|
||||
'user' => $user,
|
||||
'userForm' => $form
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
#[Route(path: '/profile/{username}', name: 'app_profile')]
|
||||
public function showProfile(UserRepository $userRepository, string $username = ''): Response
|
||||
{
|
||||
|
@ -85,6 +43,61 @@ class UserController extends BaseController
|
|||
]);
|
||||
}
|
||||
|
||||
#[Route(path: '/profile/edit/{username}', name: 'app_profile_edit')]
|
||||
public function editProfile(Request $request,
|
||||
UserRepository $userRepository,
|
||||
UserPasswordHasherInterface $userPasswordHasher,
|
||||
EntityManagerInterface $entityManager,
|
||||
string $username = ''): Response
|
||||
{
|
||||
$user = $this->getUser();
|
||||
|
||||
$editUser = $userRepository->findOneBy(['username' => $username]);
|
||||
|
||||
if ($username !== $editUser->getUsername()) {
|
||||
if (!$this->isGranted(attribute: 'ROLE_ADMIN')) {
|
||||
$this->addFlash(type: 'error', message: 'Only admins are allowed to edit foreign profiles.');
|
||||
return $this->redirectToRoute(route: 'app_main');
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
$form = $this->createForm(type: EditProfileFormType::class, data: $user);
|
||||
$form->handleRequest(request: $request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$user = $form->getData();
|
||||
// if there's a new password, use it
|
||||
|
||||
if ($form->get(name: 'newPassword')->getData()) {
|
||||
$user->setPassword(
|
||||
password: $userPasswordHasher->hashPassword(
|
||||
user: $user,
|
||||
plainPassword: $form->get(name: 'newPassword')->getData()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ($user->getTmpAvatar()) {
|
||||
$user->setAvatar($user->getTmpAvatar());
|
||||
$user->setTmpAvatar('');
|
||||
}
|
||||
|
||||
$entityManager->persist(entity: $user);
|
||||
$entityManager->flush();
|
||||
|
||||
$this->addFlash(type: 'success', message: 'Profile successfully updated.');
|
||||
|
||||
return $this->redirectToRoute(route: 'app_main');
|
||||
};
|
||||
|
||||
return $this->renderForm(view: '@default/user/edit_profile.html.twig', parameters: [
|
||||
'user' => $editUser,
|
||||
'userForm' => $form
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
#[Route(path: '/list_users/', name: 'app_list_user')]
|
||||
public function listUsers(UserRepository $userRepository): Response
|
||||
{
|
||||
|
@ -96,7 +109,7 @@ class UserController extends BaseController
|
|||
}
|
||||
|
||||
// TODO move to a helper class
|
||||
function humanFilesize($bytes, $decimals = 2)
|
||||
function humanFilesize($bytes, $decimals = 2): string
|
||||
{
|
||||
$sz = 'BKMGTP';
|
||||
$factor = floor((strlen($bytes) - 1) / 3);
|
||||
|
@ -132,7 +145,8 @@ class UserController extends BaseController
|
|||
$cleanFilename = $slugger->slugify($originalFilename);
|
||||
$newFilename = $cleanFilename . '-' . uniqid() . '.' . $uploadedAvatar->guessExtension();
|
||||
$uploadedAvatar->move($destination, $newFilename);
|
||||
$user->setAvatar($newFilename);
|
||||
// Store the tmp name, use it on real form submit.
|
||||
$user->setTmpAvatar($newFilename);
|
||||
$entityManager->persist(entity: $user);
|
||||
$entityManager->flush();
|
||||
|
||||
|
|
Loading…
Reference in New Issue