Advertisement
Guest User

Untitled

a guest
Jul 7th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.96 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Forms;
  4.  
  5. use Nette,
  6.     Nette\Application\UI\Form,
  7.     App\Model\Utils\StringUtils,
  8.     App\Model\UserManager,
  9.     Nette\Security\User;
  10.  
  11. /**
  12.  * Factory for sign-in form
  13.  * @author Michal Rost
  14.  * @since 6.4.2015
  15.  */
  16. class SignFormFactory extends AbstractFormFactory {
  17.  
  18.   private $userManager;
  19.   private $userObject;
  20.   private $user;
  21.   private $db;
  22.  
  23.   /**
  24.    * Creates form factory
  25.    * @param UserManager $um
  26.    * @param DibiConnection $db
  27.    * @param User $user
  28.    */
  29.   public function __construct(UserManager $um, \DibiConnection $db, User $user) {
  30.     $this->userObject = (new \App\Model\Dao\AppUser())->setDb($db);
  31.     $this->userManager = $um;
  32.     $this->user = $user;
  33.     $this->db = $db;
  34.   }
  35.  
  36.   /**
  37.    * Creates form
  38.    * @return Form
  39.    */
  40.   public function create() {
  41.    
  42.     // Messages
  43.     $msgUsr = 'Please enter your username.';
  44.     $msgPas = 'Please enter your password.';
  45.    
  46.     // Form
  47.     $form = new Form;
  48.     $form->addText('email', 'E-mail:')->setRequired($msgUsr);
  49.     $form->addPassword('passwd', 'Password:')->setRequired($msgPas);
  50.     $form->addCheckbox('remember', 'Keep me signed in');
  51.     $form->addSubmit('send', 'Sign in');
  52.     $form->onSuccess[] = array($this, 'formSucceeded');
  53.     $form->onValidate[] = array($this, 'formValidation');
  54.     return $this->updateRenderer($form);
  55.   }
  56.  
  57.   /**
  58.    * Handles form suceed
  59.    * @param Form $form
  60.    * @param array $values
  61.    */
  62.   public function formSucceeded($form, $values) {
  63.     if ($values->remember) {
  64.       $this->user->setExpiration('14 days', FALSE);
  65.     } else {
  66.       $this->user->setExpiration('20 minutes', TRUE);
  67.     }
  68.     try {
  69.       $this->user->login($values->email, $values->passwd, null, null);
  70.     } catch (Nette\Security\AuthenticationException $e) {      
  71.       if ($e->getCode() == UserManager::NOT_VERIFIED) {
  72.         $args = array('email' => $values->email);
  73.         $form->getPresenter()->redirect("Sign:verify", $args);
  74.       } else {      
  75.         $form->addError($e->getMessage());
  76.       }
  77.     }
  78.   }
  79.  
  80.   /**
  81.    * Checks sign in form validity
  82.    * @param Form $form
  83.    */
  84.   public function formValidation(Form $form) {
  85.  
  86.     // Helper variables
  87.     $values = $form->getValues();
  88.     $email = $values['email'];
  89.     $passwd = $values['passwd'];
  90.  
  91.     // Check email is set
  92.     if (strlen(StringUtils::simplify($email)) == 0) {
  93.       $form['email']->addError('Fill e-mail');
  94.     }
  95.  
  96.     // Check if email has valid format
  97.     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  98.       $form['email']->addError('Invalid e-mail format.');
  99.     }
  100.  
  101.     // Check password is set
  102.     if (strlen(StringUtils::simplify($passwd)) == 0) {
  103.       $form['passwd']->addError('Fill password');
  104.     }
  105.  
  106.     // Check password
  107.     $ac = $this->userObject->loadByEmail($email);
  108.     if (!$ac->isPopulated() || !$this->userManager->checkPasswd($ac, $passwd)) {
  109.       $form['passwd']->addError('Invalid password');
  110.     }
  111.   }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement