-
+
{control flashMessage TRUE}
diff --git a/bin/venne b/bin/venne
new file mode 100644
index 00000000..a692126a
--- /dev/null
+++ b/bin/venne
@@ -0,0 +1,4 @@
+#!/usr/bin/env php
+getService('application')->run();
diff --git a/build.php b/build.php
deleted file mode 100755
index e8f249a3..00000000
--- a/build.php
+++ /dev/null
@@ -1,5 +0,0 @@
-
+
+
+
+
+
+
+
+
+
diff --git a/build/ruleset.xml b/build/ruleset.xml
new file mode 100644
index 00000000..8698b2d9
--- /dev/null
+++ b/build/ruleset.xml
@@ -0,0 +1,59 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Variable "%s" not allowed in double quoted string; use sprintf() instead
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/composer.json b/composer.json
index fc21e2b7..ec519f7d 100755
--- a/composer.json
+++ b/composer.json
@@ -34,7 +34,9 @@
"blueimp/jquery-file-upload": "9.7.1"
},
"require-dev": {
- "nette/tester": "~1.2.0"
+ "nette/tester": "~1.2.0",
+ "phing/phing": "~2.8.0",
+ "squizlabs/php_codesniffer": "~1.5.0"
},
"autoload": {
"psr-4": {
@@ -46,6 +48,7 @@
"VenneTests\\": "tests/src/"
}
},
+ "bin": ["bin/venne", "bin/venne.php"],
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
diff --git a/src/Config/ConfigBuilder.php b/src/Config/ConfigBuilder.php
index da6fa2be..a4183373 100755
--- a/src/Config/ConfigBuilder.php
+++ b/src/Config/ConfigBuilder.php
@@ -94,7 +94,7 @@ public function offsetExists($index)
public function offsetGet($index)
{
if ($index < 0 || $index >= count($this->data)) {
- throw new OutOfRangeException("Offset invalid or out of range");
+ throw new OutOfRangeException('Offset invalid or out of range');
}
return $this->data[$index];
@@ -123,7 +123,7 @@ public function offsetSet($index, $value)
public function offsetUnset($index)
{
if ($index < 0 || $index >= count($this->data)) {
- throw new OutOfRangeException("Offset invalid or out of range");
+ throw new OutOfRangeException('Offset invalid or out of range');
}
array_splice($this->data, $index, 1);
}
diff --git a/src/Config/ConfigMapper.php b/src/Config/ConfigMapper.php
index 33c39f9e..038c2957 100755
--- a/src/Config/ConfigMapper.php
+++ b/src/Config/ConfigMapper.php
@@ -150,7 +150,7 @@ private function save($container = null, $rec = false, array $values = array())
if (!Strings::startsWith($key, '_')) {
if ($control instanceof \Nette\Forms\Container) {
$values[$key] = $this->save($control, true, $values);
- } else if ($control instanceof \Nette\Forms\IControl) {
+ } elseif ($control instanceof \Nette\Forms\IControl) {
if (!$control->isOmitted()) {
$values[$key] = $control->value;
}
diff --git a/src/Doctrine/DerivedEntityDao.php b/src/Doctrine/DerivedEntityDao.php
deleted file mode 100755
index b4e2167d..00000000
--- a/src/Doctrine/DerivedEntityDao.php
+++ /dev/null
@@ -1,34 +0,0 @@
-
- */
-class DerivedEntityDao extends \Kdyby\Doctrine\EntityDao
-{
-
- public function save($entity = null, $relations = null)
- {
- $metadata = $this->getEntityManager()->getClassMetadata(get_class($entity));
- $primaryProperty = $metadata->getSingleIdReflectionProperty()->getName();
-
- $targetEntity = $entity->$primaryProperty;
-
- $this->getEntityManager()
- ->getRepository($targetEntity::getClassName())
- ->save($targetEntity);
-
- return parent::save($entity, $relations);
- }
-
-}
diff --git a/src/Doctrine/Entities/NamedEntityTrait.php b/src/Doctrine/Entities/NamedEntityTrait.php
index 45557b58..0c039bf0 100755
--- a/src/Doctrine/Entities/NamedEntityTrait.php
+++ b/src/Doctrine/Entities/NamedEntityTrait.php
@@ -34,7 +34,7 @@ trait NamedEntityTrait
/**
* @var string
*
- * @ORM\Column(type="string")
+ * @ORM\Column(type="string", unique=true)
*/
protected $name;
diff --git a/src/Doctrine/Mapping/DynamicMapperSubscriber.php b/src/Doctrine/Mapping/DynamicMapperSubscriber.php
index 4ea61853..21b48398 100755
--- a/src/Doctrine/Mapping/DynamicMapperSubscriber.php
+++ b/src/Doctrine/Mapping/DynamicMapperSubscriber.php
@@ -25,10 +25,10 @@ class DynamicMapperSubscriber implements EventSubscriber
{
/** @var bool */
- private $_l = false;
+ private $l = false;
/** @var string */
- private $_lName;
+ private $lName;
/**
* Array of events.
@@ -46,9 +46,9 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $args)
{
$meta = $args->getClassMetadata();
- if ($this->_l) {
- if (Strings::endsWith($meta->associationMappings[$this->_lName]['targetEntity'], '::dynamic')) {
- $meta->associationMappings[$this->_lName]['targetEntity'] = $this->getTargetEntity($meta->name, $this->_l);
+ if ($this->l) {
+ if (Strings::endsWith($meta->associationMappings[$this->lName]['targetEntity'], '::dynamic')) {
+ $meta->associationMappings[$this->lName]['targetEntity'] = $this->getTargetEntity($meta->name, $this->l);
}
return;
@@ -63,23 +63,23 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $args)
$em = $args->getEntityManager();
$target = $this->getTargetEntity($meta, $name);
- $this->_l = $meta->name;
- $this->_lName = $meta->associationMappings[$name]['inversedBy'];
+ $this->l = $meta->name;
+ $this->lName = $meta->associationMappings[$name]['inversedBy'];
- if (!$this->_lName) {
- $this->_lName = $meta->associationMappings[$name]['mappedBy'];
+ if (!$this->lName) {
+ $this->lName = $meta->associationMappings[$name]['mappedBy'];
}
- if ($this->_lName) {
+ if ($this->lName) {
$targetMeta = $em->getClassMetadata($target);
}
- $this->_l = false;
+ $this->l = false;
$meta->associationMappings[$name]['targetEntity'] = $target;
- if ($this->_lName) {
- $targetMeta->associationMappings[$this->_lName]['targetEntity'] = $meta->name;
+ if ($this->lName) {
+ $targetMeta->associationMappings[$this->lName]['targetEntity'] = $meta->name;
}
}
@@ -95,14 +95,13 @@ private function getTargetEntity(ClassMetadata $meta, $association)
$method = 'get' . ucfirst($association) . 'Name';
if (($ret = call_user_func(array($meta->name, $method))) === null) {
- throw new InvalidArgumentException("Entity '{$meta->name}' must implemented method '{$method}'.");
+ throw new InvalidArgumentException(sprintf('Entity \'%s\' must implemented method \'%s\'.', $meta->name, $method));
}
if (!class_exists($ret)) {
- throw new InvalidArgumentException("Class '{$ret}' does not exist.");
+ throw new InvalidArgumentException(sprintf('Class \'%s\' does not exist.', $ret));
}
return $ret;
}
}
-
diff --git a/src/Notifications/AdminModule/DefaultPresenter.php b/src/Notifications/AdminModule/DefaultPresenter.php
index 39e8be86..1cd1ab81 100755
--- a/src/Notifications/AdminModule/DefaultPresenter.php
+++ b/src/Notifications/AdminModule/DefaultPresenter.php
@@ -16,8 +16,6 @@
/**
* @author Josef Kříž
- *
- * @secured
*/
class DefaultPresenter extends \Nette\Application\UI\Presenter
{
diff --git a/src/Notifications/AdminModule/EmailPresenter.php b/src/Notifications/AdminModule/EmailPresenter.php
index 3b0e00e5..4704f261 100755
--- a/src/Notifications/AdminModule/EmailPresenter.php
+++ b/src/Notifications/AdminModule/EmailPresenter.php
@@ -15,8 +15,6 @@
/**
* @author Josef Kříž
- *
- * @secured
*/
class EmailPresenter extends \Nette\Application\UI\Presenter
{
diff --git a/src/Notifications/AdminModule/SettingsPresenter.php b/src/Notifications/AdminModule/SettingsPresenter.php
index a9c1fa6a..6a303e49 100755
--- a/src/Notifications/AdminModule/SettingsPresenter.php
+++ b/src/Notifications/AdminModule/SettingsPresenter.php
@@ -11,24 +11,22 @@
namespace Venne\Notifications\AdminModule;
+use Doctrine\ORM\EntityManager;
use Grido\DataSources\Doctrine;
-use Kdyby\Doctrine\EntityDao;
use Venne\Notifications\NotificationManager;
-use Venne\Notifications\NotificationSettingEntity;
+use Venne\Notifications\NotificationSetting;
use Venne\System\Components\AdminGrid\IAdminGridFactory;
/**
* @author Josef Kříž
- *
- * @secured
*/
class SettingsPresenter extends \Nette\Application\UI\Presenter
{
use \Venne\System\AdminPresenterTrait;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $notificationSettingDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $notificationSettingRepository;
/** @var \Venne\Notifications\NotificationManager */
private $notificationManager;
@@ -40,13 +38,13 @@ class SettingsPresenter extends \Nette\Application\UI\Presenter
private $notificationSettingFormFactory;
public function __construct(
- EntityDao $notificationSettingDao,
+ EntityManager $entityManager,
NotificationManager $notificationManager,
IAdminGridFactory $adminGridFactory,
NotificationSettingFormFactory $notificationSettingFormFactory
)
{
- $this->notificationSettingDao = $notificationSettingDao;
+ $this->notificationSettingRepository = $entityManager->getRepository(NotificationSetting::class);
$this->notificationManager = $notificationManager;
$this->adminGridFactory = $adminGridFactory;
$this->notificationSettingFormFactory = $notificationSettingFormFactory;
@@ -65,12 +63,15 @@ public function getNotificationManager()
*/
protected function createComponentTable()
{
- $admin = $this->adminGridFactory->create($this->notificationSettingDao);
+ $qb = $this
+ ->notificationSettingRepository
+ ->createQueryBuilder('a')
+ ->andWhere('a.user = :user')->setParameter('user', $this->presenter->user->identity->getId());
+
+ $admin = $this->adminGridFactory->create($this->notificationSettingRepository);
$table = $admin->getTable();
$table->setTranslator($this->translator);
- $table->setModel(new Doctrine($this->notificationSettingDao->createQueryBuilder('a')
- ->andWhere('a.user = :user')->setParameter('user', $this->presenter->user->identity->getId())
- ));
+ $table->setModel(new Doctrine($qb));
// columns
$table->addColumnText('user', 'User')
@@ -78,7 +79,7 @@ protected function createComponentTable()
$table->addColumnText('type', 'Type')
->setSortable()
- ->setCustomRender(function (NotificationSettingEntity $entity) {
+ ->setCustomRender(function (NotificationSetting $entity) {
return $entity->type !== null
? $this->notificationManager->getType($entity->type->type)->getHumanName()
: '';
@@ -86,7 +87,7 @@ protected function createComponentTable()
->getCellPrototype()->width = '20%';
$table->addColumnText('action', 'Action')
- ->setCustomRender(function (NotificationSettingEntity $entity) {
+ ->setCustomRender(function (NotificationSetting $entity) {
return $entity->type !== null
? $entity->type->action
: '';
diff --git a/src/Notifications/Components/NotificationControl.php b/src/Notifications/Components/NotificationControl.php
index a1f12be1..db468179 100755
--- a/src/Notifications/Components/NotificationControl.php
+++ b/src/Notifications/Components/NotificationControl.php
@@ -11,12 +11,13 @@
namespace Venne\Notifications\Components;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\EntityManager;
use Nette\Application\BadRequestException;
use Nette\Security\User;
use Venne\DataTransfer\DataTransferManager;
use Venne\Notifications\NotificationDto;
use Venne\Notifications\NotificationManager;
+use Venne\Notifications\NotificationUser;
/**
* @author Josef Kříž
@@ -30,14 +31,17 @@ class NotificationControl extends \Venne\System\UI\Control
/** @var \Venne\Notifications\NotificationManager */
private $notificationManager;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $notificationUserDao;
+ /** @var \Doctrine\ORM\EntityManager */
+ private $entityManager;
+
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $notificationUserRepository;
/** @var \Venne\DataTransfer\DataTransferManager */
private $dataTransferManager;
public function __construct(
- EntityDao $notificationUserDao,
+ EntityManager $entityManager,
NotificationManager $notificationManager,
User $user,
DataTransferManager $dataTransferManager
@@ -45,14 +49,15 @@ public function __construct(
{
parent::__construct();
+ $this->entityManager = $entityManager;
+ $this->notificationUserRepository = $entityManager->getRepository(NotificationUser::class);
$this->user = $user;
- $this->notificationUserDao = $notificationUserDao;
$this->notificationManager = $notificationManager;
$this->dataTransferManager = $dataTransferManager;
}
/**
- * @return \Kdyby\Doctrine\EntityDao
+ * @return \Venne\Notifications\NotificationManager
*/
public function getNotificationManager()
{
@@ -64,12 +69,12 @@ public function getNotificationManager()
*/
public function handleRead($id)
{
- if (($entity = $this->notificationUserDao->find($id)) === null) {
+ if (($entity = $this->notificationUserRepository->find($id)) === null) {
throw new BadRequestException;
}
$entity->markRead = true;
- $this->notificationUserDao->save($entity);
+ $this->entityManager->flush($entity);
$this->redirect('this');
}
@@ -79,11 +84,11 @@ public function handleRead($id)
*/
public function handleRemove($id)
{
- if (($entity = $this->notificationUserDao->find($id)) === null) {
+ if (($entity = $this->notificationUserRepository->find($id)) === null) {
throw new BadRequestException;
}
- $this->notificationUserDao->delete($entity);
+ $this->notificationUserRepository->delete($entity);
$this->redirect('this');
}
@@ -91,8 +96,8 @@ public function handleRemove($id)
public function render($id)
{
$this->template->notification = $this->dataTransferManager
- ->createQuery(NotificationDto::getClassName(), function () use ($id) {
- return $this->notificationUserDao->find($id);
+ ->createQuery(NotificationDto::class, function () use ($id) {
+ return $this->notificationUserRepository->find($id);
})
->enableCache()
->fetch();
diff --git a/src/Notifications/Components/NotificationsControl.php b/src/Notifications/Components/NotificationsControl.php
index 8dc0662b..5e6f403c 100755
--- a/src/Notifications/Components/NotificationsControl.php
+++ b/src/Notifications/Components/NotificationsControl.php
@@ -11,10 +11,11 @@
namespace Venne\Notifications\Components;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\EntityManager;
use Venne\DataTransfer\DataTransferManager;
use Venne\Notifications\NotificationDto;
use Venne\Notifications\NotificationManager;
+use Venne\Notifications\NotificationUser;
/**
* @author Josef Kříž
@@ -25,8 +26,8 @@ class NotificationsControl extends \Venne\System\UI\Control
/** @var \Venne\Notifications\NotificationManager */
private $notificationManager;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $notificationUserDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $notificationUserRepository;
/** @var \Venne\Notifications\Components\INotificationControlFactory */
private $notificationControlFactory;
@@ -35,7 +36,7 @@ class NotificationsControl extends \Venne\System\UI\Control
private $dataTransferManager;
public function __construct(
- EntityDao $notificationUserDao,
+ EntityManager $entityManager,
NotificationManager $notificationManager,
INotificationControlFactory $notificationControlFactory,
DataTransferManager $dataTransferManager
@@ -43,7 +44,7 @@ public function __construct(
{
parent::__construct();
- $this->notificationUserDao = $notificationUserDao;
+ $this->notificationUserRepository = $entityManager->getRepository(NotificationUser::class);
$this->notificationManager = $notificationManager;
$this->notificationControlFactory = $notificationControlFactory;
$this->dataTransferManager = $dataTransferManager;
@@ -60,7 +61,7 @@ protected function createComponentNotification()
public function render()
{
$this->template->notifications = $this->dataTransferManager
- ->createQuery(NotificationDto::getClassName(), function () {
+ ->createQuery(NotificationDto::class, function () {
return $this->notificationManager->getNotifications(5);
})
->enableCache()
diff --git a/src/Notifications/DI/NotificationsExtension.php b/src/Notifications/DI/NotificationsExtension.php
index f8cc8973..456eb3e6 100755
--- a/src/Notifications/DI/NotificationsExtension.php
+++ b/src/Notifications/DI/NotificationsExtension.php
@@ -39,13 +39,7 @@ public function loadConfiguration()
$config = $this->getConfig($this->defaults);
$notificationManager = $container->addDefinition($this->prefix('notificationManager'))
- ->setClass('Venne\Notifications\NotificationManager', array(
- new Statement('@doctrine.dao', array('Venne\Notifications\NotificationEntity')),
- new Statement('@doctrine.dao', array('Venne\Notifications\NotificationUserEntity')),
- new Statement('@doctrine.dao', array('Venne\Notifications\NotificationSettingEntity')),
- new Statement('@doctrine.dao', array('Venne\Notifications\NotificationTypeEntity')),
- new Statement('@doctrine.dao', array('Venne\Security\UserEntity'))
- ));
+ ->setClass('Venne\Notifications\NotificationManager');
$container->addDefinition($this->prefix('emailManager'))
->setClass('Venne\Notifications\EmailManager', array(
@@ -55,43 +49,31 @@ public function loadConfiguration()
foreach ($this->compiler->extensions as $extension) {
if ($extension instanceof IEventProvider) {
- foreach ($extension->getEventTypes() as $type)
+ foreach ($extension->getEventTypes() as $type) {
$notificationManager->addSetup('$service->addType(?)', array(new Statement($type, array())));
+ }
}
}
$container->addDefinition($this->prefix('notificationControl'))
- ->setArguments(array(
- new Statement('@doctrine.dao', array('Venne\Notifications\NotificationUserEntity'))
- ))
->setImplement('Venne\Notifications\Components\INotificationControlFactory')
->setInject(true);
$container->addDefinition($this->prefix('notificationsControl'))
- ->setArguments(array(
- new Statement('@doctrine.dao', array('Venne\Notifications\NotificationUserEntity'))
- ))
->setImplement('Venne\Notifications\Components\INotificationsControlFactory')
->addTag(SystemExtension::TAG_TRAY_COMPONENT)
->addTag(WidgetsExtension::TAG_WIDGET, 'notifications')
->setInject(true);
$container->addDefinition($this->prefix('settingsPresenter'))
- ->setClass('Venne\Notifications\AdminModule\SettingsPresenter', array(new Statement('@doctrine.dao', array('Venne\Notifications\NotificationSettingEntity'))));
+ ->setClass('Venne\Notifications\AdminModule\SettingsPresenter');
// Jobs
$container->addDefinition($this->prefix('notificationJob'))
- ->setClass('Venne\Notifications\Jobs\NotificationJob', array(
- new Statement('@doctrine.dao', array('Venne\Notifications\NotificationEntity')),
- new Statement('@doctrine.dao', array('Venne\Notifications\NotificationUserEntity')),
- new Statement('@doctrine.dao', array('Venne\Notifications\NotificationSettingEntity')),
- ));
+ ->setClass('Venne\Notifications\Jobs\NotificationJob');
$container->addDefinition($this->prefix('emailJob'))
- ->setClass('Venne\Notifications\Jobs\EmailJob', array(
- new Statement('@doctrine.dao', array('Venne\Notifications\NotificationUserEntity')),
- new Statement('@doctrine.dao', array('Venne\Security\UserEntity')),
- ));
+ ->setClass('Venne\Notifications\Jobs\EmailJob');
$container->addDefinition($this->prefix('notificationSettingFormFactory'))
->setClass('Venne\Notifications\AdminModule\NotificationSettingFormFactory', array(new Statement('@system.admin.ajaxFormFactory')));
@@ -133,7 +115,7 @@ public function getPresenterMapping()
public function getEntityMappings()
{
return array(
- 'Venne\Notifications' => dirname(__DIR__) . '/*Entity.php',
+ 'Venne\Notifications' => dirname(__DIR__) . '/*.php',
);
}
diff --git a/src/Notifications/EmailManager.php b/src/Notifications/EmailManager.php
index b2cb4de7..cd97b398 100644
--- a/src/Notifications/EmailManager.php
+++ b/src/Notifications/EmailManager.php
@@ -78,14 +78,14 @@ public function send($user, $name, $type, $action, array $templateArgs = array()
}
if (!$response instanceof TextResponse) {
- throw new InvalidArgumentException("Type '$type' does not exist.");
+ throw new InvalidArgumentException(sprintf('Type \'%s\' does not exist.', $type));
}
try {
$data = (string) $response->getSource();
} catch (\Nette\Application\BadRequestException $e) {
if (Strings::startsWith($e->getMessage(), 'Page not found. Missing template')) {
- throw new InvalidArgumentException("Type '$type' does not exist.");
+ throw new InvalidArgumentException(sprintf('Type \'%s\' does not exist.', $type));
}
}
diff --git a/src/Notifications/Event.php b/src/Notifications/Event.php
index a3ad458a..086f357f 100755
--- a/src/Notifications/Event.php
+++ b/src/Notifications/Event.php
@@ -26,10 +26,10 @@ public static function getName()
}
/**
- * @param \Venne\Notifications\NotificationEntity $log
+ * @param \Venne\Notifications\Notification $log
* @return null
*/
- public static function getLink(NotificationEntity $log)
+ public static function getLink(Notification $log)
{
return null;
}
diff --git a/src/Notifications/EventManager.php b/src/Notifications/EventManager.php
index 3dbb8aac..98e85d86 100755
--- a/src/Notifications/EventManager.php
+++ b/src/Notifications/EventManager.php
@@ -30,4 +30,3 @@ public function dispatchEvent($eventName, EventArgs $eventArgs = null)
}
}
-
diff --git a/src/Notifications/IEvent.php b/src/Notifications/IEvent.php
index 568b33bc..4caeb260 100755
--- a/src/Notifications/IEvent.php
+++ b/src/Notifications/IEvent.php
@@ -28,9 +28,9 @@ public static function getName();
public static function getHumanName();
/**
- * @param \Venne\Notifications\NotificationEntity $log
+ * @param \Venne\Notifications\Notification $log
* @return string
*/
- public static function getLink(NotificationEntity $log);
+ public static function getLink(Notification $log);
}
diff --git a/src/Notifications/Jobs/EmailJob.php b/src/Notifications/Jobs/EmailJob.php
index aaed94b3..4471ec05 100755
--- a/src/Notifications/Jobs/EmailJob.php
+++ b/src/Notifications/Jobs/EmailJob.php
@@ -11,45 +11,46 @@
namespace Venne\Notifications\Jobs;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\EntityManager;
use Venne\Notifications\EmailManager;
use Venne\Notifications\Events\NotificationEvent;
-use Venne\Queue\JobEntity;
+use Venne\Notifications\NotificationUser;
+use Venne\Queue\Job;
+use Venne\Security\User;
/**
* @author Josef Kříž
*/
-class EmailJob extends \Venne\Queue\Job
+class EmailJob extends \Venne\Queue\JobType
{
- /** @var \Kdyby\Doctrine\EntityDao */
- private $notificationUserDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $notificationUserRepository;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $userDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $userRepository;
/** @var \Venne\Notifications\EmailManager */
private $emailManager;
public function __construct(
- EntityDao $notificationUserDao,
- EntityDao $userDao,
+ EntityManager $entityManager,
EmailManager $emailManager
)
{
- $this->notificationUserDao = $notificationUserDao;
- $this->userDao = $userDao;
+ $this->notificationUserRepository = $entityManager->getRepository(NotificationUser::class);
+ $this->userRepository = $entityManager->getRepository(User::class);
$this->emailManager = $emailManager;
}
/**
- * @param \Venne\Queue\JobEntity $jobEntity
+ * @param \Venne\Queue\Job $job
* @param integer $priority
*/
- public function run(JobEntity $jobEntity, $priority)
+ public function run(Job $job, $priority)
{
- $user = $this->userDao->find($jobEntity->arguments['user']);
- $notificationUser = $this->notificationUserDao->find($jobEntity->arguments['notification']);
+ $user = $this->userRepository->find($job->arguments['user']);
+ $notificationUser = $this->notificationUserRepository->find($job->arguments['notification']);
$notification = $notificationUser->getNotification();
$this->emailManager->send($user->email, $user->name, NotificationEvent::getName(), 'userNotification', array(
diff --git a/src/Notifications/Jobs/NotificationJob.php b/src/Notifications/Jobs/NotificationJob.php
index a0f9c39d..7a4adfb1 100755
--- a/src/Notifications/Jobs/NotificationJob.php
+++ b/src/Notifications/Jobs/NotificationJob.php
@@ -11,59 +11,61 @@
namespace Venne\Notifications\Jobs;
-use Kdyby\Doctrine\EntityDao;
-use Venne\Notifications\NotificationEntity;
-use Venne\Notifications\NotificationUserEntity;
+use Doctrine\ORM\EntityManager;
+use Venne\Notifications\Notification;
+use Venne\Notifications\NotificationSetting;
+use Venne\Notifications\NotificationUser;
use Venne\Queue\Job;
-use Venne\Queue\JobEntity;
use Venne\Queue\JobManager;
-use Venne\Security\UserEntity;
+use Venne\Security\User;
/**
* @author Josef Kříž
*/
-class NotificationJob extends Job
+class NotificationJob extends \Venne\Queue\JobType
{
- /** @var \Kdyby\Doctrine\EntityDao */
- private $notificationDao;
+ /** @var \Doctrine\ORM\EntityManager */
+ private $entityManager;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $notificationUserDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $notificationRepository;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $settingDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $notificationUserRepository;
+
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $notificationSettingRepository;
/** @var \Venne\Queue\JobManager */
private $jobManager;
public function __construct(
- EntityDao $notificationDao,
- EntityDao $userDao,
- EntityDao $settingDao,
+ EntityManager $entityManager,
JobManager $jobManager
)
{
- $this->notificationDao = $notificationDao;
- $this->notificationUserDao = $userDao;
- $this->settingDao = $settingDao;
+ $this->entityManager = $entityManager;
+ $this->notificationRepository = $entityManager->getRepository(Notification::class);
+ $this->notificationUserRepository = $entityManager->getRepository(NotificationUser::class);
+ $this->notificationSettingRepository = $entityManager->getRepository(NotificationSetting::class);
$this->jobManager = $jobManager;
}
/**
- * @param \Venne\Queue\JobEntity $jobEntity
+ * @param \Venne\Queue\Job $job
* @param integer $priority
*/
- public function run(JobEntity $jobEntity, $priority)
+ public function run(Job $job, $priority)
{
- /** @var NotificationEntity $notificationEntity */
- $notificationEntity = $this->notificationDao->find($jobEntity->arguments[0]);
+ /** @var Notification $notificationEntity */
+ $notificationEntity = $this->notificationRepository->find($job->arguments[0]);
if ($notificationEntity === null) {
return;
}
- $qb = $this->settingDao->createQueryBuilder('a')
+ $qb = $this->notificationSettingRepository->createQueryBuilder('a')
->andWhere('a.type IS NULL OR a.type = :type')->setParameter('type', $notificationEntity->type->id);
if ($notificationEntity->user) {
@@ -93,13 +95,15 @@ public function run(JobEntity $jobEntity, $priority)
}
foreach ($users as $user) {
- $notificationUserEntity = new NotificationUserEntity($notificationEntity, $user['user']);
- $this->notificationUserDao->save($notificationUserEntity);
+ $notificationUser = new NotificationUser($notificationEntity, $user['user']);
+
+ $this->entityManager->persist($notificationUser);
+ $this->entityManager->flush($notificationUser);
if ($user['email']) {
- $this->jobManager->scheduleJob(new JobEntity(EmailJob::getName(), null, array(
- 'user' => $user['user'] instanceof UserEntity ? $user['user']->id : $user['user'],
- 'notification' => $notificationUserEntity->id,
+ $this->jobManager->scheduleJob(new Job(EmailJob::getName(), null, array(
+ 'user' => $user['user'] instanceof User ? $user['user']->id : $user['user'],
+ 'notification' => $notificationUser->id,
)), $priority);
}
}
diff --git a/src/Notifications/Notification.php b/src/Notifications/Notification.php
new file mode 100644
index 00000000..f0a5ae9d
--- /dev/null
+++ b/src/Notifications/Notification.php
@@ -0,0 +1,127 @@
+
+ *
+ * @property \Venne\Notifications\NotificationType $type
+ * @property \Venne\Security\User $user
+ * @property \DateTime $created
+ * @property string $target
+ * @property int $targetKey
+ *
+ * @ORM\Entity
+ * @ORM\Table(name="notification", indexes={@ORM\Index(name="created_idx", columns={"created"})})
+ */
+class Notification extends \Kdyby\Doctrine\Entities\BaseEntity
+{
+
+ use \Venne\Doctrine\Entities\IdentifiedEntityTrait;
+
+ /**
+ * @var \Venne\Notifications\NotificationType
+ *
+ * @ORM\ManyToOne(targetEntity="NotificationType")
+ * @ORM\JoinColumn(onDelete="CASCADE")
+ */
+ private $type;
+
+ /**
+ * @var \Venne\Security\User
+ *
+ * @ORM\ManyToOne(targetEntity="\Venne\Security\User")
+ * @ORM\JoinColumn(onDelete="CASCADE")
+ */
+ private $user;
+
+ /**
+ * @var \DateTime
+ *
+ * @ORM\Column(type="datetime")
+ */
+ private $created;
+
+ /**
+ * @var string|null
+ *
+ * @ORM\Column(type="string", nullable=true)
+ */
+ private $target;
+
+ /**
+ * @var integer|null
+ *
+ * @ORM\Column(type="integer", nullable=true)
+ */
+ private $targetKey;
+
+ /**
+ * @param \Venne\Notifications\NotificationType $type
+ * @param \Venne\Security\User $user
+ * @param string|null $target
+ * @param integer|null $targetKey
+ */
+ public function __construct(NotificationType $type, User $user, $target = null, $targetKey = null)
+ {
+ $this->type = $type;
+ $this->user = $user;
+ $this->target = $target;
+ $this->targetKey = $targetKey;
+ $this->created = new DateTime();
+ }
+
+ /**
+ * @return \DateTime
+ */
+ public function getCreated()
+ {
+ return $this->created;
+ }
+
+ /**
+ * @return string
+ */
+ public function getTarget()
+ {
+ return $this->target;
+ }
+
+ /**
+ * @return integer
+ */
+ public function getTargetKey()
+ {
+ return $this->targetKey;
+ }
+
+ /**
+ * @return \Venne\Notifications\NotificationType
+ */
+ public function getType()
+ {
+ return $this->type;
+ }
+
+ /**
+ * @return \Venne\Security\User
+ */
+ public function getUser()
+ {
+ return $this->user;
+ }
+
+}
diff --git a/src/Notifications/NotificationDto.php b/src/Notifications/NotificationDto.php
index ef4a302c..9de1523b 100755
--- a/src/Notifications/NotificationDto.php
+++ b/src/Notifications/NotificationDto.php
@@ -58,7 +58,7 @@ protected function getTargetKey()
*/
protected function getUserName()
{
- return (string)$this->getNotification()->user;
+ return (string) $this->getNotification()->user;
}
/**
@@ -66,7 +66,7 @@ protected function getUserName()
*/
protected function getUserEmail()
{
- return (string)$this->getNotification()->user->email;
+ return (string) $this->getNotification()->user->email;
}
/**
@@ -86,7 +86,7 @@ protected function getMessage()
}
/**
- * @return NotificationEntity
+ * @return Notification
*/
private function getNotification()
{
diff --git a/src/Notifications/NotificationEntity.php b/src/Notifications/NotificationEntity.php
deleted file mode 100755
index 8e4a7a77..00000000
--- a/src/Notifications/NotificationEntity.php
+++ /dev/null
@@ -1,89 +0,0 @@
-
- *
- * @property \Venne\Notifications\NotificationTypeEntity $type
- * @property \Venne\Security\UserEntity $user
- * @property \DateTime $created
- * @property string $target
- * @property int $targetKey
- *
- * @ORM\Entity
- * @ORM\Table(name="notification", indexes={@ORM\Index(name="created_idx", columns={"created"})})
- */
-class NotificationEntity extends \Kdyby\Doctrine\Entities\BaseEntity
-{
-
- use \Venne\Doctrine\Entities\IdentifiedEntityTrait;
-
- /**
- * @var \Venne\Notifications\NotificationTypeEntity
- *
- * @ORM\ManyToOne(targetEntity="NotificationTypeEntity")
- * @ORM\JoinColumn(onDelete="CASCADE")
- */
- protected $type;
-
- /**
- * @var \Venne\Security\UserEntity
- *
- * @ORM\ManyToOne(targetEntity="\Venne\Security\UserEntity")
- * @ORM\JoinColumn(onDelete="CASCADE")
- */
- protected $user;
-
- /**
- * @var \DateTime
- *
- * @ORM\Column(type="datetime")
- */
- protected $created;
-
- /**
- * @var string|null
- *
- * @ORM\Column(type="string", nullable=true)
- */
- protected $target;
-
- /**
- * @var int|null
- *
- * @ORM\Column(type="integer", nullable=true)
- */
- protected $targetKey;
-
- public function __construct(NotificationTypeEntity $type)
- {
- $this->type = $type;
- $this->created = new DateTime();
- }
-
- public function setType(NotificationTypeEntity $type)
- {
- $this->type = $type;
- }
-
- public function setUser(UserEntity $user = null)
- {
- $this->user = $user;
- }
-
-}
-
diff --git a/src/Notifications/NotificationManager.php b/src/Notifications/NotificationManager.php
index 31b62560..0a7a977b 100755
--- a/src/Notifications/NotificationManager.php
+++ b/src/Notifications/NotificationManager.php
@@ -11,16 +11,14 @@
namespace Venne\Notifications;
+use Doctrine\ORM\EntityManager;
use Kdyby\Doctrine\Entities\BaseEntity;
-use Kdyby\Doctrine\EntityDao;
-use Kdyby\Doctrine\EntityManager;
use Nette\InvalidArgumentException;
-use Nette\Security\User;
+use Nette\Security\User as NetteUser;
use Venne\Notifications\Jobs\NotificationJob;
-use Venne\Notifications\Jobs\NotifyJob;
-use Venne\Queue\JobEntity;
+use Venne\Queue\Job;
use Venne\Queue\JobManager;
-use Venne\Security\UserEntity;
+use Venne\Security\User;
/**
* @author Josef Kříž
@@ -38,44 +36,36 @@ class NotificationManager extends \Nette\Object
/** @var \Venne\Queue\JobManager */
private $jobManager;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $logDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $notificationUserRepository;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $notificationDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $settingRepository;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $settingDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $typeRepository;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $typeDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $userRepository;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $userDao;
-
- /** @var \Nette\Security\User */
- private $user;
+ /** @var \Nette\Security\NetteUser */
+ private $netteUser;
/** @var \Venne\Notifications\IEvent[] */
private $types = array();
public function __construct(
- EntityDao $logDao,
- EntityDao $notificationDao,
- EntityDao $settingDao,
- EntityDao $typeDao,
- EntityDao $userDao,
- User $user,
+ EntityManager $entityManager,
+ NetteUser $netteUser,
EntityManager $entityManager,
JobManager $jobManager
)
{
- $this->logDao = $logDao;
- $this->notificationDao = $notificationDao;
- $this->settingDao = $settingDao;
- $this->typeDao = $typeDao;
- $this->userDao = $userDao;
- $this->user = $user;
+ $this->notificationUserRepository = $entityManager->getRepository(NotificationUser::class);
+ $this->settingRepository = $entityManager->getRepository(NotificationSetting::class);
+ $this->typeRepository = $entityManager->getRepository(NotificationType::class);
+ $this->userRepository = $entityManager->getRepository(User::class);
+ $this->netteUser = $netteUser;
$this->entityManager = $entityManager;
$this->jobManager = $jobManager;
}
@@ -98,7 +88,7 @@ public function addType(IEvent $event)
public function getType($name)
{
if (!isset($this->types[$name])) {
- throw new InvalidArgumentException("Type '$name' does not exist.");
+ throw new InvalidArgumentException(sprintf('Type \'%s\' does not exist.', $name));
}
return $this->types[$name];
@@ -117,17 +107,17 @@ public function getTypes()
* @param string|null $target
* @param string|null $action
* @param string|null $message
- * @param \Venne\Security\UserEntity|null $user
+ * @param \Venne\Security\User|null $user
* @param integer $priority
*/
- public function notify($type, $target = null, $action = null, $message = null, UserEntity $user = null, $priority = NotificationManager::PRIORITY_DEFAULT)
+ public function notify($type, $target = null, $action = null, $message = null, User $user = null, $priority = NotificationManager::PRIORITY_DEFAULT)
{
if (!isset($this->types[$type])) {
- throw new InvalidArgumentException("Type '$type' does not exist.");
+ throw new InvalidArgumentException(sprintf('Type \'%s\' does not exist.', $type));
}
if ($target && !is_object($target)) {
- throw new InvalidArgumentException("Target must be object");
+ throw new InvalidArgumentException('Target must be object');
}
$targetKey = $this->detectPrimaryKey($target);
@@ -141,25 +131,24 @@ public function notify($type, $target = null, $action = null, $message = null, U
$typeEntity = $this->getTypeEntity($type, $action, $message);
- $notificationEntity = new NotificationEntity($typeEntity);
- $notificationEntity->user = $user = $user !== null ? $user : $this->getUser();
- $notificationEntity->target = $target;
- $notificationEntity->targetKey = $targetKey;
- $this->logDao->save($notificationEntity);
+ $user = $user !== null ? $user : $this->getUser();
+ $notification = new Notification($typeEntity, $user, $target, $targetKey);
+ $this->entityManager->persist($notification);
+ $this->entityManager->flush();
- $jobEntity = new JobEntity(NotificationJob::getName(), null, array($notificationEntity->id));
- $jobEntity->user = $user;
+ $job = new Job(NotificationJob::getName(), null, array($notification->id));
+ $job->user = $user;
- $this->jobManager->scheduleJob($jobEntity, $priority);
+ $this->jobManager->scheduleJob($job, $priority);
}
/**
* @param int|null $limit
- * @return NotificationUserEntity[]
+ * @return NotificationUser[]
*/
public function getNotifications($limit = null)
{
- return $this->notificationDao->createQueryBuilder('a')
+ return $this->notificationUserRepository->createQueryBuilder('a')
->leftJoin('a.notification', 'l')
->andWhere('a.user = :user')->setParameter('user', $this->getUser()->getId())
->orderBy('l.created', 'DESC')
@@ -172,7 +161,7 @@ public function getNotifications($limit = null)
*/
public function countNotifications()
{
- return $this->notificationDao->createQueryBuilder('a')
+ return $this->notificationUserRepository->createQueryBuilder('a')
->select('COUNT(a.id)')
->leftJoin('a.notification', 'l')
->andWhere('a.user = :user')->setParameter('user', $this->getUser()->getId())
@@ -180,27 +169,29 @@ public function countNotifications()
}
/**
- * @return \Venne\Security\UserEntity|null
+ * @return \Venne\Security\User|null
*/
private function getUser()
{
- return $this->user->isLoggedIn() ? $this->userDao->find($this->user->getIdentity()->getId()) : null;
+ return $this->netteUser->isLoggedIn() ? $this->userRepository->find($this->netteUser->getIdentity()->getId()) : null;
}
/**
* @param string $type
* @param string|null $action
* @param string|null $message
- * @return \Venne\Notifications\NotificationTypeEntity
+ * @return \Venne\Notifications\NotificationType
*/
private function getTypeEntity($type, $action = null, $message = null)
{
- if (($typeEntity = $this->typeDao->findOneBy(array('type' => $type, 'action' => $action, 'message' => $message))) === null) {
- $typeEntity = new NotificationTypeEntity;
+ if (($typeEntity = $this->typeRepository->findOneBy(array('type' => $type, 'action' => $action, 'message' => $message))) === null) {
+ $typeEntity = new NotificationType;
$typeEntity->type = $type;
$typeEntity->action = $action;
$typeEntity->message = $message;
- $this->typeDao->save($typeEntity);
+
+ $this->entityManager->persist($typeEntity);
+ $this->entityManager->flush($typeEntity);
}
return $typeEntity;
@@ -221,4 +212,3 @@ private function detectPrimaryKey(BaseEntity $object)
}
}
-
diff --git a/src/Notifications/NotificationSettingEntity.php b/src/Notifications/NotificationSetting.php
old mode 100755
new mode 100644
similarity index 74%
rename from src/Notifications/NotificationSettingEntity.php
rename to src/Notifications/NotificationSetting.php
index f07c4364..d2580a50
--- a/src/Notifications/NotificationSettingEntity.php
+++ b/src/Notifications/NotificationSetting.php
@@ -19,15 +19,15 @@
* @ORM\Entity
* @ORM\Table(name="notification_setting")
*/
-class NotificationSettingEntity extends \Kdyby\Doctrine\Entities\BaseEntity
+class NotificationSetting extends \Kdyby\Doctrine\Entities\BaseEntity
{
use \Venne\Doctrine\Entities\IdentifiedEntityTrait;
/**
- * @var \Venne\Notifications\NotificationTypeEntity
+ * @var \Venne\Notifications\NotificationType
*
- * @ORM\ManyToOne(targetEntity="NotificationTypeEntity")
+ * @ORM\ManyToOne(targetEntity="NotificationType")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
protected $type;
@@ -40,9 +40,9 @@ class NotificationSettingEntity extends \Kdyby\Doctrine\Entities\BaseEntity
protected $target;
/**
- * @var \Venne\Security\UserEntity
+ * @var \Venne\Security\User
*
- * @ORM\ManyToOne(targetEntity="\Venne\Security\UserEntity")
+ * @ORM\ManyToOne(targetEntity="\Venne\Security\User")
* @ORM\JoinColumn(onDelete="SET NULL")
*/
protected $targetUser;
@@ -55,9 +55,9 @@ class NotificationSettingEntity extends \Kdyby\Doctrine\Entities\BaseEntity
protected $targetKey;
/**
- * @var \Venne\Security\UserEntity
+ * @var \Venne\Security\User
*
- * @ORM\ManyToOne(targetEntity="\Venne\Security\UserEntity")
+ * @ORM\ManyToOne(targetEntity="\Venne\Security\User")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
protected $user;
@@ -77,4 +77,3 @@ class NotificationSettingEntity extends \Kdyby\Doctrine\Entities\BaseEntity
protected $selfNotification = false;
}
-
diff --git a/src/Notifications/NotificationTypeEntity.php b/src/Notifications/NotificationType.php
old mode 100755
new mode 100644
similarity index 93%
rename from src/Notifications/NotificationTypeEntity.php
rename to src/Notifications/NotificationType.php
index 8974820b..d74d7734
--- a/src/Notifications/NotificationTypeEntity.php
+++ b/src/Notifications/NotificationType.php
@@ -20,7 +20,7 @@
* @ORM\Entity
* @ORM\Table(name="notification_type")
*/
-class NotificationTypeEntity extends \Kdyby\Doctrine\Entities\BaseEntity
+class NotificationType extends \Kdyby\Doctrine\Entities\BaseEntity
{
use \Venne\Doctrine\Entities\IdentifiedEntityTrait;
@@ -55,4 +55,3 @@ public function __toString()
}
}
-
diff --git a/src/Notifications/NotificationUserEntity.php b/src/Notifications/NotificationUser.php
old mode 100755
new mode 100644
similarity index 69%
rename from src/Notifications/NotificationUserEntity.php
rename to src/Notifications/NotificationUser.php
index cd24f936..629b7666
--- a/src/Notifications/NotificationUserEntity.php
+++ b/src/Notifications/NotificationUser.php
@@ -12,7 +12,7 @@
namespace Venne\Notifications;
use Doctrine\ORM\Mapping as ORM;
-use Venne\Security\UserEntity;
+use Venne\Security\User;
/**
* @author Josef Kříž
@@ -20,23 +20,23 @@
* @ORM\Entity
* @ORM\Table(name="notification_user")
*/
-class NotificationUserEntity extends \Kdyby\Doctrine\Entities\BaseEntity
+class NotificationUser extends \Kdyby\Doctrine\Entities\BaseEntity
{
use \Venne\Doctrine\Entities\IdentifiedEntityTrait;
/**
- * @var \Venne\Security\UserEntity
+ * @var \Venne\Security\User
*
- * @ORM\ManyToOne(targetEntity="\Venne\Security\UserEntity")
+ * @ORM\ManyToOne(targetEntity="\Venne\Security\User")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
protected $user;
/**
- * @var \Venne\Security\UserEntity
+ * @var \Venne\Security\User
*
- * @ORM\ManyToOne(targetEntity="\Venne\Notifications\NotificationEntity")
+ * @ORM\ManyToOne(targetEntity="\Venne\Notifications\Notification")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
protected $notification;
@@ -48,21 +48,20 @@ class NotificationUserEntity extends \Kdyby\Doctrine\Entities\BaseEntity
*/
protected $markRead = false;
- public function __construct(NotificationEntity $notification, UserEntity $user)
+ public function __construct(Notification $notification, User $user)
{
$this->notification = $notification;
$this->user = $user;
}
- public function setNotification(NotificationEntity $log)
+ public function setNotification(Notification $log)
{
$this->notification = $log;
}
- public function setUser(UserEntity $user)
+ public function setUser(User $user)
{
$this->user = $user;
}
}
-
diff --git a/src/Queue/AdminModule/DefaultPresenter.php b/src/Queue/AdminModule/DefaultPresenter.php
index a156abfc..89706a71 100755
--- a/src/Queue/AdminModule/DefaultPresenter.php
+++ b/src/Queue/AdminModule/DefaultPresenter.php
@@ -18,8 +18,6 @@
/**
* @author Josef Kříž
- *
- * @Secured
*/
class DefaultPresenter extends \Nette\Application\UI\Presenter
{
diff --git a/src/Queue/AdminModule/JobFormFactory.php b/src/Queue/AdminModule/JobFormFactory.php
index e322fb25..168ab091 100644
--- a/src/Queue/AdminModule/JobFormFactory.php
+++ b/src/Queue/AdminModule/JobFormFactory.php
@@ -12,7 +12,7 @@
namespace Venne\Queue\AdminModule;
use Venne\Forms\IFormFactory;
-use Venne\Queue\JobEntity;
+use Venne\Queue\Job;
use Venne\Queue\JobManager;
/**
@@ -47,14 +47,14 @@ public function create()
->addRule($form::FILLED);
$form->addSelect('state', 'State')
- ->setItems(array(JobEntity::STATE_SCHEDULED, JobEntity::STATE_IN_PROGRESS, JobEntity::STATE_FAILED), false)
+ ->setItems(array(Job::STATE_SCHEDULED, Job::STATE_IN_PROGRESS, Job::STATE_FAILED), false)
->addRule($form::FILLED);
$form->addSelect('priority', 'priority')
->setItems(array(
- JobEntity::PRIORITY_LOW => 'low',
- JobEntity::PRIORITY_NORMAL => 'normal',
- JobEntity::PRIORITY_HIGH => 'high',
+ Job::PRIORITY_LOW => 'low',
+ Job::PRIORITY_NORMAL => 'normal',
+ Job::PRIORITY_HIGH => 'high',
))
->addRule($form::FILLED);
@@ -67,9 +67,6 @@ public function create()
$form->addText('round', 'Round')
->addCondition($form::FILLED)->addRule($form::INTEGER);
- $form->setCurrentGroup();
- $form->addSubmit('_submit', 'Save');
-
return $form;
}
diff --git a/src/Queue/AdminModule/JobFormService.php b/src/Queue/AdminModule/JobFormService.php
new file mode 100644
index 00000000..403a29b2
--- /dev/null
+++ b/src/Queue/AdminModule/JobFormService.php
@@ -0,0 +1,54 @@
+
+ */
+class JobFormService extends \Venne\System\DoctrineFormService
+{
+
+ public function __construct(
+ JobFormFactory $formFactory,
+ EntityManager $entityManager,
+ EntityFormMapper $entityFormMapper
+ )
+ {
+ parent::__construct($formFactory, $entityManager, $entityFormMapper);
+ }
+
+ /**
+ * @return string
+ */
+ protected function getEntityClassName()
+ {
+ return Job::class;
+ }
+
+ protected function error(Form $form, \Exception $e)
+ {
+ if ($e instanceof \Kdyby\Doctrine\DuplicateEntryException) {
+ $form['name']->addError($form->getTranslator()->translate('Name must be unique.'));
+
+ return;
+ }
+
+ parent::error($form, $e);
+ }
+
+}
diff --git a/src/Queue/AdminModule/JobsPresenter.php b/src/Queue/AdminModule/JobsPresenter.php
index b8421bea..6643e0c3 100755
--- a/src/Queue/AdminModule/JobsPresenter.php
+++ b/src/Queue/AdminModule/JobsPresenter.php
@@ -11,23 +11,22 @@
namespace Venne\Queue\AdminModule;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\EntityManager;
+use Venne\Queue\Job;
use Venne\Queue\JobFailedException;
use Venne\Queue\JobManager;
use Venne\System\Components\AdminGrid\IAdminGridFactory;
/**
* @author Josef Kříž
- *
- * @Secured
*/
class JobsPresenter extends \Nette\Application\UI\Presenter
{
use \Venne\System\AdminPresenterTrait;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $jobDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $jobRepository;
/** @var \Venne\Queue\JobManager */
private $jobManager;
@@ -35,20 +34,20 @@ class JobsPresenter extends \Nette\Application\UI\Presenter
/** @var \Venne\System\Components\AdminGrid\IAdminGridFactory */
private $adminGridFactory;
- /** @var \Venne\Queue\AdminModule\JobFormFactory */
- private $jobFormFactory;
+ /** @var \Venne\Queue\AdminModule\JobFormService */
+ private $jobFormService;
public function __construct(
- EntityDao $jobDao,
+ EntityManager $entityManager,
JobManager $jobManager,
IAdminGridFactory $adminGridFactory,
- JobFormFactory $jobFormFactory
+ JobFormService $jobFormService
)
{
- $this->jobDao = $jobDao;
+ $this->jobRepository = $entityManager->getRepository(Job::class);
$this->jobManager = $jobManager;
$this->adminGridFactory = $adminGridFactory;
- $this->jobFormFactory = $jobFormFactory;
+ $this->jobFormService = $jobFormService;
}
/**
@@ -56,7 +55,7 @@ public function __construct(
*/
protected function createComponentTable()
{
- $admin = $this->adminGridFactory->create($this->jobDao);
+ $admin = $this->adminGridFactory->create($this->jobRepository);
$table = $admin->getTable();
$table->addColumnText('type', 'Type')
@@ -78,9 +77,8 @@ protected function createComponentTable()
->getCellPrototype()->width = '10%';
// actions
- $table->addActionEvent('run', 'Run')
- ->onClick[] = function ($id) {
- $job = $this->jobDao->find($id);
+ $table->addActionEvent('run', 'Run')->onClick[] = function ($id) {
+ $job = $this->jobRepository->find($id);
try {
$this->jobManager->scheduleJob($job, JobManager::PRIORITY_REALTIME);
@@ -94,7 +92,9 @@ protected function createComponentTable()
$table->addActionEvent('edit', 'Edit')
->getElementPrototype()->class[] = 'ajax';
- $form = $admin->addForm('job', 'Job', $this->jobFormFactory);
+ $form = $admin->addForm('job', 'Job', function (Job $job = null) {
+ return $this->jobFormService->getFormFactory($job !== null ? $job->getId() : null);
+ });
$admin->connectFormWithAction($form, $table->getAction('edit'));
diff --git a/src/Queue/AdminModule/WorkerPresenter.php b/src/Queue/AdminModule/WorkerPresenter.php
index a3f6da50..2b46abca 100755
--- a/src/Queue/AdminModule/WorkerPresenter.php
+++ b/src/Queue/AdminModule/WorkerPresenter.php
@@ -17,8 +17,6 @@
/**
* @author Josef Kříž
- *
- * @Secured
*/
class WorkerPresenter extends \Nette\Application\UI\Presenter
{
diff --git a/src/Queue/Components/JobControl.php b/src/Queue/Components/JobControl.php
index e8f04cfc..f97cd09a 100755
--- a/src/Queue/Components/JobControl.php
+++ b/src/Queue/Components/JobControl.php
@@ -11,12 +11,12 @@
namespace Venne\Queue\Components;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\EntityManager;
use Nette\Application\BadRequestException;
use Nette\Security\User;
use Venne\DataTransfer\DataTransferManager;
use Venne\Queue\JobDto;
-use Venne\Queue\JobEntity;
+use Venne\Queue\Job;
/**
* @author Josef Kříž
@@ -27,21 +27,21 @@ class JobControl extends \Venne\System\UI\Control
/** @var \Nette\Security\User */
private $user;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $jobDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $jobRepository;
/** @var \Venne\DataTransfer\DataTransferManager */
private $dataTransferManager;
public function __construct(
- EntityDao $jobDao,
+ EntityManager $entityManager,
User $user,
DataTransferManager $dataTransferManager
)
{
parent::__construct();
- $this->jobDao = $jobDao;
+ $this->jobRepository = $entityManager->getRepository(Job::class);
$this->user = $user;
$this->dataTransferManager = $dataTransferManager;
}
@@ -51,7 +51,7 @@ public function __construct(
*/
public function handleRemove($id)
{
- if (($entity = $this->jobDao->find($id)) === null) {
+ if (($entity = $this->jobRepository->find($id)) === null) {
throw new BadRequestException;
}
@@ -59,7 +59,7 @@ public function handleRemove($id)
throw new BadRequestException;
}
- $this->jobDao->delete($entity);
+ $this->jobRepository->delete($entity);
if (!$this->presenter->isAjax()) {
$this->redirect('this');
@@ -69,8 +69,8 @@ public function handleRemove($id)
public function render($id)
{
$this->template->job = $this->dataTransferManager
- ->createQuery(JobDto::getClassName(), function () use ($id) {
- return $this->jobDao->find($id);
+ ->createQuery(JobDto::class, function () use ($id) {
+ return $this->jobRepository->find($id);
})
->enableCache()
->fetch();
diff --git a/src/Queue/Components/JobsControl.php b/src/Queue/Components/JobsControl.php
index 210cad35..4c27a155 100755
--- a/src/Queue/Components/JobsControl.php
+++ b/src/Queue/Components/JobsControl.php
@@ -11,10 +11,11 @@
namespace Venne\Queue\Components;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\EntityManager;
use Nette\Security\User;
use Venne\DataTransfer\DataTransferManager;
use Venne\Queue\JobDto;
+use Venne\Queue\Job;
/**
* @author Josef Kříž
@@ -25,8 +26,8 @@ class JobsControl extends \Venne\System\UI\Control
/** @var \Nette\Security\User */
private $user;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $jobDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $jobRepository;
/** @var \Venne\Queue\Components\IJobControlFactory */
private $jobControlFactory;
@@ -35,7 +36,7 @@ class JobsControl extends \Venne\System\UI\Control
private $dataTransferManager;
public function __construct(
- EntityDao $jobDao,
+ EntityManager $entityManager,
User $user,
IJobControlFactory $jobControlFactory,
DataTransferManager $dataTransferManager
@@ -43,7 +44,7 @@ public function __construct(
{
parent::__construct();
- $this->jobDao = $jobDao;
+ $this->jobRepository = $entityManager->getRepository(Job::class);
$this->user = $user;
$this->jobControlFactory = $jobControlFactory;
$this->dataTransferManager = $dataTransferManager;
@@ -60,8 +61,8 @@ protected function createComponentJob()
public function render()
{
$this->template->jobs = $this->dataTransferManager
- ->createQuery(JobDto::getClassName(), function () {
- return $this->jobDao->createQueryBuilder('a')
+ ->createQuery(JobDto::class, function () {
+ return $this->jobRepository->createQueryBuilder('a')
->andWhere('a.user = :user')->setParameter('user', $this->user->getIdentity()->getId())
->orderBy('a.date', 'ASC')
->getQuery()->getResult();
diff --git a/src/Queue/DI/QueueExtension.php b/src/Queue/DI/QueueExtension.php
index ccd9a75a..f042d368 100755
--- a/src/Queue/DI/QueueExtension.php
+++ b/src/Queue/DI/QueueExtension.php
@@ -34,6 +34,10 @@ class QueueExtension extends \Nette\DI\CompilerExtension
public function loadConfiguration()
{
$container = $this->getContainerBuilder();
+ $this->compiler->parseServices(
+ $container,
+ $this->loadFromFile(__DIR__ . '/services.neon')
+ );
$config = $this->getConfig($this->config);
$container->addDefinition($this->prefix('configManager'))
@@ -43,7 +47,7 @@ public function loadConfiguration()
->setClass('Venne\Queue\WorkerManager', array($config['interval']));
$jobManager = $container->addDefinition($this->prefix('jobManager'))
- ->setClass('Venne\Queue\JobManager', array(new Statement('@doctrine.dao', array('Venne\Queue\JobEntity'))));
+ ->setClass('Venne\Queue\JobManager');
foreach ($this->compiler->getExtensions() as $extension) {
if ($extension instanceof IJobProvider) {
@@ -64,25 +68,20 @@ public function loadConfiguration()
));
$container->addDefinition($this->prefix('jobsPresenter'))
- ->setClass('Venne\Queue\AdminModule\JobsPresenter', array(new Statement('@doctrine.dao', array('Venne\Queue\JobEntity'))));
+ ->setClass('Venne\Queue\AdminModule\JobsPresenter');
$container->addDefinition($this->prefix('workerFactory'))
->setImplement('Venne\Queue\IWorkerFactory')
->setArguments(array(new PhpLiteral('$id'), new PhpLiteral('$interval'), $container->expand($config['configDir'])))
->setAutowired(true);
- $container->addDefinition($this->prefix('jobFormFactory'))
- ->setClass('Venne\Queue\AdminModule\JobFormFactory', array(new Statement('@system.admin.basicFormFactory')));
-
$container->addDefinition($this->prefix('jobsControlFactory'))
- ->setArguments(array(new Statement('@doctrine.dao', array('Venne\Queue\JobEntity'))))
->setImplement('Venne\Queue\Components\IJobsControlFactory')
->setInject(true)
->addTag(SystemExtension::TAG_TRAY_COMPONENT)
->addTag(WidgetsExtension::TAG_WIDGET, 'jobs');
$container->addDefinition($this->prefix('jobControlFactory'))
- ->setArguments(array(new Statement('@doctrine.dao', array('Venne\Queue\JobEntity'))))
->setImplement('Venne\Queue\Components\IJobControlFactory')
->setInject(true);
}
@@ -93,7 +92,7 @@ public function loadConfiguration()
public function getEntityMappings()
{
return array(
- 'Venne\Queue' => dirname(__DIR__) . '/*Entity.php',
+ 'Venne\Queue' => dirname(__DIR__) . '/*.php',
);
}
diff --git a/src/Queue/DI/services.neon b/src/Queue/DI/services.neon
new file mode 100644
index 00000000..222ce7e1
--- /dev/null
+++ b/src/Queue/DI/services.neon
@@ -0,0 +1,4 @@
+services:
+
+ - Venne\Queue\AdminModule\JobFormFactory (@system.admin.ajaxFormFactory)
+ - Venne\Queue\AdminModule\JobFormService
diff --git a/src/Queue/IJob.php b/src/Queue/IJob.php
index 59ea099c..b6dbe988 100755
--- a/src/Queue/IJob.php
+++ b/src/Queue/IJob.php
@@ -18,9 +18,9 @@ interface IJob
{
/**
- * @param \Venne\Queue\JobEntity
+ * @param \Venne\Queue\Job
* @param integer $priority
*/
- public function run(JobEntity $jobEntity, $priority);
+ public function run(Job $jobEntity, $priority);
}
diff --git a/src/Queue/Job.php b/src/Queue/Job.php
index 1e2d5c5b..4e7b147e 100755
--- a/src/Queue/Job.php
+++ b/src/Queue/Job.php
@@ -11,18 +11,127 @@
namespace Venne\Queue;
+use DateTime;
+use Doctrine\ORM\Mapping as ORM;
+use Kdyby\Doctrine\Entities\BaseEntity;
+
/**
* @author Josef Kříž
+ *
+ * @property string $type
+ * @property mixed[] $arguments
+ * @property string $state
+ * @property int $priority
+ * @property \DateTime $date
+ * @property \DateTime|null $dateInterval
+ * @property int|null $round
+ * @property \Venne\Security\User $user
+ *
+ * @ORM\Entity
+ * @ORM\Table(name="jobs")
*/
-abstract class Job implements \Venne\Queue\IJob
+class Job extends \Kdyby\Doctrine\Entities\BaseEntity
{
+ use \Venne\Doctrine\Entities\IdentifiedEntityTrait;
+
+ const STATE_SCHEDULED = 'scheduled';
+
+ const STATE_IN_PROGRESS = 'in_progress';
+
+ const STATE_FAILED = 'failed';
+
+ const PRIORITY_LOW = 0;
+
+ const PRIORITY_NORMAL = 1;
+
+ const PRIORITY_HIGH = 2;
+
+ /**
+ * @var string
+ *
+ * @ORM\Column(type="string")
+ */
+ protected $type;
+
+ /**
+ * @var string
+ *
+ * @ORM\Column(type="text")
+ */
+ protected $arguments;
+
+ /**
+ * @var string
+ *
+ * @ORM\Column(type="string")
+ */
+ protected $state;
+
+ /**
+ * @var string
+ *
+ * @ORM\Column(type="integer")
+ */
+ protected $priority;
+
+ /**
+ * @var \DateTime
+ *
+ * @ORM\Column(type="datetime")
+ */
+ protected $date;
+
+ /**
+ * @var \DateTime|null
+ *
+ * @ORM\Column(type="datetime", nullable=true)
+ */
+ protected $dateInterval;
+
+ /**
+ * @var int|null
+ *
+ * @ORM\Column(type="integer", nullable=true)
+ */
+ protected $round;
+
+ /**
+ * @var \Nette\Security\User
+ *
+ * @ORM\ManyToOne(targetEntity="\Venne\Security\User")
+ * @ORM\JoinColumn(onDelete="CASCADE")
+ */
+ protected $user;
+
+ /**
+ * @param string $type
+ * @param \DateTime $date
+ * @param mixed[] $arguments
+ */
+ public function __construct($type, \DateTime $date = null, array $arguments = array())
+ {
+ $this->date = $date ?: new \DateTime;
+ $this->setArguments($arguments);
+ $this->type = $type;
+ $this->state = self::STATE_SCHEDULED;
+ $this->priority = self::PRIORITY_NORMAL;
+ }
+
+ /**
+ * @param mixed[] $arguments
+ */
+ public function setArguments(array $arguments = array())
+ {
+ $this->arguments = serialize($arguments);
+ }
+
/**
- * @return string
+ * @return mixed[]
*/
- public static function getName()
+ public function getArguments()
{
- return get_called_class();
+ return $this->arguments ? unserialize($this->arguments) : null;
}
}
diff --git a/src/Queue/JobEntity.php b/src/Queue/JobEntity.php
deleted file mode 100755
index e70b68de..00000000
--- a/src/Queue/JobEntity.php
+++ /dev/null
@@ -1,137 +0,0 @@
-
- *
- * @property string $type
- * @property mixed[] $arguments
- * @property string $state
- * @property int $priority
- * @property \DateTime $date
- * @property \DateTime|null $dateInterval
- * @property int|null $round
- * @property \Venne\Security\UserEntity $user
- *
- * @ORM\Entity
- * @ORM\Table(name="jobs")
- */
-class JobEntity extends \Kdyby\Doctrine\Entities\BaseEntity
-{
-
- use \Venne\Doctrine\Entities\IdentifiedEntityTrait;
-
- const STATE_SCHEDULED = 'scheduled';
-
- const STATE_IN_PROGRESS = 'in_progress';
-
- const STATE_FAILED = 'failed';
-
- const PRIORITY_LOW = 0;
-
- const PRIORITY_NORMAL = 1;
-
- const PRIORITY_HIGH = 2;
-
- /**
- * @var string
- *
- * @ORM\Column(type="string")
- */
- protected $type;
-
- /**
- * @var string
- *
- * @ORM\Column(type="text")
- */
- protected $arguments;
-
- /**
- * @var string
- *
- * @ORM\Column(type="string")
- */
- protected $state;
-
- /**
- * @var string
- *
- * @ORM\Column(type="integer")
- */
- protected $priority;
-
- /**
- * @var \DateTime
- *
- * @ORM\Column(type="datetime")
- */
- protected $date;
-
- /**
- * @var \DateTime|null
- *
- * @ORM\Column(type="datetime", nullable=true)
- */
- protected $dateInterval;
-
- /**
- * @var int|null
- *
- * @ORM\Column(type="integer", nullable=true)
- */
- protected $round;
-
- /**
- * @var \Nette\Security\UserEntity
- *
- * @ORM\ManyToOne(targetEntity="\Venne\Security\UserEntity")
- * @ORM\JoinColumn(onDelete="CASCADE")
- */
- protected $user;
-
- /**
- * @param string $type
- * @param \DateTime $date
- * @param mixed[] $arguments
- */
- public function __construct($type, \DateTime $date = null, array $arguments = array())
- {
- $this->date = $date ?: new \DateTime;
- $this->setArguments($arguments);
- $this->type = $type;
- $this->state = self::STATE_SCHEDULED;
- $this->priority = self::PRIORITY_NORMAL;
- }
-
- /**
- * @param mixed[] $arguments
- */
- public function setArguments(array $arguments = array())
- {
- $this->arguments = serialize($arguments);
- }
-
- /**
- * @return mixed[]
- */
- public function getArguments()
- {
- return $this->arguments ? unserialize($this->arguments) : null;
- }
-
-}
diff --git a/src/Queue/JobManager.php b/src/Queue/JobManager.php
index 609aff49..d833bb41 100755
--- a/src/Queue/JobManager.php
+++ b/src/Queue/JobManager.php
@@ -11,11 +11,11 @@
namespace Venne\Queue;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\EntityManager;
use Nette\DI\Container;
use Nette\InvalidArgumentException;
-use Nette\Security\User;
-use Venne\Security\UserEntity;
+use Nette\Security\User as NetteUser;
+use Venne\Security\User;
/**
* @author Josef Kříž
@@ -27,8 +27,11 @@ class JobManager extends \Nette\Object
const PRIORITY_DEFAULT = 1;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $jobDao;
+ /** @var \Doctrine\ORM\EntityManager */
+ private $entityManager;
+
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $jobRepository;
/** @var \Venne\Queue\IJob[] */
private $jobs = array();
@@ -39,19 +42,20 @@ class JobManager extends \Nette\Object
/** @var \Nette\DI\Container */
private $container;
- /** @var \Nette\Security\User */
- private $user;
+ /** @var \Nette\Security\NetteUser */
+ private $netteUser;
public function __construct(
- EntityDao $jobDao,
+ EntityManager $entityManager,
ConfigManager $configManager,
- User $user,
+ NetteUser $netteUser,
Container $container
)
{
+ $this->entityManager = $entityManager;
+ $this->jobRepository = $entityManager->getRepository(Job::class);
$this->configManager = $configManager;
- $this->jobDao = $jobDao;
- $this->user = $user;
+ $this->netteUser = $netteUser;
$this->container = $container;
}
@@ -76,34 +80,36 @@ public function getJobs()
}
/**
- * @param \Venne\Queue\JobEntity $jobEntity
+ * @param \Venne\Queue\Job $jobEntity
*/
- public function scheduleJob(JobEntity $jobEntity, $priority = self::PRIORITY_DEFAULT)
+ public function scheduleJob(Job $jobEntity, $priority = self::PRIORITY_DEFAULT)
{
- if (!$jobEntity->user && $this->user->identity instanceof UserEntity) {
- $jobEntity->user = $this->user->identity;
+ if (!$jobEntity->user && $this->netteUser->identity instanceof User) {
+ $jobEntity->user = $this->netteUser->identity;
}
if ($priority === self::PRIORITY_REALTIME) {
$this->doJob($jobEntity, $priority);
} else {
- $this->jobDao->save($jobEntity);
+ $this->entityManager->persist($jobEntity);
+ $this->entityManager->flush($jobEntity);
}
}
- private function doJob(JobEntity $jobEntity, $priority)
+ private function doJob(Job $jobEntity, $priority)
{
$jobEntity->state = $jobEntity::STATE_IN_PROGRESS;
- $this->jobDao->save($jobEntity);
+ $this->entityManager->flush($jobEntity);
$job = $this->getJob($jobEntity->type);
try {
$job->run($jobEntity, $priority);
- $this->jobDao->delete($jobEntity);
+ $this->entityManager->remove($jobEntity);
+ $this->entityManager->flush();
} catch (\Exception $e) {
$jobEntity->state = $jobEntity::STATE_FAILED;
- $this->jobDao->save($jobEntity);
+ $this->entityManager->flush($jobEntity);
throw new JobFailedException(sprintf('Job \'%s\' failed.', $jobEntity->getId()), 0, $e);
}
@@ -117,11 +123,11 @@ private function doJob(JobEntity $jobEntity, $priority)
public function doNextWork(Worker $worker)
{
$this->configManager->lock();
- $jobEntity = $this->jobDao->createQueryBuilder('a')
+ $jobEntity = $this->jobRepository->createQueryBuilder('a')
->addOrderBy('a.priority', 'DESC')
->addOrderBy('a.date', 'ASC')
->andWhere('a.date <= :now')->setParameter('now', new \DateTime)
- ->andWhere('a.state = :state')->setParameter('state', JobEntity::STATE_SCHEDULED)
+ ->andWhere('a.state = :state')->setParameter('state', Job::STATE_SCHEDULED)
->setMaxResults(1)
->getQuery()->getOneOrNullResult();
@@ -131,7 +137,7 @@ public function doNextWork(Worker $worker)
if ($jobEntity) {
$jobEntity->state = $jobEntity::STATE_IN_PROGRESS;
- $this->jobDao->save($jobEntity);
+ $this->entityManager->flush($jobEntity);
$this->configManager->unlock();
try {
@@ -139,7 +145,7 @@ public function doNextWork(Worker $worker)
} catch (JobFailedException $e) {
$failed = true;
$jobEntity->state = $jobEntity::STATE_FAILED;
- $this->jobDao->save($jobEntity);
+ $this->entityManager->flush($jobEntity);
$worker->log('Error: ' . $e->getPrevious()->getMessage());
}
@@ -152,7 +158,7 @@ public function doNextWork(Worker $worker)
$jobEntity->round--;
$jobEntity->date = $jobEntity->date->add($diff);
$jobEntity->state = $jobEntity::STATE_SCHEDULED;
- $this->jobDao->save($jobEntity);
+ $this->entityManager->persist($jobEntity);
}
}
diff --git a/src/Queue/JobType.php b/src/Queue/JobType.php
new file mode 100644
index 00000000..24beb7e1
--- /dev/null
+++ b/src/Queue/JobType.php
@@ -0,0 +1,28 @@
+
+ */
+abstract class JobType implements \Venne\Queue\IJob
+{
+
+ /**
+ * @return string
+ */
+ public static function getName()
+ {
+ return get_called_class();
+ }
+
+}
diff --git a/src/Queue/WorkerManager.php b/src/Queue/WorkerManager.php
index a5051e08..7ea87a3d 100755
--- a/src/Queue/WorkerManager.php
+++ b/src/Queue/WorkerManager.php
@@ -122,7 +122,7 @@ public function getWokrer($id)
$data = $this->configManager->loadConfigFile();
if (!isset($data['worker'][$id])) {
- throw new InvalidArgumentException("Worker '$id' does not exist.");
+ throw new InvalidArgumentException(sprintf('Worker \'%d\' does not exist.', $id));
}
return $this->workerFactory->create($id, $this->interval);
diff --git a/src/Security/AdminModule/AccountPresenter.php b/src/Security/AdminModule/AccountPresenter.php
index 9a1f6fc5..a6649a15 100755
--- a/src/Security/AdminModule/AccountPresenter.php
+++ b/src/Security/AdminModule/AccountPresenter.php
@@ -11,23 +11,21 @@
namespace Venne\Security\AdminModule;
+use Doctrine\ORM\EntityManager;
use Grido\DataSources\ArraySource;
use Grido\DataSources\Doctrine;
-use Kdyby\Doctrine\EntityDao;
use Nette\Forms\Form;
use Nette\Http\Session;
use Nette\Utils\Html;
use Venne\Bridges\Kdyby\DoctrineForms\FormFactoryFactory;
use Venne\Security\DefaultType\RegistrationFormFactory;
-use Venne\Security\LoginEntity;
+use Venne\Security\Login;
use Venne\Security\SecurityManager;
-use Venne\Security\UserEntity;
+use Venne\Security\User;
use Venne\System\Components\AdminGrid\IAdminGridFactory;
/**
* @author Josef Kříž
- *
- * @secured
*/
class AccountPresenter extends \Nette\Application\UI\Presenter
{
@@ -41,11 +39,11 @@ class AccountPresenter extends \Nette\Application\UI\Presenter
*/
public $provider;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $userDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $userRepository;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $loginDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $loginRepository;
/** @var \Venne\Security\DefaultType\RegistrationFormFactory */
private $userFormFactory;
@@ -66,8 +64,7 @@ class AccountPresenter extends \Nette\Application\UI\Presenter
private $formFactoryFactory;
public function __construct(
- EntityDao $userDao,
- EntityDao $loginDao,
+ EntityManager $entityManager,
RegistrationFormFactory $userFormFactory,
ProviderFormFactory $providerFormFactory,
SecurityManager $securityManager,
@@ -76,8 +73,8 @@ public function __construct(
FormFactoryFactory $formFactoryFactory
)
{
- $this->userDao = $userDao;
- $this->loginDao = $loginDao;
+ $this->userRepository = $entityManager->getRepository(User::class);
+ $this->loginRepository = $entityManager->getRepository(Login::class);
$this->userFormFactory = $userFormFactory;
$this->providerFormFactory = $providerFormFactory;
$this->securityManager = $securityManager;
@@ -128,7 +125,7 @@ public function handleDisconnect($service)
}
}
- $this->userDao->save($user);
+ $this->getEntityManager()->flush($user);
$this->redirect('this', array('provider' => null));
}
@@ -153,7 +150,7 @@ protected function createComponentLoginTable()
);
}
- $admin = $this->adminGridFactory->create($this->loginDao);
+ $admin = $this->adminGridFactory->create($this->loginRepository);
// columns
$table = $admin->getTable();
@@ -162,7 +159,7 @@ protected function createComponentLoginTable()
$table->addColumnText('name', 'Name')
->getCellPrototype()->width = '100%';
- /** @var \Venne\Security\UserEntity $user */
+ /** @var \Venne\Security\User $user */
$user = $this->user->identity;
$securityManager = $this->securityManager;
$providerFormFactory = $this->providerFormFactory;
@@ -210,7 +207,7 @@ protected function createComponentLoginTable()
return $element;
})
->setConfirm(function ($entity) {
- return "Really disconnect from '{$entity['name']}'?";
+ return array('Really disconnect from \'%s\'?', $entity['name']);
});
$table->getAction('disconnect')->onClick[] = function ($button, $name) use ($_this) {
$_this->handleDisconnect(str_replace('_', ' ', $name));
@@ -226,18 +223,18 @@ protected function createComponentLoginTable()
protected function createComponentTable()
{
$session = $this->session;
- $admin = $this->adminGridFactory->create($this->loginDao);
+ $admin = $this->adminGridFactory->create($this->loginRepository);
// columns
$table = $admin->getTable();
- if ($this->user->identity instanceof UserEntity) {
- $table->setModel(new Doctrine($this->loginDao->createQueryBuilder('a')->andWhere('a.user = :user')->setParameter('user', $this->user->identity)));
+ if ($this->user->identity instanceof User) {
+ $table->setModel(new Doctrine($this->loginRepository->createQueryBuilder('a')->andWhere('a.user = :user')->setParameter('user', $this->user->identity)));
} else {
- $table->setModel(new Doctrine($this->loginDao->createQueryBuilder('a')->andWhere('a.user IS NULL')));
+ $table->setModel(new Doctrine($this->loginRepository->createQueryBuilder('a')->andWhere('a.user IS NULL')));
}
$table->setTranslator($this->translator);
$table->addColumnDate('current', 'Current')
- ->setCustomRender(function (LoginEntity $entity) use ($session) {
+ ->setCustomRender(function (Login $entity) use ($session) {
$el = Html::el('span');
$el->class[] = 'glyphicon ' . ($session->id == $entity->getSessionId() ? 'glyphicon-ok' : 'glyphicon-remove');
@@ -252,9 +249,6 @@ protected function createComponentTable()
// actions
$table->addActionEvent('delete', 'Delete')
- ->setConfirm(function ($entity) {
- return "Really delete session '{$entity->sessionId}'?";
- })
->getElementPrototype()->class[] = 'ajax';
$admin->connectActionAsDelete($table->getAction('delete'));
@@ -266,15 +260,14 @@ protected function createComponentTable()
*/
protected function createComponentAccountForm()
{
- $user = $this->userDao->find($this->getUser()->getIdentity()->getId());
+ $user = $this->userRepository->find($this->getUser()->getIdentity()->getId());
- $formFactory = $this->securityManager
+ $formService = $this->securityManager
->getUserTypeByClass($user->getClass())
- ->getFrontFormFactory();
+ ->getFrontFormService();
- $form = $this->formFactoryFactory
- ->create($formFactory)
- ->setEntity($user->getExtendedUser())
+ $form = $formService
+ ->createFormFactory($user->getExtendedUser())
->create();
$form->onSuccess[] = $this->accountFormSuccess;
diff --git a/src/Security/AdminModule/DefaultPresenter.php b/src/Security/AdminModule/DefaultPresenter.php
index 5e724b70..9d71a81a 100755
--- a/src/Security/AdminModule/DefaultPresenter.php
+++ b/src/Security/AdminModule/DefaultPresenter.php
@@ -11,17 +11,17 @@
namespace Venne\Security\AdminModule;
+use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
-use Kdyby\Doctrine\EntityDao;
use Venne\Security\DefaultType\AdminFormFactory;
+use Venne\Security\ExtendedUser;
use Venne\Security\SecurityManager;
+use Venne\Security\User;
use Venne\System\Components\AdminGrid\Form;
use Venne\System\Components\AdminGrid\IAdminGridFactory;
/**
* @author Josef Kříž
- *
- * @secured
*/
class DefaultPresenter extends \Nette\Application\UI\Presenter
{
@@ -42,8 +42,8 @@ class DefaultPresenter extends \Nette\Application\UI\Presenter
*/
public $type;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $userDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $userRepository;
/** @var \Venne\Security\DefaultType\AdminFormFactory */
private $form;
@@ -58,14 +58,14 @@ class DefaultPresenter extends \Nette\Application\UI\Presenter
private $adminGridFactory;
public function __construct(
- EntityDao $userDao,
+ EntityManager $entityManager,
AdminFormFactory $form,
ProvidersFormFactory $providersForm,
SecurityManager $securityManager,
IAdminGridFactory $adminGridFactory
)
{
- $this->userDao = $userDao;
+ $this->userRepository = $entityManager->getRepository(User::class);
$this->form = $form;
$this->providersForm = $providersForm;
$this->securityManager = $securityManager;
@@ -94,8 +94,8 @@ protected function startup()
*/
protected function createComponentTable()
{
- $dao = $this->entityManager->getDao($this->type);
- $admin = $this->adminGridFactory->create($dao);
+ $repository = $this->entityManager->getRepository($this->type);
+ $admin = $this->adminGridFactory->create($repository);
$table = $admin->getTable();
$table->setTranslator($this->translator);
$table->setPrimaryKey('user.id');
@@ -116,10 +116,10 @@ protected function createComponentTable()
$type = $this->type;
- $form = $admin->addForm('user', 'User', $this->getUserType()->getFormFactory(), function () use ($type) {
- return new $type;
+ $form = $admin->addForm('user', 'User', function (ExtendedUser $extendedUser = null) {
+ return $this->getUserType()->getFormService()->getFormFactory($extendedUser ? $extendedUser->getUser()->getId() : null);
}, Form::TYPE_LARGE);
- $form->onSuccess[] = function () {
+ $form->onSuccess[] = function (\Nette\Application\UI\Form $form) {
$this->flashMessage('User has been saved.', 'success');
$this->redrawControl('flashes');
};
diff --git a/src/Security/AdminModule/InvitationFormFactory.php b/src/Security/AdminModule/InvitationFormFactory.php
index e2e79c73..cf6deec3 100644
--- a/src/Security/AdminModule/InvitationFormFactory.php
+++ b/src/Security/AdminModule/InvitationFormFactory.php
@@ -42,8 +42,6 @@ public function create()
->addRule($form::FILLED)
->addRule($form::EMAIL);
- $form->addSubmit('_submit', 'Send');
-
return $form;
}
diff --git a/src/Security/AdminModule/InvitationFormService.php b/src/Security/AdminModule/InvitationFormService.php
new file mode 100644
index 00000000..95cc260c
--- /dev/null
+++ b/src/Security/AdminModule/InvitationFormService.php
@@ -0,0 +1,64 @@
+
+ */
+class InvitationFormService extends \Venne\System\DoctrineFormService
+{
+
+ /** @var \Nette\Security\User */
+ private $netteUser;
+
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $userRepository;
+
+ public function __construct(
+ InvitationFormFactory $formFactory,
+ EntityManager $entityManager,
+ EntityFormMapper $entityFormMapper,
+ User $netteUser
+ )
+ {
+ parent::__construct($formFactory, $entityManager, $entityFormMapper);
+ $this->netteUser = $netteUser;
+ $this->userRepository = $entityManager->getRepository(\Venne\Security\User::class);
+ }
+
+ /**
+ * @return string
+ */
+ protected function getEntityClassName()
+ {
+ return Invitation::class;
+ }
+
+ /**
+ * @return \Kdyby\Doctrine\Entities\BaseEntity
+ */
+ protected function createEntity()
+ {
+ $user = $this->userRepository->find($this->netteUser->getIdentity()->getId());
+
+ $class = new \ReflectionClass($this->getEntityClassName());
+
+ return $class->newInstanceArgs(array($user));
+ }
+
+}
diff --git a/src/Security/AdminModule/InvitationPresenter.php b/src/Security/AdminModule/InvitationPresenter.php
index a6d1960c..b91af722 100644
--- a/src/Security/AdminModule/InvitationPresenter.php
+++ b/src/Security/AdminModule/InvitationPresenter.php
@@ -13,8 +13,6 @@
/**
* @author Josef Kříž
- *
- * @secured
*/
class InvitationPresenter extends \Nette\Application\UI\Presenter
{
diff --git a/src/Security/AdminModule/InvitationsTableFactory.php b/src/Security/AdminModule/InvitationsTableFactory.php
index bfc82836..8a23aab4 100644
--- a/src/Security/AdminModule/InvitationsTableFactory.php
+++ b/src/Security/AdminModule/InvitationsTableFactory.php
@@ -11,12 +11,13 @@
namespace Venne\Security\AdminModule;
+use Doctrine\ORM\EntityManager;
use Grido\DataSources\Doctrine;
-use Kdyby\Doctrine\EntityDao;
use Nette\Localization\ITranslator;
-use Nette\Security\User;
+use Venne\Security\NetteUser;
+use Venne\Security\User;
use Venne\System\Components\AdminGrid\IAdminGridFactory;
-use Venne\System\InvitationEntity;
+use Venne\System\Invitation;
/**
* @author Josef Kříž
@@ -24,14 +25,11 @@
class InvitationsTableFactory
{
- /** @var \Kdyby\Doctrine\EntityDao */
- private $invitationDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $invitationRepository;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $userDao;
-
- /** @var \Venne\Security\AdminModule\InvitationFormFactory */
- private $formFactory;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $userRepository;
/** @var \Venne\System\Components\AdminGrid\IAdminGridFactory */
private $adminGridFactory;
@@ -39,24 +37,26 @@ class InvitationsTableFactory
/** @var \Nette\Localization\ITranslator */
private $translator;
- /** @var \Nette\Security\User */
- private $user;
+ /** @var \Nette\Security\NetteUser */
+ private $netteUser;
+
+ /** @var \Venne\Security\AdminModule\InvitationFormService */
+ private $invitationFormService;
public function __construct(
- EntityDao $invitationDao,
- EntityDao $userDao,
- InvitationFormFactory $formFactory,
+ EntityManager $entityManager,
IAdminGridFactory $adminGridFactory,
ITranslator $translator,
- User $user
+ NetteUser $user,
+ InvitationFormService $invitationFormService
)
{
- $this->invitationDao = $invitationDao;
- $this->userDao = $userDao;
- $this->formFactory = $formFactory;
+ $this->invitationRepository = $entityManager->getRepository(Invitation::class);
+ $this->userRepository = $entityManager->getRepository(User::class);
$this->adminGridFactory = $adminGridFactory;
$this->translator = $translator;
- $this->user = $user;
+ $this->netteUser = $user;
+ $this->invitationFormService = $invitationFormService;
}
/**
@@ -64,13 +64,15 @@ public function __construct(
*/
public function create()
{
- $admin = $this->adminGridFactory->create($this->invitationDao);
+ $admin = $this->adminGridFactory->create($this->invitationRepository);
+ $qb = $this
+ ->invitationRepository
+ ->createQueryBuilder('a')
+ ->andWhere('a.author = :author')->setParameter('author', $this->netteUser->identity->getId());
// columns
$table = $admin->getTable();
- $table->setModel(new Doctrine($this->invitationDao->createQueryBuilder('a')
- ->andWhere('a.author = :author')->setParameter('author', $this->user->identity->getId())
- ));
+ $table->setModel(new Doctrine($qb));
$table->setTranslator($this->translator);
$table->addColumnText('email', 'E-mail')
->setSortable()
@@ -79,7 +81,7 @@ public function create()
->setFilterText()->setSuggestion();
$table->addColumnText('type', 'Type')
- ->setCustomRender(function (InvitationEntity $invitationEntity) {
+ ->setCustomRender(function (Invitation $invitationEntity) {
return $invitationEntity->registration->getName();
})
->getCellPrototype()->width = '40%';
@@ -88,10 +90,8 @@ public function create()
$table->addActionEvent('edit', 'Edit')
->getElementPrototype()->class[] = 'ajax';
- $form = $admin->addForm('role', 'Role', $this->formFactory, function () {
- return new InvitationEntity(
- $this->userDao->find($this->user->getIdentity()->getId())
- );
+ $form = $admin->addForm('invitation', 'Invitation', function (Invitation $invitation = null) {
+ return $this->invitationFormService->getFormFactory($invitation !== null ? $invitation->getId() : null);
});
$admin->connectFormWithAction($form, $table->getAction('edit'));
diff --git a/src/Security/AdminModule/RequestPresenter.php b/src/Security/AdminModule/RequestPresenter.php
index b069086f..b30b0dbc 100644
--- a/src/Security/AdminModule/RequestPresenter.php
+++ b/src/Security/AdminModule/RequestPresenter.php
@@ -18,8 +18,6 @@
/**
* @author Josef Kříž
- *
- * @secured
*/
class RequestPresenter extends \Nette\Application\UI\Presenter
{
diff --git a/src/Security/AdminModule/RoleFormFactory.php b/src/Security/AdminModule/RoleFormFactory.php
index c015be19..44abfbbe 100755
--- a/src/Security/AdminModule/RoleFormFactory.php
+++ b/src/Security/AdminModule/RoleFormFactory.php
@@ -13,11 +13,12 @@
use Kdyby\DoctrineForms\IComponentMapper;
use Venne\Forms\IFormFactory;
+use Venne\Security\Role;
/**
* @author Josef Kříž
*/
-class RoleFormFactory implements \Venne\Forms\IFormFactory
+class RoleFormFactory extends \Nette\Object implements \Venne\Forms\IFormFactory
{
/** @var \Venne\Forms\IFormFactory */
@@ -29,7 +30,7 @@ public function __construct(IFormFactory $formFactory)
}
/**
- * @return \Nette\Forms\Form
+ * @return \Nette\Application\UI\Form
*/
public function create()
{
@@ -37,11 +38,11 @@ public function create()
$form->addText('name', 'Name');
$form->addSelect('parent', 'Parent')
- ->setOption(IComponentMapper::ITEMS_TITLE, 'name')
+ ->setOption(IComponentMapper::ITEMS_TITLE, function (Role $role) {
+ return $role->getName();
+ })
->setPrompt('root');
- $form->addSubmit('_submit', 'Save');
-
return $form;
}
diff --git a/src/Security/AdminModule/RoleFormService.php b/src/Security/AdminModule/RoleFormService.php
new file mode 100644
index 00000000..53d249ea
--- /dev/null
+++ b/src/Security/AdminModule/RoleFormService.php
@@ -0,0 +1,54 @@
+
+ */
+class RoleFormService extends \Venne\System\DoctrineFormService
+{
+
+ public function __construct(
+ RoleFormFactory $formFactory,
+ EntityManager $entityManager,
+ EntityFormMapper $entityFormMapper
+ )
+ {
+ parent::__construct($formFactory, $entityManager, $entityFormMapper);
+ }
+
+ /**
+ * @return string
+ */
+ protected function getEntityClassName()
+ {
+ return Role::class;
+ }
+
+ protected function error(Form $form, \Exception $e)
+ {
+ if ($e instanceof \Kdyby\Doctrine\DuplicateEntryException) {
+ $form['name']->addError($form->getTranslator()->translate('Name must be unique.'));
+
+ return;
+ }
+
+ parent::error($form, $e);
+ }
+
+}
diff --git a/src/Security/AdminModule/RolesPresenter.php b/src/Security/AdminModule/RolesPresenter.php
index 73e32931..ed4c7a2a 100755
--- a/src/Security/AdminModule/RolesPresenter.php
+++ b/src/Security/AdminModule/RolesPresenter.php
@@ -11,10 +11,11 @@
namespace Venne\Security\AdminModule;
+use Nette\Application\UI\Form;
+use Nette\Utils\ArrayHash;
+
/**
* @author Josef Kříž
- *
- * @secured
*/
class RolesPresenter extends \Nette\Application\UI\Presenter
{
@@ -33,12 +34,8 @@ protected function createComponentTable()
{
$admin = $this->rolesTableFactory->create();
$form = $admin->getForm('role');
- $form->onSuccess[] = function () {
- $this->flashMessage('Role has been saved.', 'success');
- $this->redrawControl('flashes');
- };
- $form->onError[] = function () {
- $this->flashMessage('Failed.', 'warning');
+ $form->onSuccess[] = function (Form $form, ArrayHash $values) {
+ $this->flashMessage($this->getTranslator()->translate('Role \'%name%\' has been saved.', null, (array) $values), 'success');
$this->redrawControl('flashes');
};
diff --git a/src/Security/AdminModule/RolesTableFactory.php b/src/Security/AdminModule/RolesTableFactory.php
index 8f3bb2cf..342b6751 100644
--- a/src/Security/AdminModule/RolesTableFactory.php
+++ b/src/Security/AdminModule/RolesTableFactory.php
@@ -11,9 +11,9 @@
namespace Venne\Security\AdminModule;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\EntityManager;
use Nette\Localization\ITranslator;
-use Venne\Security\RoleEntity;
+use Venne\Security\Role;
use Venne\System\Components\AdminGrid\IAdminGridFactory;
/**
@@ -22,11 +22,11 @@
class RolesTableFactory
{
- /** @var \Kdyby\Doctrine\EntityDao */
- private $dao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $roleRepository;
- /** @var \Venne\Security\AdminModule\RoleFormFactory */
- private $roleForm;
+ /** @var \Venne\Security\AdminModule\RoleFormService */
+ private $roleFormService;
/** @var \Venne\System\Components\AdminGrid\IAdminGridFactory */
private $adminGridFactory;
@@ -35,14 +35,14 @@ class RolesTableFactory
private $translator;
public function __construct(
- EntityDao $dao,
- RoleFormFactory $roleForm,
+ EntityManager $entityManager,
+ RoleFormService $roleFormService,
IAdminGridFactory $adminGridFactory,
ITranslator $translator
)
{
- $this->dao = $dao;
- $this->roleForm = $roleForm;
+ $this->roleRepository = $entityManager->getRepository(Role::class);
+ $this->roleFormService = $roleFormService;
$this->adminGridFactory = $adminGridFactory;
$this->translator = $translator;
}
@@ -52,7 +52,7 @@ public function __construct(
*/
public function create()
{
- $admin = $this->adminGridFactory->create($this->dao);
+ $admin = $this->adminGridFactory->create($this->roleRepository);
// columns
$table = $admin->getTable();
@@ -67,7 +67,7 @@ public function create()
->setSortable()
->getCellPrototype()->width = '60%';
$table->getColumn('parent')
- ->setCustomRender(function (RoleEntity $entity) {
+ ->setCustomRender(function (Role $entity) {
$entities = array();
$en = $entity;
while (($en = $en->getParent())) {
@@ -84,7 +84,9 @@ public function create()
$table->addActionEvent('permissions', 'Permissions')
->getElementPrototype()->class[] = 'ajax';
- $form = $admin->addForm('role', 'Role', $this->roleForm);
+ $form = $admin->addForm('role', 'Role', function (Role $role = null) {
+ return $this->roleFormService->getFormFactory($role ? $role->getId() : null);
+ });
$admin->connectFormWithAction($form, $table->getAction('edit'));
diff --git a/src/Security/AdminModule/templates/Account/default.latte b/src/Security/AdminModule/templates/Account/default.latte
index 223318df..c3ded22e 100755
--- a/src/Security/AdminModule/templates/Account/default.latte
+++ b/src/Security/AdminModule/templates/Account/default.latte
@@ -19,14 +19,14 @@
-{if $user instanceof \CmsModule\Pages\Users\UserEntity}
+{if $user instanceof \CmsModule\Pages\Users\User}
{_Authentication}
diff --git a/src/Security/Authenticator.php b/src/Security/Authenticator.php
index 69c8844d..1f409d3e 100755
--- a/src/Security/Authenticator.php
+++ b/src/Security/Authenticator.php
@@ -11,7 +11,7 @@
namespace Venne\Security;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\EntityManager;
use Nette\Security\AuthenticationException;
/**
@@ -20,12 +20,16 @@
class Authenticator extends \Nette\Object implements \Nette\Security\IAuthenticator
{
- /** @var \Kdyby\Doctrine\EntityDao */
- private $userDao;
+ /** @var \Doctrine\ORM\EntityManager */
+ private $entityManager;
- public function __construct(EntityDao $userDao)
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $userRepository;
+
+ public function __construct(EntityManager $entityManager)
{
- $this->userDao = $userDao;
+ $this->entityManager = $entityManager;
+ $this->userRepository = $entityManager->getRepository(User::class);
}
/**
@@ -34,11 +38,10 @@ public function __construct(EntityDao $userDao)
*/
public function authenticate(array $credentials)
{
-
list($username, $password) = $credentials;
- /** @var UserEntity $user */
- $user = $this->userDao->findOneBy(array('email' => $username, 'published' => 1));
+ /** @var User $user */
+ $user = $this->userRepository->findOneBy(array('email' => $username, 'published' => 1));
if (!$user) {
throw new AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);
@@ -50,11 +53,11 @@ public function authenticate(array $credentials)
if ($user->needsRehash()) {
$user->setPassword($password);
- $this->userDao->save($user);
+ $this->entityManager->flush($user);
}
- return new Identity($user->id, $user->getRoles(), array(
- 'email' => $user->email,
+ return new Identity($user->getId(), $user->getRoles(), array(
+ 'email' => $user->getEmail(),
));
}
diff --git a/src/Security/AuthorizatorFactory.php b/src/Security/AuthorizatorFactory.php
index 05d0399f..93617118 100755
--- a/src/Security/AuthorizatorFactory.php
+++ b/src/Security/AuthorizatorFactory.php
@@ -11,10 +11,9 @@
namespace Venne\Security;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\EntityManager;
use Nette;
use Nette\Http\Session;
-use Nette\Security\Permission;
use Venne\System\AdministrationManager;
/**
@@ -25,11 +24,11 @@ class AuthorizatorFactory extends \Nette\Object
const SESSION_SECTION = 'Venne.Security.Authorizator';
- /** @var \Kdyby\Doctrine\EntityDao */
- private $roleDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $roleRepository;
- /** @var \Kdyby\Doctrine\EntityDao */
- private $permissionDao;
+ /** @var \Kdyby\Doctrine\EntityRepository */
+ private $permissionRepository;
/** @var \Nette\Http\SessionSection */
private $session;
@@ -38,14 +37,13 @@ class AuthorizatorFactory extends \Nette\Object
private $administrationManager;
public function __construct(
- EntityDao $roleDao,
- EntityDao $permissionDao,
+ EntityManager $entityManager,
Session $session,
AdministrationManager $administrationManager
)
{
- $this->roleDao = $roleDao;
- $this->permissionDao = $permissionDao;
+ $this->roleRepository = $entityManager->getRepository(Role::class);
+ $this->permissionRepository = $entityManager->getRepository(Permission::class);
$this->session = $session->getSection(self::SESSION_SECTION);
$this->administrationManager = $administrationManager;
}
@@ -79,7 +77,7 @@ public function getPermissionsByUser(Nette\Security\User $user, $fromSession = f
* Get permission for roles.
*
* @param string $roles
- * @return \Nette\Security\Permission
+ * @return \Venne\Security\Authorizator
*/
public function getPermissionsByRoles(array $roles)
{
@@ -95,11 +93,11 @@ public function getPermissionsByRoles(array $roles)
/**
* Setup permission by role
*
- * @param \Nette\Security\Permission $permission
+ * @param \Venne\Security\Authorizator $permission
* @param string $role
- * @return \Nette\Security\Permission
+ * @return \Venne\Security\Authorizator
*/
- private function setPermissionsByRole(Permission $permission, $role)
+ private function setPermissionsByRole(Authorizator $permission, $role)
{
// add role
if (!$permission->hasRole($role)) {
@@ -107,7 +105,7 @@ private function setPermissionsByRole(Permission $permission, $role)
}
// add resources
- $resources = $this->permissionDao->createQueryBuilder('a')
+ $resources = $this->permissionRepository->createQueryBuilder('a')
->select('a.resource')
->andWhere('a.role = :role')->setParameter('role', $role)
->groupBy('a.resource')
@@ -121,7 +119,7 @@ private function setPermissionsByRole(Permission $permission, $role)
}
// set allow/deny
- $roleEntity = $this->roleDao->findOneByName($role);
+ $roleEntity = $this->roleRepository->findOneByName($role);
if ($roleEntity) {
if ($roleEntity->parent) {
$this->setPermissionsByRole($permission, $roleEntity->parent->name);
diff --git a/src/Security/ControlVerifierReaders/AnnotationReader.php b/src/Security/ControlVerifierReaders/AnnotationReader.php
index 681e1012..09cef374 100755
--- a/src/Security/ControlVerifierReaders/AnnotationReader.php
+++ b/src/Security/ControlVerifierReaders/AnnotationReader.php
@@ -21,7 +21,7 @@ class AnnotationReader extends \Nette\Object implements \Venne\Security\IControl
{
/** @var mixed[] */
- protected $_annotationSchema = array();
+ private $annotationSchema = array();
/**
* @param string $class
@@ -29,7 +29,7 @@ class AnnotationReader extends \Nette\Object implements \Venne\Security\IControl
*/
public function getSchema($class)
{
- if (!isset($this->_annotationSchema[$class])) {
+ if (!isset($this->annotationSchema[$class])) {
$schema = array();
$ref = ClassType::from($class);
@@ -52,10 +52,10 @@ public function getSchema($class)
}
}
- $this->_annotationSchema[$class] = $schema;
+ $this->annotationSchema[$class] = $schema;
}
- return $this->_annotationSchema[$class];
+ return $this->annotationSchema[$class];
}
/**
diff --git a/src/Security/ControlVerifiers/ControlVerifier.php b/src/Security/ControlVerifiers/ControlVerifier.php
index 0571ad05..6f0c0cd6 100755
--- a/src/Security/ControlVerifiers/ControlVerifier.php
+++ b/src/Security/ControlVerifiers/ControlVerifier.php
@@ -25,19 +25,19 @@ class ControlVerifier extends \Nette\Object implements \Venne\Security\IControlV
{
/** @var \Nette\Security\User */
- protected $user;
+ private $user;
/** @var \Venne\Security\IControlVerifierReader */
- protected $reader;
+ private $reader;
/** @var mixed[] */
- protected $_annotationSchema = array();
+ private $annotationSchema = array();
/** @var mixed[] */
- protected $_presenterAllowed = array();
+ private $presenterAllowed = array();
/** @var mixed[] */
- protected $_methodAllowed = array();
+ private $methodAllowed = array();
public function __construct(User $user, IControlVerifierReader $reader)
{
@@ -81,11 +81,11 @@ public function checkRequirements($element)
*/
protected function isPresenterAllowedCached(PresenterComponentReflection $element)
{
- if (!array_key_exists($element->name, $this->_presenterAllowed)) {
- $this->_presenterAllowed[$element->name] = $this->isPresenterAllowed($element);
+ if (!array_key_exists($element->name, $this->presenterAllowed)) {
+ $this->presenterAllowed[$element->name] = $this->isPresenterAllowed($element);
}
- return $this->_presenterAllowed[$element->name];
+ return $this->presenterAllowed[$element->name];
}
/**
@@ -94,11 +94,11 @@ protected function isPresenterAllowedCached(PresenterComponentReflection $elemen
*/
protected function isMethodAllowedCached(Method $element)
{
- if (!array_key_exists($element->name, $this->_methodAllowed)) {
- $this->_methodAllowed[$element->name] = $this->isMethodAllowed($element);
+ if (!array_key_exists($element->name, $this->methodAllowed)) {
+ $this->methodAllowed[$element->name] = $this->isMethodAllowed($element);
}
- return $this->_methodAllowed[$element->name];
+ return $this->methodAllowed[$element->name];
}
/**
@@ -125,12 +125,15 @@ protected function checkMethod(Method $element)
$users = $schema[$name]['users'];
if (!in_array($this->user->getId(), $users)) {
- $exception = "Access denied for your username: '{$this->user->getId()}'. Require: '" . implode(', ', $users) . "'";
+ $exception = sprintf(
+ 'Access denied for your username: \'%s\'. Require: \'%s\'',
+ $this->user->getId(),
+ implode(', ', $users)
+ );
} else {
return;
}
- } // roles
- else if (isset($schema[$name]['roles']) && count($schema[$name]['roles']) > 0) {
+ } elseif (isset($schema[$name]['roles']) && count($schema[$name]['roles']) > 0) {
$userRoles = $this->user->getRoles();
$roles = $schema[$name]['roles'];
@@ -139,10 +142,13 @@ protected function checkMethod(Method $element)
} else {
return;
}
- } // resource & privilege
- else if (isset($schema[$name]['resource']) && $schema[$name]['resource']) {
+ } elseif (isset($schema[$name]['resource']) && $schema[$name]['resource']) {
if (!$this->user->isAllowed($schema[$name]['resource'], $schema[$name]['privilege'])) {
- $exception = "Access denied for resource: {$schema[$name]['resource']}" . ($schema[$name]['privilege'] ? " and privilege: {$schema[$name]['privilege']}" : '');
+ $exception = sprintf(
+ 'Access denied for resource: \'%s\' and privilege: \'%s\'',
+ $schema[$name]['resource'],
+ $schema[$name]['privilege']
+ );
} else {
return;
}
diff --git a/src/Security/DI/SecurityExtension.php b/src/Security/DI/SecurityExtension.php
index e7142814..aa757b2d 100755
--- a/src/Security/DI/SecurityExtension.php
+++ b/src/Security/DI/SecurityExtension.php
@@ -11,6 +11,7 @@
namespace Venne\Security\DI;
+use Kdyby\Console\DI\ConsoleExtension;
use Kdyby\Events\DI\EventsExtension;
use Nette\DI\ContainerBuilder;
use Nette\DI\Statement;
@@ -27,22 +28,23 @@ class SecurityExtension extends \Nette\DI\CompilerExtension
implements
\Kdyby\Doctrine\DI\IEntityProvider,
\Venne\Notifications\DI\IEventProvider,
- \Venne\System\DI\IPresenterProvider
+ \Venne\System\DI\IPresenterProvider,
+ \Venne\Security\DI\UserTypeProvider
{
+ const TAG_LOGIN_PROVIDER = 'venne.loginProvider';
+
public function loadConfiguration()
{
$container = $this->getContainerBuilder();
+ $this->compiler->parseServices(
+ $container,
+ $this->loadFromFile(__DIR__ . '/services.neon')
+ );
$container->addDefinition($this->prefix('listeners.userStateListener'))
->setClass('Venne\Security\Listeners\UserStateListener');
- $container->addDefinition($this->prefix('resetFormFactory'))
- ->setClass('Venne\Security\Login\ResetFormFactory', array(new Statement('@system.admin.basicFormFactory')));
-
- $container->addDefinition($this->prefix('confirmFormFactory'))
- ->setClass('Venne\Security\Login\ConfirmFormFactory', array(new Statement('@system.admin.basicFormFactory')));
-
$container->addDefinition($this->prefix('extendedUserListener'))
->setClass('Venne\Security\Listeners\ExtendedUserListener');
@@ -50,23 +52,40 @@ public function loadConfiguration()
->setClass('Venne\Security\Listeners\UserLogListener')
->addTag(EventsExtension::TAG_SUBSCRIBER);
- $this->setupDefaultType($container);
+ $container->addDefinition($this->prefix('securityManager'))
+ ->setClass('Venne\Security\SecurityManager');
+
+ $container->addDefinition($this->prefix('authorizatorFactory'))
+ ->setFactory('Venne\Security\AuthorizatorFactory');
+
+ $container->getDefinition('packageManager.packageManager')
+ ->addSetup('$service->onInstall[] = ?->clearPermissionSession', array($this->prefix('@authorizatorFactory')))
+ ->addSetup('$service->onUninstall[] = ?->clearPermissionSession', array($this->prefix('@authorizatorFactory')));
+
+ $container->addDefinition('authorizator')
+ ->setClass('Nette\Security\Permission')
+ ->setFactory($this->prefix('@authorizatorFactory') . '::getPermissionsByUser', array('@user', true));
+
+ $container->addDefinition('authenticator')
+ ->setClass('Venne\Security\Authenticator');
+
+ $container->addDefinition($this->prefix('installCommand'))
+ ->setFactory('Venne\System\Commands\InstallCommand')
+ ->addTag(ConsoleExtension::COMMAND_TAG);
+
$this->setupSecurity($container);
+ $this->registerUsers();
}
- public function setupSecurity(ContainerBuilder $container)
+ public function beforeCompile()
{
- $container->addDefinition($this->prefix('roleFormFactory'))
- ->setClass('Venne\Security\AdminModule\RoleFormFactory', array(new Statement('@system.admin.ajaxFormFactory')));
-
- $container->addDefinition($this->prefix('providerFormFactory'))
- ->setClass('Venne\Security\AdminModule\ProviderFormFactory', array(new Statement('@system.admin.basicFormFactory')));
-
- $container->addDefinition($this->prefix('providersFormFactory'))
- ->setClass('Venne\Security\AdminModule\ProvidersFormFactory', array(new Statement('@system.admin.ajaxFormFactory')));
+ $this->registerLoginProvider();
+ }
+ public function setupSecurity(ContainerBuilder $container)
+ {
$container->addDefinition($this->prefix('defaultPresenter'))
- ->setClass('Venne\Security\AdminModule\DefaultPresenter', array(new Statement('@doctrine.dao', array('Venne\Security\UserEntity'))))
+ ->setClass('Venne\Security\AdminModule\DefaultPresenter')
->addTag(SystemExtension::TAG_ADMINISTRATION, array(
'link' => 'Admin:Security:Default:',
'category' => 'System',
@@ -75,46 +94,38 @@ public function setupSecurity(ContainerBuilder $container)
'priority' => 60,
));
- $container->addDefinition($this->prefix('rolesTableFactory'))
- ->setClass('Venne\Security\AdminModule\RolesTableFactory', array(
- new Statement('@doctrine.dao', array('Venne\Security\RoleEntity'))
- ));
-
- $container->addDefinition($this->prefix('invitationsTableFactory'))
- ->setClass('Venne\Security\AdminModule\InvitationsTableFactory', array(
- new Statement('@doctrine.dao', array('Venne\System\InvitationEntity')),
- new Statement('@doctrine.dao', array('Venne\Security\UserEntity'))
- ));
-
- $container->addDefinition($this->prefix('rolesPresenter'))
- ->setClass('Venne\Security\AdminModule\RolesPresenter');
-
$container->addDefinition($this->prefix('accountPresenter'))
- ->setClass('Venne\Security\AdminModule\AccountPresenter', array(
- new Statement('@doctrine.dao', array('Venne\Security\UserEntity')),
- new Statement('@doctrine.dao', array('Venne\Security\LoginEntity'))
- ));
+ ->setClass('Venne\Security\AdminModule\AccountPresenter');
}
- public function setupDefaultType(ContainerBuilder $container)
+ private function registerUsers()
{
- $container->addDefinition($this->prefix('userType'))
- ->setClass('Venne\Security\UserType', array('Venne\Security\DefaultType\UserEntity'))
- ->addSetup('setFormFactory', array(new Statement('@Venne\Security\DefaultType\AdminFormFactory')))
- ->addSetup('setFrontFormFactory', array(new Statement('@Venne\Security\DefaultType\FrontFormFactory')))
- ->addSetup('setRegistrationFormFactory', array(new Statement('@Venne\Security\DefaultType\RegistrationFormFactory')))
- ->addTag(SystemExtension::TAG_USER, array(
- 'name' => 'Default user',
- ));
+ $container = $this->getContainerBuilder();
+ $config = $container->getDefinition($this->prefix('securityManager'));
+
+ foreach ($this->compiler->extensions as $extension) {
+ if ($extension instanceof UserTypeProvider) {
+ foreach ($extension->getUserTypes() as $type) {
+ $config->addSetup(
+ '$service->addUserType(new Venne\Security\UserType(?, ?, ?, ?, ?));',
+ $type->getArguments()
+ );
+ }
+ }
+ }
+ }
- $container->addDefinition($this->prefix('adminFormFactory'))
- ->setClass('Venne\Security\DefaultType\AdminFormFactory', array(new Statement('@system.admin.ajaxFormFactory')));
+ private function registerLoginProvider()
+ {
+ $container = $this->getContainerBuilder();
+ $config = $container->getDefinition($this->prefix('securityManager'));
- $container->addDefinition($this->prefix('frontFormFactory'))
- ->setClass('Venne\Security\DefaultType\FrontFormFactory', array(new Statement('@system.admin.ajaxFormFactory')));
+ foreach ($container->findByTag(static::TAG_LOGIN_PROVIDER) as $item => $tags) {
+ $class = '\\' . $container->getDefinition($item)->class;
+ $type = $class::getType();
- $container->addDefinition($this->prefix('registrationFormFactory'))
- ->setClass('Venne\Security\DefaultType\RegistrationFormFactory', array(new Statement('@system.admin.ajaxFormFactory')));
+ $config->addSetup('addLoginProvider', array($type, (string) $item));
+ }
}
/**
@@ -123,7 +134,7 @@ public function setupDefaultType(ContainerBuilder $container)
public function getEntityMappings()
{
return array(
- 'Venne\Security' => dirname(__DIR__) . '/*Entity.php',
+ 'Venne\Security' => dirname(__DIR__) . '/*.php',
);
}
@@ -150,4 +161,20 @@ public function getEventTypes()
);
}
+ /**
+ * @return \Venne\Security\UserType[]
+ */
+ public function getUserTypes()
+ {
+ return array(
+ new UserType(
+ 'Default user',
+ \Venne\Security\DefaultType\User::class,
+ new Statement('@' . \Venne\Security\DefaultType\AdminFormService::getReflection()->getName()),
+ new Statement('@' . \Venne\Security\DefaultType\FrontFormService::getReflection()->getName()),
+ new Statement('@' . \Venne\Security\DefaultType\RegistrationFormService::getReflection()->getName())
+ ),
+ );
+ }
+
}
diff --git a/src/Security/DI/UserType.php b/src/Security/DI/UserType.php
new file mode 100644
index 00000000..269892e8
--- /dev/null
+++ b/src/Security/DI/UserType.php
@@ -0,0 +1,67 @@
+
+ */
+class UserType extends \Nette\Object
+{
+
+ /** @var string */
+ private $name;
+
+ /** @var string */
+ private $entityName;
+
+ /** @var \Nette\DI\Statement */
+ private $formFactory;
+
+ /** @var \Nette\DI\Statement */
+ private $frontFormFactory;
+
+ /** @var \Nette\DI\Statement */
+ private $registrationFormFactory;
+
+ /**
+ * @param string $name
+ * @param string $entityName
+ * @param \Nette\DI\Statement $formFactory
+ * @param \Nette\DI\Statement $frontFormFactory
+ * @param \Nette\DI\Statement $registrationFormFactory
+ */
+ public function __construct($name, $entityName, Statement $formFactory, Statement $frontFormFactory, Statement $registrationFormFactory)
+ {
+ $this->name = $name;
+ $this->entityName = $entityName;
+ $this->formFactory = $formFactory;
+ $this->frontFormFactory = $frontFormFactory;
+ $this->registrationFormFactory = $registrationFormFactory;
+ }
+
+ /**
+ * @return mixed[]
+ */
+ public function getArguments()
+ {
+ return array(
+ $this->name,
+ $this->entityName,
+ $this->formFactory,
+ $this->frontFormFactory,
+ $this->registrationFormFactory,
+ );
+ }
+
+}
diff --git a/src/Security/DI/UserTypeProvider.php b/src/Security/DI/UserTypeProvider.php
new file mode 100644
index 00000000..1fd3fe04
--- /dev/null
+++ b/src/Security/DI/UserTypeProvider.php
@@ -0,0 +1,25 @@
+
+ */
+interface UserTypeProvider
+{
+
+ /**
+ * @return \Venne\Security\DI\UserType[]
+ */
+ public function getUserTypes();
+
+}
diff --git a/src/Security/DI/services.neon b/src/Security/DI/services.neon
new file mode 100644
index 00000000..119bb697
--- /dev/null
+++ b/src/Security/DI/services.neon
@@ -0,0 +1,22 @@
+services:
+
+ - Venne\Security\Login\ResetFormFactory (@system.admin.ajaxFormFactory)
+ - Venne\Security\Login\ConfirmFormFactory (@system.admin.ajaxFormFactory)
+
+ - Venne\Security\AdminModule\ProviderFormFactory (@system.admin.ajaxFormFactory)
+ - Venne\Security\AdminModule\ProvidersFormFactory (@system.admin.ajaxFormFactory)
+
+ - Venne\Security\AdminModule\InvitationFormFactory (@system.admin.ajaxFormFactory)
+ - Venne\Security\AdminModule\InvitationFormService
+ - Venne\Security\AdminModule\InvitationsTableFactory
+
+ - Venne\Security\AdminModule\RoleFormFactory (@system.admin.ajaxFormFactory)
+ - Venne\Security\AdminModule\RoleFormService
+ - Venne\Security\AdminModule\RolesTableFactory
+
+ - Venne\Security\DefaultType\AdminFormFactory (@system.admin.ajaxFormFactory)
+ - Venne\Security\DefaultType\AdminFormService
+ - Venne\Security\DefaultType\FrontFormFactory (@system.admin.ajaxFormFactory)
+ - Venne\Security\DefaultType\FrontFormService
+ - Venne\Security\DefaultType\RegistrationFormFactory (@system.admin.ajaxFormFactory)
+ - Venne\Security\DefaultType\RegistrationFormService
diff --git a/src/Security/DefaultType/AdminFormFactory.php b/src/Security/DefaultType/AdminFormFactory.php
index 5ff70bd1..d62bc690 100755
--- a/src/Security/DefaultType/AdminFormFactory.php
+++ b/src/Security/DefaultType/AdminFormFactory.php
@@ -51,42 +51,18 @@ public function create()
->setOption('description', 'If is set user cannot log in.');
$user->setCurrentGroup($form->addGroup('Password'));
- $user->addCheckbox('password_new', 'Change password')->addCondition($form::EQUAL, true)->toggle('setPasswd');
+ $passwordNew = $user->addCheckbox('password_new', 'Change password');
+ $passwordNew->addCondition($form::EQUAL, true)->toggle('setPasswd');
$user->setCurrentGroup($form->addGroup()->setOption('container', 'fieldset id=setPasswd'));
- $this->addPassword($form);
-
- $form->setCurrentGroup();
- $form->addSubmit('_submit', 'Save');
-
- $form->onSuccess[] = $this->success;
- $form->onError[] = $this->error;
-
- return $form;
- }
-
- public function success(Form $form)
- {
- if (!$form['user']['password_new']->value) {
- unset($form['user']['password']);
- unset($form['user']['password_confirm']);
- }
- }
-
- public function error(Form $form)
- {
- $this->addPassword($form);
- }
-
- private function addPassword(Form $form)
- {
- $user = $form->getComponent('user');
$user->addPassword('password', 'Password')
- ->addConditionOn($user['password_new'], Form::FILLED)
+ ->addConditionOn($passwordNew, Form::FILLED)
->addRule(Form::FILLED, 'Enter password')
->addRule(Form::MIN_LENGTH, 'Password is short', 5);
$user->addPassword('password_confirm', 'Confirm password')
- ->addConditionOn($user['password_new'], Form::FILLED)
+ ->addConditionOn($passwordNew, Form::FILLED)
->addRule(Form::EQUAL, 'Invalid re password', $user['password']);
+
+ return $form;
}
}
diff --git a/src/Security/DefaultType/AdminFormService.php b/src/Security/DefaultType/AdminFormService.php
new file mode 100644
index 00000000..8f25a432
--- /dev/null
+++ b/src/Security/DefaultType/AdminFormService.php
@@ -0,0 +1,72 @@
+
+ */
+class AdminFormService extends \Venne\System\DoctrineFormService
+{
+
+ public function __construct(
+ AdminFormFactory $formFactory,
+ EntityManager $entityManager,
+ EntityFormMapper $entityFormMapper
+ )
+ {
+ parent::__construct($formFactory, $entityManager, $entityFormMapper);
+ }
+
+ /**
+ * @return string
+ */
+ protected function getEntityClassName()
+ {
+ return User::class;
+ }
+
+ protected function save(Form $form, $entity)
+ {
+ $this->getEntityManager()->beginTransaction();
+ try {
+ $this->getEntityFormMapper()->save($entity, $form);
+
+ $this->getEntityManager()->persist($entity->getUser());
+ $this->getEntityManager()->flush($entity->getUser());
+ $this->getEntityManager()->persist($entity);
+ $this->getEntityManager()->flush($entity);
+
+ $this->getEntityManager()->commit();
+ } catch (\Exception $e) {
+ $this->getEntityManager()->rollback();
+
+ $this->error($form, $e);
+ }
+ }
+
+ protected function error(Form $form, \Exception $e)
+ {
+ if ($e instanceof \Kdyby\Doctrine\DuplicateEntryException) {
+ $form['name']->addError($form->getTranslator()->translate('Name must be unique.'));
+
+ return;
+ }
+
+ parent::error($form, $e);
+ }
+
+}
diff --git a/src/Security/DefaultType/FrontFormFactory.php b/src/Security/DefaultType/FrontFormFactory.php
index 2774ee5f..4757780a 100755
--- a/src/Security/DefaultType/FrontFormFactory.php
+++ b/src/Security/DefaultType/FrontFormFactory.php
@@ -17,7 +17,7 @@
/**
* @author Josef Kříž
*/
-class FrontFormFactory implements \Venne\Forms\IFormFactory
+class FrontFormFactory extends \Nette\Object implements \Venne\Forms\IFormFactory
{
/** @var \Venne\Forms\IFormFactory */
@@ -57,31 +57,7 @@ public function create()
->addConditionOn($user['password_new'], $form::FILLED)
->addRule($form::EQUAL, 'Invalid re password', $user['password']);
- $form->setCurrentGroup();
- $form->addSubmit('_submit', 'Save');
-
return $form;
}
- public function handleAttached(Form $form)
- {
- $form->setCurrentGroup();
- $form->addSaveButton('Save');
-
- if ($form->isSubmitted()) {
- if (!$form['user']['password_new']->value) {
- unset($form['user']['password']);
- }
- }
- }
-
- public function handleSave(Form $form)
- {
- if ($form['user']['password_new']->value) {
- $form->data->user->setPassword($form['user']['password']->value);
- }
-
- parent::handleSave($form);
- }
-
}
diff --git a/src/Security/DefaultType/FrontFormService.php b/src/Security/DefaultType/FrontFormService.php
new file mode 100644
index 00000000..546b5ca7
--- /dev/null
+++ b/src/Security/DefaultType/FrontFormService.php
@@ -0,0 +1,71 @@
+
+ */
+class FrontFormService extends \Venne\System\DoctrineFormService
+{
+
+ public function __construct(
+ FrontFormFactory $formFactory,
+ EntityManager $entityManager,
+ EntityFormMapper $entityFormMapper
+ )
+ {
+ parent::__construct($formFactory, $entityManager, $entityFormMapper);
+ }
+
+ /**
+ * @return string
+ */
+ protected function getEntityClassName()
+ {
+ return User::class;
+ }
+
+ protected function save(Form $form, $entity)
+ {
+ $this->getEntityManager()->beginTransaction();
+ try {
+ $this->getEntityFormMapper()->save($entity, $form);
+
+ $this->getEntityManager()->persist($entity);
+ $this->getEntityManager()->flush($entity->getUser());
+ $this->getEntityManager()->flush($entity);
+
+ $this->getEntityManager()->commit();
+ } catch (\Exception $e) {
+ $this->getEntityManager()->rollback();
+
+ $this->error($form, $e);
+ }
+ }
+
+ protected function error(Form $form, \Exception $e)
+ {
+ if ($e instanceof \Kdyby\Doctrine\DuplicateEntryException) {
+ $form['name']->addError($form->getTranslator()->translate('Name must be unique.'));
+
+ return;
+ }
+
+ parent::error($form, $e);
+ }
+
+}
diff --git a/src/Security/DefaultType/RegistrationFormFactory.php b/src/Security/DefaultType/RegistrationFormFactory.php
index 24df4f9c..2b659619 100755
--- a/src/Security/DefaultType/RegistrationFormFactory.php
+++ b/src/Security/DefaultType/RegistrationFormFactory.php
@@ -20,7 +20,7 @@
/**
* @author Josef Kříž
*/
-class RegistrationFormFactory implements IRegistrationForm, IFormFactory
+class RegistrationFormFactory extends \Nette\Object implements IRegistrationForm, IFormFactory
{
/** @var \Venne\Forms\IFormFactory */
@@ -49,18 +49,9 @@ public function create()
$user->addPassword('password_confirm', 'Confirm password')
->addRule(Form::EQUAL, 'Invalid re password', $user['password']);
- $form->setCurrentGroup();
- $form->addSubmit('_submit', 'Register');
-
return $form;
}
- public function handleAttached(Form $form)
- {
- $form->setCurrentGroup();
- $form->addSaveButton('Sign up');
- }
-
/**
* @param Form $form
* @param ILoginProvider $loginProvider
diff --git a/src/Security/DefaultType/RegistrationFormService.php b/src/Security/DefaultType/RegistrationFormService.php
new file mode 100644
index 00000000..89f1c61e
--- /dev/null
+++ b/src/Security/DefaultType/RegistrationFormService.php
@@ -0,0 +1,71 @@
+
+ */
+class RegistrationFormService extends \Venne\System\DoctrineFormService
+{
+
+ public function __construct(
+ RegistrationFormFactory $formFactory,
+ EntityManager $entityManager,
+ EntityFormMapper $entityFormMapper
+ )
+ {
+ parent::__construct($formFactory, $entityManager, $entityFormMapper);
+ }
+
+ /**
+ * @return string
+ */
+ protected function getEntityClassName()
+ {
+ return User::class;
+ }
+
+ protected function save(Form $form, $entity)
+ {
+ $this->getEntityManager()->beginTransaction();
+ try {
+ $this->getEntityFormMapper()->save($entity, $form);
+
+ $this->getEntityManager()->persist($entity);
+ $this->getEntityManager()->flush($entity->getUser());
+ $this->getEntityManager()->flush($entity);
+
+ $this->getEntityManager()->commit();
+ } catch (\Exception $e) {
+ $this->getEntityManager()->rollback();
+
+ $this->error($form, $e);
+ }
+ }
+
+ protected function error(Form $form, \Exception $e)
+ {
+ if ($e instanceof \Kdyby\Doctrine\DuplicateEntryException) {
+ $form['name']->addError($form->getTranslator()->translate('Name must be unique.'));
+
+ return;
+ }
+
+ parent::error($form, $e);
+ }
+
+}
diff --git a/src/Security/DefaultType/UserEntity.php b/src/Security/DefaultType/User.php
old mode 100755
new mode 100644
similarity index 78%
rename from src/Security/DefaultType/UserEntity.php
rename to src/Security/DefaultType/User.php
index f3880daf..5907f1f9
--- a/src/Security/DefaultType/UserEntity.php
+++ b/src/Security/DefaultType/User.php
@@ -16,10 +16,10 @@
/**
* @author Josef Kříž
*
- * @ORM\Entity(repositoryClass="\Venne\Doctrine\DerivedEntityDao")
+ * @ORM\Entity
* @ORM\Table(name="default_user")
*/
-class UserEntity extends \Venne\Security\ExtendedUserEntity
+class User extends \Venne\Security\ExtendedUser
{
}
diff --git a/src/Security/ExtendedUserEntity.php b/src/Security/ExtendedUser.php
old mode 100755
new mode 100644
similarity index 77%
rename from src/Security/ExtendedUserEntity.php
rename to src/Security/ExtendedUser.php
index b231d08c..51d5e4f4
--- a/src/Security/ExtendedUserEntity.php
+++ b/src/Security/ExtendedUser.php
@@ -19,14 +19,14 @@
*
* @ORM\MappedSuperclass
*/
-abstract class ExtendedUserEntity extends \Kdyby\Doctrine\Entities\BaseEntity
+abstract class ExtendedUser extends \Kdyby\Doctrine\Entities\BaseEntity
{
/**
- * @var \Venne\Security\UserEntity
+ * @var \Venne\Security\User
*
* @ORM\Id
- * @ORM\OneToOne(targetEntity="\Venne\Security\UserEntity", cascade={"all"})
+ * @ORM\OneToOne(targetEntity="\Venne\Security\User", cascade={"all"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $user;
@@ -51,7 +51,7 @@ public function __toString()
}
/**
- * @return \Venne\Security\UserEntity
+ * @return \Venne\Security\User
*/
public function getUser()
{
@@ -59,11 +59,11 @@ public function getUser()
}
/**
- * @return \Venne\Security\UserEntity
+ * @return \Venne\Security\User
*/
private function createUserEntity()
{
- return new UserEntity;
+ return new User();
}
}
diff --git a/src/Security/ILoginProvider.php b/src/Security/ILoginProvider.php
index 00a6bea2..9c185ea6 100755
--- a/src/Security/ILoginProvider.php
+++ b/src/Security/ILoginProvider.php
@@ -23,14 +23,14 @@ interface ILoginProvider extends \Nette\Security\IAuthenticator
public static function getType();
/**
- * @return \Venne\Security\LoginProviderEntity
+ * @return \Venne\Security\LoginProvider
*/
public function getLoginProviderEntity();
/**
- * @param \Venne\Security\UserEntity $userEntity
+ * @param \Venne\Security\User $user
*/
- public function connectWithUser(UserEntity $userEntity);
+ public function connectWithUser(User $user);
/**
* @param mixed[] $parameters
diff --git a/src/Security/Listeners/ExtendedUserListener.php b/src/Security/Listeners/ExtendedUserListener.php
index d0423211..8bd78edc 100755
--- a/src/Security/Listeners/ExtendedUserListener.php
+++ b/src/Security/Listeners/ExtendedUserListener.php
@@ -13,7 +13,7 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
-use Venne\Security\UserEntity;
+use Venne\Security\User;
/**
* @author Josef Kříž
@@ -24,10 +24,10 @@ class ExtendedUserListener
/**
* @ORM\PostLoad
*
- * @param \Venne\Security\UserEntity $user
+ * @param \Venne\Security\User $user
* @param \Doctrine\ORM\Event\LifecycleEventArgs $event
*/
- public function postLoadHandler(UserEntity $user, LifecycleEventArgs $event)
+ public function postLoadHandler(User $user, LifecycleEventArgs $event)
{
$em = $event->getEntityManager();
$user->setExtendedUserCallback(function () use ($em, $user) {
diff --git a/src/Security/Listeners/UserLogListener.php b/src/Security/Listeners/UserLogListener.php
index ba678be6..540acd29 100755
--- a/src/Security/Listeners/UserLogListener.php
+++ b/src/Security/Listeners/UserLogListener.php
@@ -11,10 +11,14 @@
namespace Venne\Security\Listeners;
-use Kdyby\Doctrine\EntityDao;
+use Doctrine\ORM\Event\OnFlushEventArgs;
+use Doctrine\ORM\Event\PreFlushEventArgs;
+use Doctrine\ORM\Event\PreUpdateEventArgs;
+use Doctrine\ORM\Events;
use Nette\DI\Container;
-use Nette\Security\User;
+use Nette\Security\User as NetteUser;
use Venne\Security\Events\LoginEvent;
+use Venne\Security\User;
/**
* @author Josef Kříž
@@ -44,21 +48,21 @@ public function getSubscribedEvents()
);
}
- public function onLoggedIn(User $user)
+ public function onLoggedIn(NetteUser $netteUser)
{
$this->getNotificationManager()->notify(
LoginEvent::getName(),
- $this->getUserDao()->find($user->getIdentity()->getId()),
+ $this->getUserRepository()->find($netteUser->getIdentity()->getId()),
'login',
'User has been logged in'
);
}
- public function onLoggedOut(User $user)
+ public function onLoggedOut(NetteUser $netteUser)
{
$this->getNotificationManager()->notify(
LoginEvent::getName(),
- $this->getUserDao()->find($user->getIdentity()->getId()),
+ $this->getUserRepository()->find($netteUser->getIdentity()->getId()),
'logout',
'User has been logged out'
);
@@ -69,16 +73,17 @@ public function onLoggedOut(User $user)
*/
private function getNotificationManager()
{
- return $this->container->getByType('Venne\Notifications\NotificationManager');
+ return $this->container->getByType(\Venne\Notifications\NotificationManager::class);
}
/**
- * @return \Kdyby\Doctrine\EntityDao
+ * @return \Kdyby\Doctrine\EntityRepository
*/
- private function getUserDao()
+ private function getUserRepository()
{
- $entityManager = $this->container->getByType('Doctrine\ORM\EntityManager');
- return $entityManager->getRepository('Venne\Security\UserEntity');
+ $entityManager = $this->container->getByType(\Doctrine\ORM\EntityManager::class);
+
+ return $entityManager->getRepository(User::class);
}
}
diff --git a/src/Security/Listeners/UserStateListener.php b/src/Security/Listeners/UserStateListener.php
index 72a10cb7..116e2774 100755
--- a/src/Security/Listeners/UserStateListener.php
+++ b/src/Security/Listeners/UserStateListener.php
@@ -14,7 +14,7 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Venne\Notifications\NotificationManager;
use Venne\Security\Events\RegistrationEvent;
-use Venne\Security\UserEntity;
+use Venne\Security\User;
/**
* @author Josef Kříž
@@ -34,10 +34,10 @@ public function __construct(NotificationManager $notificationManager)
}
/**
- * @param \Venne\Security\UserEntity $entity
+ * @param \Venne\Security\User $entity
* @param \Doctrine\ORM\Event\LifecycleEventArgs $event
*/
- public function postPersist(UserEntity $entity, LifecycleEventArgs $event)
+ public function postPersist(User $entity, LifecycleEventArgs $event)
{
if (!self::$lock) {
self::$lock = true;
diff --git a/src/Security/LoginEntity.php b/src/Security/Login.php
old mode 100755
new mode 100644
similarity index 83%
rename from src/Security/LoginEntity.php
rename to src/Security/Login.php
index d21e7637..cb43a211
--- a/src/Security/LoginEntity.php
+++ b/src/Security/Login.php
@@ -21,7 +21,7 @@
* @ORM\Entity
* @ORM\Table(name="login",indexes={@ORM\Index(name="session_idx", columns={"session_id"})})
*/
-class LoginEntity extends \Kdyby\Doctrine\Entities\BaseEntity
+class Login extends \Kdyby\Doctrine\Entities\BaseEntity
{
use \Venne\Doctrine\Entities\IdentifiedEntityTrait;
@@ -29,9 +29,9 @@ class LoginEntity extends \Kdyby\Doctrine\Entities\BaseEntity
const USER_ADMIN = null;
/**
- * @var \Venne\Security\UserEntity
+ * @var \Venne\Security\User
*
- * @ORM\ManyToOne(targetEntity="\Venne\Security\UserEntity", inversedBy="logins")
+ * @ORM\ManyToOne(targetEntity="\Venne\Security\User", inversedBy="logins")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $user;
@@ -59,9 +59,9 @@ class LoginEntity extends \Kdyby\Doctrine\Entities\BaseEntity
/**
* @param string $sessionId
- * @param \Venne\Security\UserEntity $user
+ * @param \Venne\Security\User $user
*/
- public function __construct($sessionId, UserEntity $user = null)
+ public function __construct($sessionId, User $user = null)
{
$this->setUser($user);
$this->setSessionId($sessionId);
@@ -85,13 +85,13 @@ public function getSessionId()
return $this->sessionId;
}
- public function setUser(UserEntity $user)
+ public function setUser(User $user)
{
$this->user = $user;
}
/**
- * @return \Venne\Security\UserEntity
+ * @return \Venne\Security\User
*/
public function getUser()
{
diff --git a/src/Security/Login/LoginControl.panel.latte b/src/Security/Login/LoginControl.panel.latte
index 704226dd..b7e789b9 100755
--- a/src/Security/Login/LoginControl.panel.latte
+++ b/src/Security/Login/LoginControl.panel.latte
@@ -2,7 +2,7 @@