Skip to content

Commit

Permalink
use aware trait
Browse files Browse the repository at this point in the history
  • Loading branch information
laoneo committed Apr 20, 2023
1 parent 2e06b5a commit 34ac782
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 83 deletions.
2 changes: 1 addition & 1 deletion plugins/system/actionlogs/services/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ function (Container $container) {
$plugin = new ActionLogs(
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('system', 'actionlogs'),
$container->get(UserFactoryInterface::class)
);
$plugin->setApplication(Factory::getApplication());
$plugin->setDatabase($container->get(DatabaseInterface::class));
$plugin->setUserFactory($container->get(UserFactoryInterface::class));

return $plugin;
}
Expand Down
27 changes: 8 additions & 19 deletions plugins/system/actionlogs/src/Extension/ActionLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\Component\Actionlogs\Administrator\Helper\ActionlogsHelper;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\Exception\ExecutionFailureException;
Expand All @@ -39,31 +39,20 @@
final class ActionLogs extends CMSPlugin
{
use DatabaseAwareTrait;

/**
* The user factory
*
* @var UserFactoryInterface
*
* @since __DEPLOY_VERSION__
*/
private $userFactory;
use UserFactoryAwareTrait;

/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param UserFactoryInterface $userFactory The user factory
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
*
* @since 3.9.0
*/
public function __construct(DispatcherInterface $dispatcher, array $config, UserFactoryInterface $userFactory)
public function __construct(DispatcherInterface $dispatcher, array $config)
{
parent::__construct($dispatcher, $config);

$this->userFactory = $userFactory;

// Import actionlog plugin group so that these plugins will be triggered for events
PluginHelper::importPlugin('actionlog');
}
Expand Down Expand Up @@ -127,7 +116,7 @@ public function onContentPrepareForm(Form $form, $data)
$data = (object) $data;
}

if (empty($data->id) || !$this->userFactory->loadUserById($data->id)->authorise('core.admin')) {
if (empty($data->id) || !$this->getUserFactory()->loadUserById($data->id)->authorise('core.admin')) {
return true;
}

Expand Down Expand Up @@ -168,7 +157,7 @@ public function onContentPrepareData($context, $data)
$data = (object) $data;
}

if (!$this->userFactory->loadUserById($data->id)->authorise('core.admin')) {
if (!$this->getUserFactory()->loadUserById($data->id)->authorise('core.admin')) {
return true;
}

Expand Down Expand Up @@ -348,7 +337,7 @@ public function onUserAfterSave($user, $isNew, $success, $msg): void
}

// Clear access rights in case user groups were changed.
$userObject = $this->userFactory->loadUserById($user['id']);
$userObject = $this->getUserFactory()->loadUserById($user['id']);
$userObject->clearAccessRights();

$authorised = $userObject->authorise('core.admin');
Expand Down
7 changes: 4 additions & 3 deletions plugins/system/fields/services/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public function register(Container $container): void
$container->set(
PluginInterface::class,
function (Container $container) {
$plugin = new Fields(
$container->get(DispatcherInterface::class),
$dispatcher = $container->get(DispatcherInterface::class),
$plugin = new Fields(
$dispatcher,
(array) PluginHelper::getPlugin('system', 'fields'),
$container->get(UserFactoryInterface::class)
);
$plugin->setApplication(Factory::getApplication());
$plugin->setUserFactory($container->get(UserFactoryInterface::class));

return $plugin;
}
Expand Down
32 changes: 4 additions & 28 deletions plugins/system/fields/src/Extension/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
use Joomla\Event\DispatcherInterface;
use Joomla\Registry\Registry;
use stdClass;

Expand All @@ -30,6 +29,8 @@
*/
final class Fields extends CMSPlugin
{
use UserFactoryAwareTrait;

/**
* Load the language file on instantiation.
*
Expand All @@ -38,31 +39,6 @@ final class Fields extends CMSPlugin
*/
protected $autoloadLanguage = true;

/**
* The user factory
*
* @var UserFactoryInterface
*
* @since __DEPLOY_VERSION__
*/
private $userFactory;

/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param UserFactoryInterface $userFactory The user factory
*
* @since __DEPLOY_VERSION__
*/
public function __construct(DispatcherInterface $dispatcher, array $config, UserFactoryInterface $userFactory)
{
parent::__construct($dispatcher, $config);

$this->userFactory = $userFactory;
}

/**
* Normalizes the request data.
*
Expand Down Expand Up @@ -202,7 +178,7 @@ public function onUserAfterSave($userData, $isNew, $success, $msg): void
return;
}

$user = $this->userFactory->loadUserById($userData['id']);
$user = $this->getUserFactory()->loadUserById($userData['id']);

$task = $this->getApplication()->getInput()->getCmd('task');

Expand Down
7 changes: 4 additions & 3 deletions plugins/system/tasknotification/services/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ public function register(Container $container): void
$container->set(
PluginInterface::class,
function (Container $container) {
$plugin = new TaskNotification(
$container->get(DispatcherInterface::class),
$dispatcher = $container->get(DispatcherInterface::class);
$plugin = new TaskNotification(
$dispatcher,
(array) PluginHelper::getPlugin('system', 'tasknotification'),
$container->get(UserFactoryInterface::class)
);
$plugin->setApplication(Factory::getApplication());
$plugin->setDatabase($container->get(DatabaseInterface::class));
$plugin->setUserFactory($container->get(UserFactoryInterface::class));

return $plugin;
}
Expand Down
32 changes: 3 additions & 29 deletions plugins/system/tasknotification/src/Extension/TaskNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Mail\MailTemplate;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\Component\Scheduler\Administrator\Task\Status;
use Joomla\Component\Scheduler\Administrator\Task\Task;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Event\DispatcherInterface;
use Joomla\Event\Event;
use Joomla\Event\EventInterface;
use Joomla\Event\SubscriberInterface;
Expand All @@ -47,6 +45,7 @@
final class TaskNotification extends CMSPlugin implements SubscriberInterface
{
use DatabaseAwareTrait;
use UserFactoryAwareTrait;

/**
* The task notification form. This form is merged into the task item form by {@see
Expand All @@ -63,15 +62,6 @@ final class TaskNotification extends CMSPlugin implements SubscriberInterface
*/
protected $autoloadLanguage = true;

/**
* The user factory
*
* @var UserFactoryInterface
*
* @since __DEPLOY_VERSION__
*/
private $userFactory;

/**
* @inheritDoc
*
Expand All @@ -90,22 +80,6 @@ public static function getSubscribedEvents(): array
];
}

/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param UserFactoryInterface $userFactory The user factory
*
* @since __DEPLOY_VERSION__
*/
public function __construct(DispatcherInterface $dispatcher, array $config, UserFactoryInterface $userFactory)
{
parent::__construct($dispatcher, $config);

$this->userFactory = $userFactory;
}

/**
* Inject fields to support configuration of post-execution notifications into the task item form.
*
Expand Down Expand Up @@ -306,7 +280,7 @@ private function sendMail(string $template, array $data, string $attachment = ''

// Mail all matching users who also have the `core.manage` privilege for com_scheduler.
foreach ($users as $user) {
$user = $this->userFactory->loadUserById($user->id);
$user = $this->getUserFactory()->loadUserById($user->id);

if ($user->authorise('core.manage', 'com_scheduler')) {
try {
Expand Down

0 comments on commit 34ac782

Please sign in to comment.