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
5 changes: 5 additions & 0 deletions cli/joomla.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@

$app = \Joomla\CMS\Factory::getContainer()->get(\Joomla\Console\Application::class);
\Joomla\CMS\Factory::$application = $app;

if ($app instanceof \Joomla\CMS\User\UserFactoryAwareInterface) {
$app->setUserFactory($container->get(\Joomla\CMS\User\UserFactoryInterface::class));
}

$app->execute();
29 changes: 28 additions & 1 deletion 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\UserFactoryAwareInterface;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\CMS\Version;
use Joomla\Console\Application;
use Joomla\Database\DatabaseAwareTrait;
Expand All @@ -40,14 +42,15 @@
*
* @since 4.0.0
*/
class ConsoleApplication extends Application implements CMSApplicationInterface
class ConsoleApplication extends Application implements CMSApplicationInterface, UserFactoryAwareInterface
{
use EventAware;
use IdentityAware;
use ContainerAwareTrait;
use ExtensionManagerTrait;
use ExtensionNamespaceMapper;
use DatabaseAwareTrait;
use UserFactoryAwareTrait;

/**
* The input.
Expand Down Expand Up @@ -247,6 +250,20 @@ public function execute()
*/
$this->populateHttpHost();

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

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

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

// 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 +566,7 @@ protected function populateHttpHost()
protected function getDefaultInputDefinition(): InputDefinition
{
$inputDefinition = parent::getDefaultInputDefinition();

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

$inputDefinition->addOption(
new InputOption(
'--user',
null,
InputOption::VALUE_OPTIONAL,
'The user to use, can be the user id or username'
)
);

return $inputDefinition;
}

Expand Down
9 changes: 9 additions & 0 deletions libraries/src/Console/AddUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Joomla\CMS\Console;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\User\User;
use Joomla\Console\Command\AbstractCommand;
use Joomla\Database\DatabaseAwareTrait;
Expand Down Expand Up @@ -142,6 +144,13 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in
return Command::FAILURE;
}

// Load the action log plugins when an identity is set
$app = $this->getApplication();
if ($app instanceof CMSApplicationInterface && $app->getIdentity() instanceof User) {
PluginHelper::importPlugin('actionlog');
$app->getInput()->set('option', 'com_users');
}

// Get filter to remove invalid characters
$filter = new InputFilter();

Expand Down
9 changes: 9 additions & 0 deletions libraries/src/Console/ChangeUserPasswordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Joomla\CMS\Console;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\User\User;
use Joomla\CMS\User\UserHelper;
use Joomla\Console\Command\AbstractCommand;
Expand Down Expand Up @@ -93,6 +95,13 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in
return Command::FAILURE;
}

// Load the action log plugins when an identity is set
$app = $this->getApplication();
if ($app instanceof CMSApplicationInterface && $app->getIdentity() instanceof User) {
PluginHelper::importPlugin('actionlog');
$app->getInput()->set('option', 'com_users');
}

$user = User::getInstance($userId);
$this->password = $this->getStringFromOption('password', 'Please enter a new password');

Expand Down
9 changes: 9 additions & 0 deletions libraries/src/Console/DeleteUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Joomla\CMS\Console;

use Joomla\CMS\Access\Access;
use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\User\User;
use Joomla\CMS\User\UserHelper;
use Joomla\Console\Command\AbstractCommand;
Expand Down Expand Up @@ -142,6 +144,13 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in
}
}

// Load the action log plugins when an identity is set
$app = $this->getApplication();
if ($app instanceof CMSApplicationInterface && $app->getIdentity() instanceof User) {
PluginHelper::importPlugin('actionlog');
$app->getInput()->set('option', 'com_users');
}

// Trigger delete of user
$result = $user->delete();

Expand Down
4 changes: 2 additions & 2 deletions libraries/src/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public function save($updateOnly = false)

// @todo ACL - this needs to be acl checked

$my = Factory::getUser();
$my = Factory::getApplication()->getIdentity() ?: Factory::getUser();

// Are we creating a new user
$isNew = empty($this->id);
Expand Down Expand Up @@ -748,7 +748,7 @@ public function save($updateOnly = false)
// Check if we are using a CLI application
$isCli = false;

if (Factory::getApplication()->isCli()) {
if ($my->id === 0 && Factory::getApplication()->isCli()) {
$isCli = true;
}

Expand Down