Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Forms;
- use Nette,
- Nette\Application\UI\Form,
- App\Model\Utils\StringUtils,
- App\Model\UserManager,
- Nette\Security\User;
- /**
- * Factory for sign-in form
- * @author Michal Rost
- * @since 6.4.2015
- */
- class SignFormFactory extends AbstractFormFactory {
- private $userManager;
- private $userObject;
- private $user;
- private $db;
- /**
- * Creates form factory
- * @param UserManager $um
- * @param DibiConnection $db
- * @param User $user
- */
- public function __construct(UserManager $um, \DibiConnection $db, User $user) {
- $this->userObject = (new \App\Model\Dao\AppUser())->setDb($db);
- $this->userManager = $um;
- $this->user = $user;
- $this->db = $db;
- }
- /**
- * Creates form
- * @return Form
- */
- public function create() {
- // Messages
- $msgUsr = 'Please enter your username.';
- $msgPas = 'Please enter your password.';
- // Form
- $form = new Form;
- $form->addText('email', 'E-mail:')->setRequired($msgUsr);
- $form->addPassword('passwd', 'Password:')->setRequired($msgPas);
- $form->addCheckbox('remember', 'Keep me signed in');
- $form->addSubmit('send', 'Sign in');
- $form->onSuccess[] = array($this, 'formSucceeded');
- $form->onValidate[] = array($this, 'formValidation');
- return $this->updateRenderer($form);
- }
- /**
- * Handles form suceed
- * @param Form $form
- * @param array $values
- */
- public function formSucceeded($form, $values) {
- if ($values->remember) {
- $this->user->setExpiration('14 days', FALSE);
- } else {
- $this->user->setExpiration('20 minutes', TRUE);
- }
- try {
- $this->user->login($values->email, $values->passwd, null, null);
- } catch (Nette\Security\AuthenticationException $e) {
- if ($e->getCode() == UserManager::NOT_VERIFIED) {
- $args = array('email' => $values->email);
- $form->getPresenter()->redirect("Sign:verify", $args);
- } else {
- $form->addError($e->getMessage());
- }
- }
- }
- /**
- * Checks sign in form validity
- * @param Form $form
- */
- public function formValidation(Form $form) {
- // Helper variables
- $values = $form->getValues();
- $email = $values['email'];
- $passwd = $values['passwd'];
- // Check email is set
- if (strlen(StringUtils::simplify($email)) == 0) {
- $form['email']->addError('Fill e-mail');
- }
- // Check if email has valid format
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $form['email']->addError('Invalid e-mail format.');
- }
- // Check password is set
- if (strlen(StringUtils::simplify($passwd)) == 0) {
- $form['passwd']->addError('Fill password');
- }
- // Check password
- $ac = $this->userObject->loadByEmail($email);
- if (!$ac->isPopulated() || !$this->userManager->checkPasswd($ac, $passwd)) {
- $form['passwd']->addError('Invalid password');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement