Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] [Events] Use event classes for System plugins #43637

Open
wants to merge 19 commits into
base: 5.3-dev
Choose a base branch
from
Prev Previous commit
Next Next commit
system PrivacyConsent
  • Loading branch information
Fedik committed Jun 9, 2024
commit 7752d5f8a44859916a03035f22960ade0d0cfe14
73 changes: 45 additions & 28 deletions plugins/system/privacyconsent/src/Extension/PrivacyConsent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

namespace Joomla\Plugin\System\PrivacyConsent\Extension;

use Joomla\CMS\Event\Model;
use Joomla\CMS\Event\Privacy\CheckPrivacyPolicyPublishedEvent;
use Joomla\CMS\Event\User;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Language\Associations;
use Joomla\CMS\Language\Text;
Expand All @@ -21,6 +22,7 @@
use Joomla\Component\Actionlogs\Administrator\Model\ActionlogModel;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\ParameterType;
use Joomla\Event\SubscriberInterface;
use Joomla\Utilities\ArrayHelper;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -32,27 +34,48 @@
*
* @since 3.9.0
*/
final class PrivacyConsent extends CMSPlugin
final class PrivacyConsent extends CMSPlugin implements SubscriberInterface
{
use DatabaseAwareTrait;

/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public static function getSubscribedEvents(): array
{
return [
'onContentPrepareForm' => 'onContentPrepareForm',
'onUserBeforeSave' => 'onUserBeforeSave',
'onUserAfterSave' => 'onUserAfterSave',
'onUserAfterDelete' => 'onUserAfterDelete',
'onAfterRoute' => 'onAfterRoute',
'onPrivacyCheckPrivacyPolicyPublished' => 'onPrivacyCheckPrivacyPolicyPublished',
];
}

/**
* Adds additional fields to the user editing form
*
* @param Form $form The form to be altered.
* @param mixed $data The associated data for the form.
* @param Model\PrepareFormEvent $event The event instance.
*
* @return boolean
* @return void
*
* @since 3.9.0
*/
public function onContentPrepareForm(Form $form, $data)
public function onContentPrepareForm(Model\PrepareFormEvent $event): void
{
$form = $event->getForm();
$data = $event->getData();

// Check we are manipulating a valid form - we only display this on user registration form and user profile form.
$name = $form->getName();

if (!\in_array($name, ['com_users.profile', 'com_users.registration'])) {
return true;
return;
}

// Load plugin language files
Expand All @@ -63,7 +86,7 @@ public function onContentPrepareForm(Form $form, $data)
$userId = $data->id ?? 0;

if ($userId > 0 && $this->isUserConsented($userId)) {
return true;
return;
}
}

Expand All @@ -84,30 +107,29 @@ public function onContentPrepareForm(Form $form, $data)
/**
* Method is called before user data is stored in the database
*
* @param array $user Holds the old user data.
* @param boolean $isNew True if a new user is stored.
* @param array $data Holds the new user data.
* @param User\BeforeSaveEvent $event The event instance.
*
* @return boolean
* @return void
*
* @since 3.9.0
* @throws \InvalidArgumentException on missing required data.
*/
public function onUserBeforeSave($user, $isNew, $data)
public function onUserBeforeSave(User\BeforeSaveEvent $event): void
{
// // Only check for front-end user creation/update profile
if ($this->getApplication()->isClient('administrator')) {
return true;
return;
}

$user = $event->getUser();
$userId = ArrayHelper::getValue($user, 'id', 0, 'int');

// Load plugin language files
$this->loadLanguage();

// User already consented before, no need to check it further
if ($userId > 0 && $this->isUserConsented($userId)) {
return true;
return;
}

// Check that the privacy is checked if required ie only in registration from frontend.
Expand All @@ -122,30 +144,26 @@ public function onUserBeforeSave($user, $isNew, $data)
) {
throw new \InvalidArgumentException($this->getApplication()->getLanguage()->_('PLG_SYSTEM_PRIVACYCONSENT_FIELD_ERROR'));
}

return true;
}

/**
* Saves user privacy confirmation
*
* @param array $data entered user data
* @param boolean $isNew true if this is a new user
* @param boolean $result true if saving the user worked
* @param string $error error message
* @param User\AfterSaveEvent $event The event instance.
*
* @return void
*
* @since 3.9.0
*/
public function onUserAfterSave($data, $isNew, $result, $error): void
public function onUserAfterSave(User\AfterSaveEvent $event): void
{
// Only create an entry on front-end user creation/update profile
if ($this->getApplication()->isClient('administrator')) {
return;
}

// Get the user's ID
$data = $event->getUser();
$userId = ArrayHelper::getValue($data, 'id', 0, 'int');

// If user already consented before, no need to check it further
Expand Down Expand Up @@ -208,20 +226,19 @@ public function onUserAfterSave($data, $isNew, $result, $error): void
*
* Method is called after user data is deleted from the database
*
* @param array $user Holds the user data
* @param boolean $success True if user was successfully stored in the database
* @param string $msg Message
* @param User\AfterDeleteEvent $event The event instance.
*
* @return void
*
* @since 3.9.0
*/
public function onUserAfterDelete($user, $success, $msg): void
public function onUserAfterDelete(User\AfterDeleteEvent $event): void
{
if (!$success) {
if (!$event->getDeletingResult()) {
return;
}

$user = $event->getUser();
$userId = ArrayHelper::getValue($user, 'id', 0, 'int');

if ($userId) {
Expand All @@ -243,7 +260,7 @@ public function onUserAfterDelete($user, $success, $msg): void
*
* @since 3.9.0
*/
public function onAfterRoute()
public function onAfterRoute(): void
{
// Run this in frontend only
if (!$this->getApplication()->isClient('site')) {
Expand Down