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

[4.2] Convert joomla action log plugin to services #37788

Merged
merged 27 commits into from May 30, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8ec1661
Merge action log DB changes
laoneo May 13, 2022
02b0b6e
Convert joomla action log plugin to services
laoneo May 13, 2022
3b1b366
import
laoneo May 14, 2022
a4aee09
cs
laoneo May 14, 2022
24753a2
Merge branch '4.2-dev' into j4/plugins/actionlog/joomla
laoneo May 14, 2022
8c6937a
doc
laoneo May 14, 2022
bc8beb2
Merge branch 'j4/plugins/actionlog/joomla' of github.com:Digital-Peak…
laoneo May 14, 2022
f74f563
Merge branch '4.2-dev' into j4/plugins/actionlog/joomla
laoneo May 15, 2022
35acc88
cs
laoneo May 15, 2022
4f54dcc
Move to root
laoneo May 15, 2022
b987c1a
Update plugins/actionlog/joomla/services/provider.php
laoneo May 16, 2022
495be7e
doc
laoneo May 16, 2022
f74643c
Merge branch 'j4/plugins/actionlog/joomla' of github.com:Digital-Peak…
laoneo May 16, 2022
96dead5
more doc
laoneo May 16, 2022
f36344c
Merge branch '4.2-dev' into j4/plugins/actionlog/joomla
laoneo May 23, 2022
2c1a2d3
Merge branch '4.2-dev' into j4/plugins/actionlog/joomla
laoneo May 23, 2022
c49d860
Merge branch '4.2-dev' into j4/plugins/actionlog/joomla
laoneo May 24, 2022
1f03ed3
Update plugins/actionlog/joomla/services/provider.php
laoneo May 30, 2022
afaca09
Update plugins/actionlog/joomla/services/provider.php
laoneo May 30, 2022
713a149
moved to extension
laoneo May 30, 2022
fb8f8bf
Merge remote-tracking branch 'upstream/4.2-dev' into j4/plugins/actio…
laoneo May 30, 2022
12dce3a
Merge branch '4.2-dev' into j4/plugins/actionlog/joomla
laoneo May 30, 2022
a97b0f9
cs
laoneo May 30, 2022
a4aae75
Update plugins/actionlog/joomla/services/provider.php
laoneo May 30, 2022
051abe3
lower case
laoneo May 30, 2022
a878b87
Merge branch 'j4/plugins/actionlog/joomla' of github.com:Digital-Peak…
laoneo May 30, 2022
7010457
Merge branch '4.2-dev' into j4/plugins/actionlog/joomla
roland-d May 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -158,19 +158,13 @@ public static function loadTranslationFiles($extension)
* @return mixed An object contains content type parameters, or null if not found
*
* @since 3.9.0
*
* @deprecated 5.0 Use the action log config model instead
*/
public static function getLogContentTypeParams($context)
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('a.*')
->from($db->quoteName('#__action_log_config', 'a'))
->where($db->quoteName('a.type_alias') . ' = :context')
->bind(':context', $context);

$db->setQuery($query);

return $db->loadObject();
return Factory::getApplication()->bootComponent('actionlogs')->getMVCFactory()
->createModel('ActionlogConfig', 'Administrator')->getLogContentTypeParams($context);
}

