/**
  * Callback method, that is called once form is successfully submitted, without validation errors.
  *
  * @param Form $form
  * @param Nette\Utils\ArrayHash $values
  */
 public function formSucceeded(Form $form, $values)
 {
     if (!($user = $this->userRepository->findOneBy(['email' => $values->email]))) {
         $form['email']->addError("User with given email doesn't exist");
         return;
     }
     // this is not a very secure way of getting new password
     // but it's the same way the symfony app is doing it...
     $newPassword = $user->generateRandomPassword();
     $this->em->flush();
     try {
         $message = new Nette\Mail\Message();
         $message->setSubject('Notejam password');
         $message->setFrom('*****@*****.**');
         $message->addTo($user->getEmail());
         // !!! Never send passwords through email !!!
         // This is only for demonstration purposes of Notejam.
         // Ideally, you can create a unique link where user can change his password
         // himself for limited amount of time, and then send the link.
         $message->setBody("Your new password is {$newPassword}");
         $this->mailer->send($message);
     } catch (Nette\Mail\SendException $e) {
         Debugger::log($e, 'email');
         $form->addError('Could not send email with new password');
     }
     $this->onSuccess($this);
 }
Exemplo n.º 2
0
 /**
  * Factory method for subcomponent form instance.
  * This factory is called internally by Nette in the component model.
  *
  * @return Form
  */
 protected function createComponentForm()
 {
     $form = $this->formFactory->create();
     $form->addText('email', 'Email')->setRequired('%label is required')->addRule($form::EMAIL);
     $form->addPassword('password', 'Password')->setRequired('%label is required');
     $form->addPassword('confirm', 'Confirm')->setRequired('%label is required')->addRule(Form::EQUAL, 'New passwords must match', $form['password']);
     $form->addSubmit('send');
     $form->onSuccess[] = [$this, 'formSucceeded'];
     $form->onValidate[] = function (Form $form, $values) {
         if ($this->userRepository->countBy(['email' => $values->email]) > 0) {
             $form->addError('Account with this email already exists');
         }
     };
     return $form;
 }