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] Add user argument to perform the cli command with a specific user #44618

Open
wants to merge 9 commits into
base: 5.3-dev
Choose a base branch
from
25 changes: 25 additions & 0 deletions libraries/src/Application/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Router\Router;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\CMS\Version;
use Joomla\Console\Application;
use Joomla\Database\DatabaseAwareTrait;
Expand Down Expand Up @@ -48,6 +50,7 @@ class ConsoleApplication extends Application implements CMSApplicationInterface
use ExtensionManagerTrait;
use ExtensionNamespaceMapper;
use DatabaseAwareTrait;
use UserFactoryAwareTrait;

/**
* The input.
Expand Down Expand Up @@ -136,6 +139,7 @@ public function __construct(

$this->setContainer($container);
$this->setDispatcher($dispatcher);
$this->setUserFactory($container->get(UserFactoryInterface::class));

// Set the execution datetime and timestamp;
$this->set('execution.datetime', gmdate('Y-m-d H:i:s'));
Expand Down Expand Up @@ -247,6 +251,17 @@ public function execute()
*/
$this->populateHttpHost();

// Load the user when specified
$user = $this->getConsoleInput()->getParameterOption(['--user'], null);

if ($user !== null && is_numeric($user)) {
$this->loadIdentity($this->getUserFactory()->loadUserById((int)$user));
laoneo marked this conversation as resolved.
Show resolved Hide resolved
}

if ($user !== null && !is_numeric($user)) {
$this->loadIdentity($this->getUserFactory()->loadUserByUsername($user));
}

// Import CMS plugin groups to be able to subscribe to events
PluginHelper::importPlugin('behaviour', null, true, $this->getDispatcher());
PluginHelper::importPlugin('system', null, true, $this->getDispatcher());
Expand Down Expand Up @@ -549,6 +564,7 @@ protected function populateHttpHost()
protected function getDefaultInputDefinition(): InputDefinition
{
$inputDefinition = parent::getDefaultInputDefinition();

$inputDefinition->addOption(
new InputOption(
'--live-site',
Expand All @@ -558,6 +574,15 @@ protected function getDefaultInputDefinition(): InputDefinition
)
);

$inputDefinition->addOption(
new InputOption(
'--user',
null,
InputOption::VALUE_OPTIONAL,
'The user to use, can be the id or username'
laoneo marked this conversation as resolved.
Show resolved Hide resolved
laoneo marked this conversation as resolved.
Show resolved Hide resolved
)
);

return $inputDefinition;
}

Expand Down