/**
Expand Down
@@ -0,0 +1,46 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_actionlogs
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Component\Actionlogs\Administrator\Model;

\defined('_JEXEC') or die;

use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use stdClass;

/**
* Model to interact with the action log configuration.
*
* @since __DEPLOY_VERSION__
*/
class ActionlogConfigModel extends BaseDatabaseModel
{
/**
* Returns the action logs config for the given context.
*
* @param string $context The context of the content
*
* @return stdClass|null An object contains content type parameters, or null if not found
*
* @since __DEPLOY_VERSION__
*/
public function getLogContentTypeParams(string $context): ?stdClass
{
$db = $this->getDatabase();
$query = $db->getQuery(true)
->select('a.*')
->from($db->quoteName('#__action_log_config', 'a'))
->where($db->quoteName('a.type_alias') . ' = :context')
->bind(':context', $context);

$db->setQuery($query);

return $db->loadObject();
}
}
4 changes: 3 additions & 1 deletion plugins/actionlog/joomla/joomla.xml
Expand Up @@ -9,8 +9,10 @@
<authorUrl>www.joomla.org</authorUrl>
<version>3.9.0</version>
<description>PLG_ACTIONLOG_JOOMLA_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Actionlog\Joomla</namespace>
<files>
<filename plugin="joomla">joomla.php</filename>
<folder plugin="joomla">services</folder>
laoneo marked this conversation as resolved.
Show resolved Hide resolved
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_actionlog_joomla.ini</language>
Expand Down
46 changes: 46 additions & 0 deletions plugins/actionlog/joomla/services/provider.php
@@ -0,0 +1,46 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage System.Actionlog
laoneo marked this conversation as resolved.
Show resolved Hide resolved
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\Actionlog\Joomla\Extension\Joomla;

