diff --git a/src/Controller/AjaxController.php b/src/Controller/AjaxController.php index 213cea3..f75b630 100644 --- a/src/Controller/AjaxController.php +++ b/src/Controller/AjaxController.php @@ -20,6 +20,7 @@ use Exception; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Address; use Symfony\Component\Routing\Attribute\Route; @@ -453,6 +454,47 @@ class AjaxController extends AbstractController return $res; } + #[Route('/save-profile', name: 'app_save_profile', methods: ['POST'])] + public function saveProfile(Request $req, EntityManagerInterface $emi): Response + { + $data = json_decode($req->getContent()); + /** @var App\Entity\User $user */ + $user = $this->getUser(); + + if (!$user) { + return new JsonResponse(['msg' => 'No User']); + } + + if ($data->passChange) { + if(!$data->password) { + return new JsonResponse(['msg' => 'Blank password']); + } + + // @todo check that password matches current password + if ($data->password != $user->getPassword()) { + return new JsonResponse(['msg' => 'Invalid password']); + } + + if ($data->newPassword != $data->confPassword) { + return new JsonResponse(['msg' => 'Passwords don\'t match']); + } + } + + $user->setName($data->name); + $user->setEmail($data->email); + $user->setHomeChurchRSS($data->homeChurch); + + $emi->persist($user); + + try { + $emi->flush(); + } catch (Exception $e) { + return new JsonResponse(); + } + + return new JsonResponse(['msg' => 'Updated']); + } + #[Route('/save-settings', name: 'app_save_settings', methods: ['POST'])] public function saveSettings(Request $req, EntityManagerInterface $emi): Response {