Magento 2自定义控制台命令创建具有角色的管理员用户,Laminas服务定位器问题



我正在克隆Magento的AdminUserCreateCommand,以创建我的自定义命令来设置命令参数中给定的角色。计划是使用这个模块创建用户,就像默认的magento命令一样,现在不保存角色,当它工作时,我想为用户保存提供的角色,而不是默认的管理员角色。我将进一步使用/setup/src/Magento/Setup/Model/AdminAccount.php,并将retrieveAdministratorsRoleId()修改为通过该命令从获取的输入中设置角色名称。

app/code/Mymodule/CreateUser/registration.php

<?php
use MagentoFrameworkComponentComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Mymodule_CreateUser', __DIR__);

app/code/Mymodule/CreateUser/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mymodule_CreateUser" setup_version="1.0.0"/>
</config>

app/code/Mymodule/CreateUser/etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="LaminasServiceManagerServiceLocatorInterface" type="LaminasServiceManagerServiceManager" />
<preference for="MagentoFrameworkSetupLoggerInterface" type="MagentoFrameworkSetupConsoleLogger" />
<type name="MagentoFrameworkConsoleCommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="AdminUserCreateCommand" xsi:type="object">MymoduleCreateUserConsoleCommandAdminUserCreateCommand</item>
</argument>
</arguments>
</type>
</config>

app/code/Mymodule/CreateUser/Console/Command/AdminUserCreateCommand.php