return new class implements ServiceProviderInterface {
/**
laoneo marked this conversation as resolved.
Show resolved Hide resolved
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
laoneo marked this conversation as resolved.
Show resolved Hide resolved
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container)
{
$container->set(
PluginInterface::class,
function (Container $container)
{
$plugin = new Joomla(
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('system', 'cache'),
laoneo marked this conversation as resolved.
Show resolved Hide resolved
);
$plugin->setDatabase($container->get(DatabaseInterface::class));

return $plugin;
}
);
}
};
@@ -1,30 +1,49 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage System.actionlogs
* @subpackage System.actionlog
*
* @copyright (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Plugin\Actionlog\Joomla\Extension;

defined('_JEXEC') or die;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\MVC\Factory\MVCFactoryServiceInterface;
use Joomla\CMS\Table\Table;
use Joomla\CMS\User\User;
use Joomla\Component\Actionlogs\Administrator\Helper\ActionlogsHelper;
use Joomla\Component\Actionlogs\Administrator\Plugin\ActionLogPlugin;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\Exception\ExecutionFailureException;
use Joomla\Event\DispatcherInterface;
use Joomla\Utilities\ArrayHelper;
use RuntimeException;
use stdClass;

/**
* Joomla! Users Actions Logging Plugin.
*
* @since 3.9.0
*/
class PlgActionlogJoomla extends ActionLogPlugin
final class Joomla extends ActionLogPlugin
{
use DatabaseAwareTrait;

/**
* Application object.
*
* @var CMSApplicationInterface
* @since __DEPLOY_VERSION__
*/
protected $app;

/**
* Array of loggable extensions.
*
Expand Down Expand Up @@ -60,14 +79,14 @@ class PlgActionlogJoomla extends ActionLogPlugin
/**
* Constructor.
*
* @param object &$subject The object to observe.
* @param array $config An optional associative array of configuration settings.
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
*
* @since 3.9.0
*/
public function __construct(&$subject, $config)
public function __construct(DispatcherInterface $dispatcher, array $config)
{
parent::__construct($subject, $config);
parent::__construct($dispatcher, $config);

$params = ComponentHelper::getComponent('com_actionlogs')->getParams();

Expand All @@ -77,7 +96,6 @@ public function __construct(&$subject, $config)

$this->loggableVerbs = $params->get('loggable_verbs', []);
}

/**
laoneo marked this conversation as resolved.
Show resolved Hide resolved
* After save content logging method
* This method adds a record to #__action_logs contains (message, date, context, user)
Expand All @@ -98,7 +116,7 @@ public function onContentAfterSave($context, $article, $isNew): void
$context = $this->contextAliases[$context];
}

$params = ActionlogsHelper::getLogContentTypeParams($context);
$params = $this->getActionLogParams($context);

// Not found a valid content type, don't process further
if ($params === null)
Expand Down Expand Up @@ -164,7 +182,7 @@ public function onContentAfterDelete($context, $article): void
return;
}

$params = ActionlogsHelper::getLogContentTypeParams($context);
$params = $this->getActionLogParams($context);

// Not found a valid content type, don't process further
if ($params === null)
Expand Down Expand Up @@ -216,7 +234,7 @@ public function onContentChangeState($context, $pks, $value)
return;
}

$params = ActionlogsHelper::getLogContentTypeParams($context);
$params = $this->getActionLogParams($context);

// Not found a valid content type, don't process further
if ($params === null)
Expand Down Expand Up @@ -261,7 +279,7 @@ public function onContentChangeState($context, $pks, $value)
$messageLanguageKey = $defaultLanguageKey;
}

$db = $this->db;
$db = $this->getDatabase();
$query = $db->getQuery(true)
->select($db->quoteName([$params->title_holder, $params->id_holder]))
->from($db->quoteName($params->table_name))
Expand Down Expand Up @@ -457,7 +475,7 @@ public function onExtensionAfterUpdate($installer, $eid)
return;
}

$manifest = $installer->get('manifest');
$manifest = $installer->get('manifest');

if ($manifest === null)
{
Expand Down Expand Up @@ -513,7 +531,7 @@ public function onExtensionAfterSave($context, $table, $isNew): void
return;
}

$params = ActionlogsHelper::getLogContentTypeParams($context);
$params = $this->getActionLogParams($context);

// Not found a valid content type, don't process further
if ($params === null)
Expand Down Expand Up @@ -570,7 +588,7 @@ public function onExtensionAfterDelete($context, $table): void
return;
}

$params = ActionlogsHelper::getLogContentTypeParams($context);
$params = $this->getActionLogParams($context);

// Not found a valid content type, don't process further
if ($params === null)
Expand Down Expand Up @@ -839,17 +857,18 @@ public function onUserLoginFailure($response)
}

// Get the user id for the given username
$query = $this->db->getQuery(true)
->select($this->db->quoteName(array('id', 'username')))
->from($this->db->quoteName('#__users'))
->where($this->db->quoteName('username') . ' = ' . $this->db->quote($response['username']));
$this->db->setQuery($query);
$db = $this->getDatabase();
$query = $db->getQuery(true)
->select($db->quoteName(array('id', 'username')))
->from($db->quoteName('#__users'))
->where($db->quoteName('username') . ' = ' . $db->quote($response['username']));
$db->setQuery($query);

try
{
$loggedInUser = $this->db->loadObject();
$loggedInUser = $db->loadObject();
}
catch (\Joomla\Database\Exception\ExecutionFailureException $e)
catch (ExecutionFailureException $e)
{
return;
}
Expand Down Expand Up @@ -992,7 +1011,7 @@ public function onAfterCheckin($table)
'userid' => $user->id,
'username' => $user->username,
'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
'table' => str_replace($this->db->getPrefix(), '#__', $table),
'table' => str_replace($this->getDatabase()->getPrefix(), '#__', $table),
);

$this->addLog(array($message), 'PLG_ACTIONLOG_JOOMLA_USER_CHECKIN', $context, $user->id);
Expand Down Expand Up @@ -1153,7 +1172,7 @@ public function onJoomlaAfterUpdate($oldVersion = null)

if (empty($oldVersion))
{
$oldVersion = \Joomla\CMS\Language\Text::_('JLIB_UNKNOWN');
$oldVersion = $this->app->getLanguage()->_('JLIB_UNKNOWN');
}

$message = array(
Expand All @@ -1170,4 +1189,25 @@ public function onJoomlaAfterUpdate($oldVersion = null)
);
$this->addLog(array($message), 'PLG_ACTIONLOG_JOOMLA_USER_UPDATE', $context, $user->id);
}

/**
* Returns the action log params for the given context.
*
* @param string $context The context of the action log
*
* @return stdClass The params
*
* @since __DEPLOY_VERSION__
*/
private function getActionLogParams($context): ?stdClass
{
$component = $this->app->bootComponent('actionlogs');

if (!$component instanceof MVCFactoryServiceInterface)
{
return null;
}

return $component->getMVCFactory()->createModel('ActionlogConfig', 'Administrator')->getLogContentTypeParams($context);
}
}