finished user prfile and passwords
This commit is contained in:
@@ -3,9 +3,13 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Form\EditProfileFormType;
|
||||
use App\Repository\UserRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
|
||||
@@ -13,31 +17,56 @@ use Symfony\Component\Security\Core\Exception\UserNotFoundException;
|
||||
/**
|
||||
* Class UserController.
|
||||
*/
|
||||
class UserController extends AbstractController
|
||||
class UserController extends BaseController
|
||||
{
|
||||
|
||||
#[Route(path: '/profile/edit/{username}', name: 'app_profile_edit')]
|
||||
public function editProfile(UserRepository $userRepository, string $username = ''): Response
|
||||
public function editProfile(Request $request, UserRepository $userRepository, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, string $username = ''): Response
|
||||
{
|
||||
/* var User $user */
|
||||
if ($username === '') {
|
||||
if ($this->isGranted(attribute: 'ROLE_USER')) {
|
||||
$user = $this->getUser();
|
||||
} else {
|
||||
throw new AccessDeniedException(message: 'You need to be logged in.');
|
||||
}
|
||||
} else {
|
||||
if ($username !== '') {
|
||||
if ($this->isGranted(attribute: 'ROLE_ADMIN')) {
|
||||
$user = $userRepository->findOneBy([
|
||||
'username' => $username,
|
||||
]);
|
||||
} else {
|
||||
throw new AccessDeniedException(message: 'Only admins are allowed to edit 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');
|
||||
};
|
||||
|
||||
$user = $form->getData();
|
||||
// hash the plain password
|
||||
|
||||
|
||||
return $this->renderForm(view: '@default/user/edit_profile.html.twig', parameters: [
|
||||
'user' => $user,
|
||||
'userForm' => $form
|
||||
]);
|
||||
|
||||
|
||||
if (isset($user)) {
|
||||
return $this->render(view: 'user/edit_profile.html.twig', parameters: [
|
||||
'user' => $user,
|
||||
]);
|
||||
} else {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user