<?php
declare(strict_types=1);
namespace MymoduleCreateUserConsoleCommand;
use MagentoFrameworkSetupConsoleLogger;
use MagentoSetupModelAdminAccount;
use MagentoSetupModelInstallerFactory;
use MagentoUserModelUserValidationRules;
use SymfonyComponentConsoleQuestionQuestion;
use MagentoSetupConsoleCommandAbstractSetupCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleOutputOutputInterface;
class AdminUserCreateCommand extends AbstractSetupCommand
{
/**
* @var InstallerFactory
*/
private $installerFactory;
/**
* @var UserValidationRules
*/
private $validationRules;
/**
* @param InstallerFactory $installerFactory
* @param UserValidationRules $validationRules
*/
public function __construct(InstallerFactory $installerFactory, UserValidationRules $validationRules)
{
$this->installerFactory = $installerFactory;
$this->validationRules = $validationRules;
parent::__construct();
}

/**
* Initialization of the command
*
* @return void
*/
protected function configure()
{
$this->setName('admin:myuser:create')
->setDescription('Creates an administrator with role')
->setDefinition($this->getOptionsList());
parent::configure();
}
/**
* Creation admin user in interaction mode.
*
* @param SymfonyComponentConsoleInputInputInterface $input
* @param SymfonyComponentConsoleOutputOutputInterface $output
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
/** @var SymfonyComponentConsoleHelperQuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');
if (!$input->getOption(AdminAccount::KEY_USER)) {
$question = new Question('<question>Admin user:</question> ', '');
$this->addNotEmptyValidator($question);
$input->setOption(
AdminAccount::KEY_USER,
$questionHelper->ask($input, $output, $question)
);
}
if (!$input->getOption(AdminAccount::KEY_PASSWORD)) {
$question = new Question('<question>Admin password:</question> ', '');
$question->setHidden(true);
$question->setValidator(function ($value) use ($output) {
$user = new MagentoFrameworkDataObject();
$user->setPassword($value);
$validator = new MagentoFrameworkValidatorDataObject();
$this->validationRules->addPasswordRules($validator);
$validator->isValid($user);
foreach ($validator->getMessages() as $message) {
throw new Exception($message);
}
return $value;
});
$input->setOption(
AdminAccount::KEY_PASSWORD,
$questionHelper->ask($input, $output, $question)
);
}
if (!$input->getOption('admin-role')) {
$question = new Question('<question>Admin role:</question> ', '');
$this->addNotEmptyValidator($question);
$input->setOption(
'admin-role',
$questionHelper->ask($input, $output, $question)
);
}
if (!$input->getOption(AdminAccount::KEY_EMAIL)) {
$question = new Question('<question>Admin email:</question> ', '');
$this->addNotEmptyValidator($question);
$input->setOption(
AdminAccount::KEY_EMAIL,
$questionHelper->ask($input, $output, $question)
);
}
if (!$input->getOption(AdminAccount::KEY_FIRST_NAME)) {
$question = new Question('<question>Admin first name:</question> ', '');
$this->addNotEmptyValidator($question);
$input->setOption(
AdminAccount::KEY_FIRST_NAME,
$questionHelper->ask($input, $output, $question)
);
}
if (!$input->getOption(AdminAccount::KEY_LAST_NAME)) {
$question = new Question('<question>Admin last name:</question> ', '');
$this->addNotEmptyValidator($question);
$input->setOption(
AdminAccount::KEY_LAST_NAME,
$questionHelper->ask($input, $output, $question)
);
}
}
/**
* Add not empty validator.
*
* @param SymfonyComponentConsoleQuestionQuestion $question
* @return void
*/
private function addNotEmptyValidator(Question $question)
{
$question->setValidator(function ($value) {
if (trim($value) == '') {
throw new Exception('The value cannot be empty');
}
return $value;
});
}
/**
* {@inheritdoc}
*/
protected function execute(
InputInterface $input,
OutputInterface $output
) {
$errors = $this->validate($input);
if ($errors) {
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
// we must have an exit code higher than zero to indicate something was wrong
return MagentoFrameworkConsoleCli::RETURN_FAILURE;
}
$installer = $this->installerFactory->create(new ConsoleLogger($output));
$installer->installAdminUser($input->getOptions());
$output->writeln(
'<info>Created Magento administrator user named ' . $input->getOption(AdminAccount::KEY_USER) . '</info>'
);
return MagentoFrameworkConsoleCli::RETURN_SUCCESS;
}
/**
* Get list of arguments for the command
*
* @param int $mode The mode of options.
* @return InputOption[]
*/
public function getOptionsList($mode = InputOption::VALUE_REQUIRED)
{
$requiredStr = ($mode === InputOption::VALUE_REQUIRED ? '(Required) ' : '');
return [
new InputOption(
AdminAccount::KEY_USER,
null,
$mode,
$requiredStr . 'Admin user'
),
new InputOption(
AdminAccount::KEY_PASSWORD,
null,
$mode,
$requiredStr . 'Admin password'
),
new InputOption(
'admin-role',
null,
$mode,
$requiredStr . 'Admin role'
),
new InputOption(
AdminAccount::KEY_EMAIL,
null,
$mode,
$requiredStr . 'Admin email'
),
new InputOption(
AdminAccount::KEY_FIRST_NAME,
null,
$mode,
$requiredStr . 'Admin first name'
),
new InputOption(
AdminAccount::KEY_LAST_NAME,
null,
$mode,
$requiredStr . 'Admin last name'
),
];
}
/**
* Check if all admin options are provided
*
* @param InputInterface $input
* @return string[]
*/
public function validate(InputInterface $input)
{
$errors = [];
$user = new MagentoFrameworkDataObject();
$user->setFirstname($input->getOption(AdminAccount::KEY_FIRST_NAME))
->setLastname($input->getOption(AdminAccount::KEY_LAST_NAME))
->setUsername($input->getOption(AdminAccount::KEY_USER))
->setRole($input->getOption('admin-role'))
->setEmail($input->getOption(AdminAccount::KEY_EMAIL))
->setPassword(
$input->getOption(AdminAccount::KEY_PASSWORD) === null
? '' : $input->getOption(AdminAccount::KEY_PASSWORD)
);
$validator = new MagentoFrameworkValidatorDataObject();
$this->validationRules->addUserInfoRules($validator);
$this->validationRules->addPasswordRules($validator);
if (!$validator->isValid($user)) {
$errors = array_merge($errors, $validator->getMessages());
}
return $errors;
}
}

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento admin:myuser:create

这是我在使用所有必需的输入执行命令时遇到的错误

我认为首先将用户创建为管理员会很容易。

  • Magento将在表authorization_role中插入资源表authorization_role
  • 您在类型从"G"更改为"U"之后
  • 从表authorization_rule中删除所有规则,因为默认情况下它将是所有规则授权规则
  • 使用已存在的角色添加新的authorization_rule

角色应在数据库上之前存在我认为使用这种机械会很容易

更新

问题来自构造函数中的类安装程序。默认情况下,Magento 2在"/设置";文件夹,因为环境不同。我可以建议的是从你的构造函数中删除这个类,并使用USERS的模型,你从这个类中需要的就是这个

function $ installer-> installAdminUser ($ input-> getOptions ());

您可以使用它,而无需使用adminAccountFactory调用安装程序类。

$installer = $this->installerFactory->create(new ConsoleLogger($output));

这个代码应该不是像吗

$this->installerFactory->create([
'the parameter name' => new ConsoleLogger($output)
]);

相关内容

  • 没有找到相关文章

最新更新