getUser(); } else { //Else we must check, if the current user is allowed to access $user $this->denyAccessUnlessGranted('read', $user); } if ($this->getParameter('use_gravatar')) { $avatar = $this->getGravatar($user->getEmail(), 200, 'identicon'); } else { $avatar = $packages->getUrl('/img/default_avatar.png'); } return $this->render('Users/user_info.html.twig', [ 'user' => $user, 'avatar' => $avatar, ]); } /** * @Route("/user/settings", name="user_settings") */ public function userSettings(Request $request, EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder) { /** * @var User */ $user = $this->getUser(); //When user change its settings, he should be logged in fully. $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); /*************************** * User settings form ***************************/ $form = $this->createForm(UserSettingsType::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em->persist($user); $em->flush(); $this->addFlash('success', 'user.settings.saved_flash'); } /***************************** * Password change form ****************************/ $pw_form = $this->createFormBuilder() ->add('old_password', PasswordType::class, [ 'label' => 'user.settings.pw_old.label', 'constraints' => [new UserPassword()], ]) //This constraint checks, if the current user pw was inputted. ->add('new_password', RepeatedType::class, [ 'type' => PasswordType::class, 'first_options' => ['label' => 'user.settings.pw_new.label'], 'second_options' => ['label' => 'user.settings.pw_confirm.label'], 'invalid_message' => 'password_must_match', 'constraints' => [new Length([ 'min' => 6, 'max' => 128, ])], ]) ->add('submit', SubmitType::class, ['label' => 'save']) ->getForm(); $pw_form->handleRequest($request); //Check if password if everything was correct, then save it to User and DB if ($pw_form->isSubmitted() && $pw_form->isValid()) { $password = $passwordEncoder->encodePassword($user, $pw_form['new_password']->getData()); $user->setPassword($password); $em->persist($user); $em->flush(); $this->addFlash('success', 'user.settings.pw_changed_flash'); } /****************************** * Output both forms *****************************/ return $this->render('Users/user_settings.html.twig', [ 'settings_form' => $form->createView(), 'pw_form' => $pw_form->createView(), ]); } /** * Get either a Gravatar URL or complete image tag for a specified email address. * * @param string $email The email address * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] * @param string $r Maximum rating (inclusive) [ g | pg | r | x ] * @param bool $img True to return a complete IMG tag False for just the URL * @param array $atts Optional, additional key/value attributes to include in the IMG tag * * @return string containing either just a URL or a complete image tag * @source https://gravatar.com/site/implement/images/php/ */ public function getGravatar(string $email, int $s = 80, string $d = 'mm', string $r = 'g', bool $img = false, array $atts = array()) { $url = 'https://www.gravatar.com/avatar/'; $url .= md5(strtolower(trim($email))); $url .= "?s=$s&d=$d&r=$r"; if ($img) { $url = ' $val) { $url .= ' '.$key.'="'.$val.'"'; } $url .= ' />'; } return $url; } }