Skip to content

Commit

Permalink
Login: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pepakriz committed Oct 10, 2014
1 parent 1d9460c commit 04538d6
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 71 deletions.
3 changes: 3 additions & 0 deletions src/Security/DI/services.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
services:

- Venne\Security\Login\ResetFormFactory (@system.admin.ajaxFormFactory)
- Venne\Security\Login\ResetFormService

- Venne\Security\Login\ConfirmFormFactory (@system.admin.ajaxFormFactory)
- Venne\Security\Login\ConfirmFormService

- Venne\Security\AdminModule\ProviderFormFactory (@system.admin.ajaxFormFactory)
- Venne\Security\AdminModule\ProvidersFormFactory (@system.admin.ajaxFormFactory)
Expand Down
2 changes: 2 additions & 0 deletions src/Security/DefaultType/RegistrationFormService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
class RegistrationFormService extends \Venne\System\DoctrineFormService
{

const SUBMIT_CAPTION = 'Register';

public function __construct(
RegistrationFormFactory $formFactory,
EntityManager $entityManager,
Expand Down
3 changes: 0 additions & 3 deletions src/Security/Login/ConfirmFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ public function create()
$form->addPassword('password_confirm', 'Confirm password')
->addRule($form::EQUAL, 'Invalid re password', $form['password']);

$form->addSubmit('_submit', 'Reset password')
->getControlPrototype()->class[] = 'btn-primary';

return $form;
}

Expand Down
92 changes: 92 additions & 0 deletions src/Security/Login/ConfirmFormService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* This file is part of the Venne:CMS (https://github.com/Venne)
*
* Copyright (c) 2011, 2012 Josef Kříž (http://www.josef-kriz.cz)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/

namespace Venne\Security\Login;

use Doctrine\ORM\EntityManager;
use Kdyby\Doctrine\Entities\BaseEntity;
use Kdyby\DoctrineForms\EntityFormMapper;
use Nette\Application\UI\Form;
use Nette\Application\UI\Link;
use Nette\Utils\Callback;
use Venne\Forms\FormFactory;
use Venne\Security\SecurityManager;
use Venne\Security\User;

/**
* @author Josef Kříž <[email protected]>
*/
class ConfirmFormService extends \Nette\Object
{

const SUBMIT_NAME = '_submit';

const SUBMIT_CAPTION = 'Reset password';

/** @var \Venne\Forms\IFormFactory */
private $formFactory;

/** @var \Doctrine\ORM\EntityManager */
private $entityManager;

/** @var \Kdyby\Doctrine\EntityRepository */
private $userRepository;

/** @var \Venne\Security\SecurityManager */
private $securityManager;

public function __construct(
ConfirmFormFactory $formFactory,
EntityManager $entityManager,
SecurityManager $securityManager
)
{
$this->formFactory = $formFactory;
$this->userRepository = $entityManager->getRepository(User::class);
$this->securityManager = $securityManager;
}

/**
* @param string $resetKey
* @return \Venne\Forms\FormFactory
*/
public function getFormFactory($resetKey)
{
return new FormFactory(function () use ($resetKey) {
$form = $this->formFactory->create();
$form->setCurrentGroup();
$form->addSubmit(static::SUBMIT_NAME, static::SUBMIT_CAPTION);

$form->onSuccess[] = function (Form $form) use ($resetKey) {
if ($form->isSubmitted() === $form[self::SUBMIT_NAME]) {
$this->save($form, $resetKey);
}
};

return $form;
});
}

/**
* @param \Nette\Application\UI\Form $form
* @param string $resetKey
*/
protected function save(Form $form, $resetKey)
{
$user = $this->userRepository->findOneBy(array('resetKey' => $resetKey));
$user->removeResetKey($resetKey);
$user->setPassword($form['password']->getValue());

$this->entityManager->flush();
$this->securityManager->sendNewPassword($user);
}

}
2 changes: 2 additions & 0 deletions src/Security/Login/LoginControl.latte
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{snippet content}
{control flashMessage}

{if $control->provider}
Expand All @@ -19,3 +20,4 @@
</div>
</div>
{/if}
{/snippet}
84 changes: 27 additions & 57 deletions src/Security/Login/LoginControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,55 +55,49 @@ class LoginControl extends \Venne\System\UI\Control
*/
public $key;

/** @var \Doctrine\ORM\EntityManager */
private $entityManager;

/** @var \Venne\System\AdminModule\LoginFormFactory */
private $loginFormFactory;

/** @var \Venne\Security\AdminModule\ProviderFormFactory */
private $providerFormFactory;

/** @var \Venne\Security\Login\ResetFormFactory */
private $resetFormFactory;
/** @var \Venne\Security\Login\ResetFormService */
private $resetFormService;

/** @var \Venne\Security\Login\ConfirmFormFactory */
private $confirmFormFactory;
/** @var \Venne\Security\Login\ConfirmFormService */
private $confirmFormService;

/** @var \Venne\Security\SecurityManager */
private $securityManager;

/** @var \Kdyby\Doctrine\EntityRepository */
private $userRepository;

/** @var \Nette\Mail\IMailer */
private $mailer;

/** @var \Venne\Bridges\Kdyby\DoctrineForms\FormFactoryFactory */
private $formFactoryFactory;

public function __construct(
EntityManager $entityManager,
LoginFormFactory $loginFormFactory,
ProviderFormFactory $providerFormFactory,
ResetFormFactory $resetFormFactory,
ConfirmFormFactory $confirmFormFactory,
ResetFormService $resetFormService,
ConfirmFormService $confirmFormService,
SecurityManager $securityManager,
IMailer $mailer,
FormFactoryFactory $formFactoryFactory
)
{
parent::__construct();

$this->entityManager = $entityManager;
$this->userRepository = $entityManager->getRepository(User::class);
$this->loginFormFactory = $loginFormFactory;
$this->providerFormFactory = $providerFormFactory;
$this->resetFormFactory = $resetFormFactory;
$this->confirmFormFactory = $confirmFormFactory;
$this->resetFormService = $resetFormService;
$this->confirmFormService = $confirmFormService;
$this->securityManager = $securityManager;
$this->mailer = $mailer;
$this->formFactoryFactory = $formFactoryFactory;

$this->redrawControl('content');
}

/**
Expand Down Expand Up @@ -153,11 +147,23 @@ protected function createComponentProviderForm()
*/
protected function createComponentResetForm()
{
$form = $this->resetFormFactory->create();
$form['cancel']->onClick[] = function () {
$this->redirect('this', array('reset' => null));
$form = $this
->resetFormService
->getFormFactory(function ($key) {
return $this->link('this', array('key' => $key, 'reset' => null));
})->create();

$cancel = $form->addSubmit('cancel', 'Cancel');
$cancel->setValidationScope(false);
$form->onSuccess[] = function (Form $form) {
if ($form->isSubmitted() === $form['_submit']) {
$this->flashMessage($this->translator->translate('New password has been sent.'), 'success');
}

$this->redirect('this', array(
'reset' => null
));
};
$form->onSuccess[] = $this->resetFormSuccess;

return $form;
}
Expand All @@ -172,7 +178,7 @@ protected function createComponentConfirmForm()
}

$form = $this->formFactoryFactory
->create($this->confirmFormFactory)
->create($this->confirmFormService)
->setEntity($user)
->create();

Expand Down Expand Up @@ -212,28 +218,6 @@ public function providerFormSuccess(Form $form)
$this->redirect('login', array($form['provider']->value, json_encode((array) $form['parameters']->values)));
}

public function resetFormSuccess(Form $form)
{
/** @var \Venne\Security\User $user */
$user = $this->userRepository->findOneBy(array('email' => $form['email']->value));

if (!$user) {
$this->flashMessage($this->translator->translate('User with email %email% does not exist.', null, array(
'email' => $form['email']->value,
)), 'warning');

return;
}

$this->sendEmail($user, $user->resetPassword());
$this->entityManager->flush($user);

$this->flashMessage($this->translator->translate('New password has been sent.'), 'success');
$this->redirect('this', array(
'reset' => null
));
}

public function confirmFormSuccess()
{
if (($user = $this->userRepository->findOneBy(array('resetKey' => $this->key))) === null) {
Expand Down Expand Up @@ -298,18 +282,4 @@ private function authenticate($provider, $parameters = null)
$this->redirect('this');
}

/**
* @param \Venne\Security\User $user
* @param string $key
*/
private function sendEmail(User $user, $key)
{
$absoluteUrls = $this->presenter->absoluteUrls;
$this->presenter->absoluteUrls = true;
$link = $this->link('this', array('key' => $key, 'reset' => null));
$this->presenter->absoluteUrls = $absoluteUrls;

$this->securityManager->sendRecoveryUrl($user, $link);
}

}
5 changes: 0 additions & 5 deletions src/Security/Login/ResetFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ public function create()
->addRule($form::FILLED)
->addRule($form::EMAIL);

$form->addSubmit('_submit', 'Reset password')
->getControlPrototype()->class[] = 'btn-primary';
$form->addSubmit('cancel', 'Cancel')
->setValidationScope(false);

return $form;
}

Expand Down
101 changes: 101 additions & 0 deletions src/Security/Login/ResetFormService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
* This file is part of the Venne:CMS (https://github.com/Venne)
*
* Copyright (c) 2011, 2012 Josef Kříž (http://www.josef-kriz.cz)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/

namespace Venne\Security\Login;

use Doctrine\ORM\EntityManager;
use Kdyby\Doctrine\Entities\BaseEntity;
use Kdyby\DoctrineForms\EntityFormMapper;
use Nette\Application\UI\Form;
use Nette\Application\UI\Link;
use Nette\Utils\Callback;
use Venne\Forms\FormFactory;
use Venne\Security\SecurityManager;
use Venne\Security\User;

/**
* @author Josef Kříž <[email protected]>
*/
class ResetFormService extends \Nette\Object
{

const SUBMIT_NAME = '_submit';

const SUBMIT_CAPTION = 'Reset password';

/** @var \Venne\Forms\IFormFactory */
private $formFactory;

/** @var \Doctrine\ORM\EntityManager */
private $entityManager;

/** @var \Kdyby\Doctrine\EntityRepository */
private $userRepository;

/** @var \Venne\Security\SecurityManager */
private $securityManager;

public function __construct(
ResetFormFactory $formFactory,
EntityManager $entityManager,
SecurityManager $securityManager
)
{
$this->formFactory = $formFactory;
$this->userRepository = $entityManager->getRepository(User::class);
$this->securityManager = $securityManager;
}

/**
* @param callable $resetLinkCallback
* @return \Venne\Forms\FormFactory
*/
public function getFormFactory($resetLinkCallback)
{
return new FormFactory(function () use ($resetLinkCallback) {
$form = $this->formFactory->create();
$form->setCurrentGroup();
$form->addSubmit(static::SUBMIT_NAME, static::SUBMIT_CAPTION);

$form->onSuccess[] = function (Form $form) use ($resetLinkCallback) {
if ($form->isSubmitted() === $form[self::SUBMIT_NAME]) {
$this->save($form, $resetLinkCallback);
}
};

return $form;
});
}

/**
* @param \Nette\Application\UI\Form $form
* @param callable $resetLinkCallback
*/
protected function save(Form $form, $resetLinkCallback)
{
/** @var \Venne\Security\User $user */
$user = $this->userRepository->findOneBy(array('email' => $form['email']->value));

if (!$user) {
$form->addError($form->getTranslator()->translate('User with email %email% does not exist.', null, array(
'email' => $form['email']->value,
)));

return;
}

$key = $user->resetPassword();
$url = Callback::invoke($resetLinkCallback, $key);
$this->securityManager->sendRecoveryUrl($user, $url);
$this->entityManager->flush($user);
}

}
Loading

0 comments on commit 04538d6

Please sign in to comment.