Skip to content

Commit

Permalink
delete stale pending users closes #4069.
Browse files Browse the repository at this point in the history
Add email notification when registration is deleted refs #2915.
  • Loading branch information
craigh committed Mar 29, 2020
1 parent a22eb09 commit 0a5ef89
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 49 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- `Zikula\Core\RouteUrl` moved into `Zikula\Bundle\CoreBundle\`.
- `Zikula\Core\UrlInterface` moved into `Zikula\Bundle\CoreBundle\`.
- `Zikula\ThemeModule\AbstractTheme` moved into `Zikula\ExtensionsModule\`.
- `Zikula\UsersModule\RegistrationEvents::DELETE_REGISTRATION` is removed in favor of `Zikula\UsersModule\Event\DeletedRegistrationEvent`
- MailerApi and Swift_Mailer is fully removed in favor of the Symfony Mailer Component. Mailer is configurable in MailerModule (#4000).
- Interface extensions and amendments
- Removed second argument (`$first = true`) from `ZikulaHttpKernelInterface` methods `getModule`, `getTheme` and `isBundle` (#3377).
Expand All @@ -50,8 +51,8 @@
- `Zikula\Bundle\HookBundle\Collector\HookCollectorInterface` has changed signature of `addProvider()` and `addSubscriber()` methods.
- `Zikula\Common\Content\ContentTypeInterface` requires a new method `getBundleName()` to be implemented.
- `Zikula\PermissionsModule\Entity\RepositoryInterface\PermissionRepositoryInterface` requires new methods `getAllColours()` and `deleteGroupPermissions()` to be implemented.
- `Zikula\ŞearchModule\SearchableInterface` requires a new method `getBundleName()` to be implemented.
- `Zikula\ŞearchModule\SearchableInterface` has changed signature of `getResults()` method.
- `Zikula\SearchModule\SearchableInterface` requires a new method `getBundleName()` to be implemented.
- `Zikula\SearchModule\SearchableInterface` has changed signature of `getResults()` method.
- `Zikula\UsersModule\MessageModule\MessageModuleInterface` requires a new method `getBundleName()` to be implemented.
- `Zikula\UsersModule\ProfileModule\ProfileModuleInterface` requires a new method `getBundleName()` to be implemented.
- `Zikula\BlocksModule\AbstractBlockHandler` is not ContainerAware anymore.
Expand Down Expand Up @@ -180,6 +181,7 @@
- Fixed logic of `CategoryProcessingHelper#mayCategoryBeDeletedOrMoved` (#3920).
- Fixed import of users from a file (#4161).
- Fixed problem with deleting user(s) from a very large database (#3953).
- Fixed problem where stale pending registrations were not deleted (#4069).

- Features:
- Utilise autowiring and autoconfiguring functionality from Symfony (#3940).
Expand Down Expand Up @@ -227,6 +229,7 @@
- Added CLI Command to create any number of users for testing purposes `bin/console zikula:users:generate`.
- Added CLI Command to delete any number of users `bin/console zikula:users:delete`.
- Added `Zikula\Bundle\CoreBundle\Helper\LocalDotEnvHelper` to assist in writing to the `.env.local` file.
- Added email notification to deleted pending registrations (#2915).

- Vendor updates:
- antishov/doctrine-extensions-bundle updated from 1.2.2 to 1.4.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ currentMenu: users

The following classes provide different events:

- `\Zikula\UsersModule\Events\*`
- `\Zikula\UsersModule\AccessEvents`
- `\Zikula\UsersModule\RegistrationEvents`
- `\Zikula\UsersModule\UserEvents`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Zikula\Bundle\CoreBundle\Event\GenericEvent;
use Zikula\UsersModule\Event\DeletedRegistrationEvent;
use Zikula\UsersModule\RegistrationEvents;

/**
Expand All @@ -32,11 +33,11 @@ public static function getSubscribedEvents()
RegistrationEvents::REGISTRATION_FAILED => ['failed', 5],
RegistrationEvents::CREATE_REGISTRATION => ['create', 5],
RegistrationEvents::UPDATE_REGISTRATION => ['update', 5],
RegistrationEvents::DELETE_REGISTRATION => ['delete', 5],
DeletedRegistrationEvent::class => ['delete', 5],
RegistrationEvents::FORCE_REGISTRATION_APPROVAL => ['forceApproval', 5]
];
}

/**
* Listener for the `module.users.ui.registration.started` event.
*
Expand All @@ -51,7 +52,7 @@ public static function getSubscribedEvents()
public function started(GenericEvent $event): void
{
}

/**
* Listener for the `full.user.create.veto` event.
*
Expand Down Expand Up @@ -82,7 +83,7 @@ public function started(GenericEvent $event): void
public function createVeto(GenericEvent $event): void
{
}

/**
* Listener for the `module.users.ui.registration.succeeded` event.
*
Expand Down Expand Up @@ -150,7 +151,7 @@ public function createVeto(GenericEvent $event): void
public function succeeded(GenericEvent $event): void
{
}

/**
* Listener for the `module.users.ui.registration.failed` event.
*
Expand Down Expand Up @@ -181,7 +182,7 @@ public function succeeded(GenericEvent $event): void
public function failed(GenericEvent $event): void
{
}

/**
* Listener for the `user.registration.create` event.
*
Expand All @@ -201,7 +202,7 @@ public function failed(GenericEvent $event): void
public function create(GenericEvent $event): void
{
}

/**
* Listener for the `user.registration.update` event.
*
Expand All @@ -219,26 +220,26 @@ public function create(GenericEvent $event): void
public function update(GenericEvent $event): void
{
}

/**
* Listener for the `user.registration.delete` event.
* Listener for the `Zikula\UsersModule\Event\DeletedRegistrationEvent` event.
*
* Occurs after a registration record is deleted. This could occur as a result of the administrator deleting
* the record through the approval/denial process, or it could happen because the registration request expired.
* This event will not fire if a registration record is converted to a full user account record. Instead,
* a `user.account.create` event will fire. This is a storage-level event, not a UI event. It should not be
* used for UI-level actions such as redirects. The subject of the event is set to the Uid being deleted.
*
* You can access general data available in the event.
* You can access the user and date in the event.
*
* The event name:
* `echo 'Event: ' . $event->getName();`
* The user:
* `echo 'UID: ' . $event->getUser()->getUid();`
*
*/
public function delete(GenericEvent $event): void
public function delete(DeletedRegistrationEvent $event): void
{
}

/**
* Listener for the `force.registration.approval` event.
*
Expand Down
3 changes: 2 additions & 1 deletion src/system/UsersModule/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Zikula\UsersModule\Constant as UsersConstant;
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
use Zikula\UsersModule\Entity\UserEntity;
use Zikula\UsersModule\Event\DeletedRegistrationEvent;
use Zikula\UsersModule\Event\UserFormAwareEvent;
use Zikula\UsersModule\Event\UserFormDataEvent;
use Zikula\UsersModule\Exception\InvalidAuthenticationMethodRegistrationFormException;
Expand Down Expand Up @@ -178,7 +179,7 @@ public function registerAction(
// revert registration
$this->addFlash('error', 'The registration process failed.');
$userRepository->removeAndFlush($userEntity);
$eventDispatcher->dispatch(new GenericEvent($userEntity->getUid()), RegistrationEvents::DELETE_REGISTRATION);
$eventDispatcher->dispatch(new DeletedRegistrationEvent($userEntity));

return $this->redirectToRoute('zikulausersmodule_registration_register'); // try again.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Zikula\UsersModule\Constant as UsersConstant;
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
use Zikula\UsersModule\Entity\UserEntity;
use Zikula\UsersModule\Event\DeletedRegistrationEvent;
use Zikula\UsersModule\Event\UserFormAwareEvent;
use Zikula\UsersModule\Event\UserFormDataEvent;
use Zikula\UsersModule\Form\Type\AdminModifyUserType;
Expand All @@ -54,7 +55,6 @@
use Zikula\UsersModule\Helper\MailHelper;
use Zikula\UsersModule\Helper\RegistrationHelper;
use Zikula\UsersModule\HookSubscriber\UserManagementUiHooksSubscriber;
use Zikula\UsersModule\RegistrationEvents;
use Zikula\UsersModule\UserEvents;

/**
Expand Down Expand Up @@ -332,8 +332,11 @@ public function deleteAction(
// send email to 'denied' registrations. see MailHelper::sendNotification (regdeny) #2915
$deletedUsers = $userRepository->query(['uid' => ['operator' => 'in', 'operand' => $userIds]]);
foreach ($deletedUsers as $deletedUser) {
$eventName = UsersConstant::ACTIVATED_ACTIVE === $deletedUser->getActivated() ? UserEvents::DELETE_ACCOUNT : RegistrationEvents::DELETE_REGISTRATION;
$eventDispatcher->dispatch(new GenericEvent($deletedUser->getUid()), $eventName);
if (UsersConstant::ACTIVATED_ACTIVE === $deletedUser->getActivated()) {
$eventDispatcher->dispatch(new GenericEvent($deletedUser->getUid()), UserEvents::DELETE_ACCOUNT);
} else {
$eventDispatcher->dispatch(new DeletedRegistrationEvent($deletedUser));
}
$eventDispatcher->dispatch(new GenericEvent(null, ['id' => $deletedUser->getUid()]), UserEvents::DELETE_PROCESS);
$hookDispatcher->dispatch(UserManagementUiHooksSubscriber::DELETE_PROCESS, new ProcessHook($deletedUser->getUid()));
$userRepository->removeAndFlush($deletedUser);
Expand Down
53 changes: 53 additions & 0 deletions src/system/UsersModule/Event/DeletedRegistrationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\UsersModule\Event;

use Zikula\UsersModule\Entity\UserEntity;

/**
* Occurs after a registration record is deleted. This could occur as a result of the administrator deleting the
* record through the approval/denial process, or it could happen because the registration request expired. This
* event will not fire if a registration record is converted to a full user account record. Instead, a
* `user.account.create` event will fire. This is a storage-level event, not a UI event. It should not be used for
* UI-level actions such as redirects.
* The subject of the event is set to the Uid being deleted.
*/
class DeletedRegistrationEvent
{
/**
* @var UserEntity
*/
private $user;

/**
* @var \DateTimeImmutable
*/
private $date;

public function __construct(UserEntity $user)
{
$this->user = $user;
$this->date = new \DateTimeImmutable('now');
}

public function getUser(): UserEntity
{
return $this->user;
}

public function getDate(): \DateTimeImmutable
{
return $this->date;
}
}
9 changes: 6 additions & 3 deletions src/system/UsersModule/Helper/DeleteHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
use Zikula\UsersModule\Constant as UsersConstant;
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
use Zikula\UsersModule\Entity\UserEntity;
use Zikula\UsersModule\Event\DeletedRegistrationEvent;
use Zikula\UsersModule\HookSubscriber\UserManagementUiHooksSubscriber;
use Zikula\UsersModule\RegistrationEvents;
use Zikula\UsersModule\UserEvents;

class DeleteHelper
Expand Down Expand Up @@ -113,8 +113,11 @@ public function getUserCollection(string $param, string $value, string $date = n

public function deleteUser(UserEntity $user): void
{
$eventName = UsersConstant::ACTIVATED_ACTIVE === $user->getActivated() ? UserEvents::DELETE_ACCOUNT : RegistrationEvents::DELETE_REGISTRATION;
$this->eventDispatcher->dispatch(new GenericEvent($user->getUid()), $eventName);
if (UsersConstant::ACTIVATED_ACTIVE === $user->getActivated()) {
$this->eventDispatcher->dispatch(new GenericEvent($user->getUid()), UserEvents::DELETE_ACCOUNT);
} else {
$this->eventDispatcher->dispatch(new DeletedRegistrationEvent($user));
}
$this->eventDispatcher->dispatch(new GenericEvent(null, ['id' => $user->getUid()]), UserEvents::DELETE_PROCESS);
$this->hookDispatcher->dispatch(UserManagementUiHooksSubscriber::DELETE_PROCESS, new ProcessHook($user->getUid()));
$this->userRepository->removeAndFlush($user);
Expand Down
10 changes: 0 additions & 10 deletions src/system/UsersModule/RegistrationEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,6 @@ class RegistrationEvents
*/
public const UPDATE_REGISTRATION = 'user.registration.update';

/**
* Occurs after a registration record is deleted. This could occur as a result of the administrator deleting the
* record through the approval/denial process, or it could happen because the registration request expired. This
* event will not fire if a registration record is converted to a full user account record. Instead, a
* `user.account.create` event will fire. This is a storage-level event, not a UI event. It should not be used for
* UI-level actions such as redirects.
* The subject of the event is set to the Uid being deleted.
*/
public const DELETE_REGISTRATION = 'user.registration.delete';

/**
* Occurs when an administrator approves a registration. The UserEntity is the subject.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Zikula\UsersModule\Constant as UsersConstant;
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
use Zikula\UsersModule\Entity\UserEntity;
use Zikula\UsersModule\Event\DeletedRegistrationEvent;
use Zikula\UsersModule\Event\UserFormAwareEvent;
use Zikula\UsersModule\Event\UserFormDataEvent;
use Zikula\UsersModule\Helper\MailHelper as UsersMailHelper;
Expand Down Expand Up @@ -197,19 +198,18 @@ public function createAction(
$this->addFlash('error', 'Errors creating user!');
$this->addFlash('error', implode('<br />', $notificationErrors));
}
$userId = $user->getUid();
$mapping->setUid($user->getUid());
$mapping->setVerifiedEmail(!$form['usermustverify']->getData());
if (!$authMethod->register($mapping->toArray())) {
$this->addFlash('error', 'The create process failed for an unknown reason.');
$userRepository->removeAndFlush($user);
$eventDispatcher->dispatch(new GenericEvent($userId), RegistrationEvents::DELETE_REGISTRATION);
$eventDispatcher->dispatch(new DeletedRegistrationEvent($user));

return $this->redirectToRoute('zikulazauthmodule_useradministration_list');
}
$formDataEvent = new UserFormDataEvent($user, $form);
$eventDispatcher->dispatch($formDataEvent, UserEvents::EDIT_FORM_HANDLE);
$hook = new ProcessHook($userId);
$hook = new ProcessHook($user->getUid());
$hookDispatcher->dispatch(UserManagementUiHooksSubscriber::EDIT_PROCESS, $hook);
$eventDispatcher->dispatch(new GenericEvent($user), RegistrationEvents::REGISTRATION_SUCCEEDED);

Expand Down
Loading

0 comments on commit 0a5ef89

Please sign in to comment.