-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Assign user to role and roles list commands #28677
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
Open
Stepa4man
wants to merge
1
commit into
magento:2.4-develop
Choose a base branch
from
Stepa4man:assign-user-to-role-command
base: 2.4-develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
app/code/Magento/Authorization/Command/AdminRolesListCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Authorization\Command; | ||
|
||
use Magento\Authorization\Model\GetAdminRolesList; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Helper\TableFactory; | ||
|
||
/** | ||
* Class AdminRolesListCommand is cli command for showing all existing admin role ids and names | ||
*/ | ||
class AdminRolesListCommand extends Command | ||
{ | ||
/** | ||
* @var GetAdminRolesList | ||
*/ | ||
private $getAdminRolesListService; | ||
|
||
/** | ||
* @var TableFactory | ||
*/ | ||
private $tableFactory; | ||
|
||
/** | ||
* @param GetAdminRolesList $getAdminRolesListService | ||
* @param TableFactory $tableFactory | ||
* @param string|null $name | ||
*/ | ||
public function __construct( | ||
GetAdminRolesList $getAdminRolesListService, | ||
TableFactory $tableFactory, | ||
string $name = null | ||
) { | ||
parent::__construct($name); | ||
$this->getAdminRolesListService = $getAdminRolesListService; | ||
$this->tableFactory = $tableFactory; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this->setName("admin:role:list"); | ||
$this->setDescription("Shows all existing admin role ids and names"); | ||
parent::configure(); | ||
} | ||
|
||
/** | ||
* Provides table with admin roles | ||
* | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$output->writeln('Existing admin roles:'); | ||
$table = $this->tableFactory->create(['output' => $output]); | ||
$table->setHeaders(['Role ID', 'Role Name']); | ||
$table->setRows($this->getAdminRolesListService->execute()); | ||
$table->render(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
app/code/Magento/Authorization/Model/GetAdminRolesList.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Authorization\Model; | ||
|
||
use Magento\Authorization\Model\ResourceModel\Role\Grid\CollectionFactory; | ||
|
||
/** | ||
* Class GetAdminRolesList provides information about all existing group roles | ||
*/ | ||
class GetAdminRolesList | ||
{ | ||
/** | ||
* @var CollectionFactory | ||
*/ | ||
private $rolesGridCollectionFactory; | ||
|
||
/** | ||
* @param CollectionFactory $rolesGridCollectionFactory | ||
*/ | ||
public function __construct( | ||
CollectionFactory $rolesGridCollectionFactory | ||
) { | ||
$this->rolesGridCollectionFactory = $rolesGridCollectionFactory; | ||
} | ||
|
||
/** | ||
* Provides information about all existing group roles | ||
* | ||
* @return array | ||
*/ | ||
public function execute(): array | ||
{ | ||
return $this->rolesGridCollectionFactory->create() | ||
->addFieldToSelect('role_id') | ||
->addFieldToSelect('role_name') | ||
->load() | ||
->toArray()['items']; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...code/Magento/Authorization/Test/Integration/Console/Command/AdminRolesListCommandTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Authorization\Test\Integration\Console\Command; | ||
|
||
use Magento\Authorization\Command\AdminRolesListCommand; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
/** | ||
* Class AdminRolesListCommandTes | ||
*/ | ||
class AdminRolesListCommandTest extends TestCase | ||
{ | ||
/** | ||
* @magentoDataFixture ./../../../../app/code/Magento/Authorization/Test/Integration/_files/test_role.php | ||
lbajsarowicz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function testExecute(): void | ||
{ | ||
$objectManager = Bootstrap::getObjectManager(); | ||
$command = $objectManager->get(AdminRolesListCommand::class); | ||
$commandTester = new CommandTester($command); | ||
$commandTester->execute([]); | ||
$this->assertStringContainsString('Role ID ', $commandTester->getDisplay()); | ||
$this->assertStringContainsString('Role Name', $commandTester->getDisplay()); | ||
$this->assertStringContainsString('test_role', $commandTester->getDisplay()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/code/Magento/Authorization/Test/Integration/_files/test_role.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
use Magento\Authorization\Model\Acl\Role\Group as RoleGroup; | ||
use Magento\Authorization\Model\ResourceModel\Role as RoleResource; | ||
use Magento\Authorization\Model\RoleFactory; | ||
use Magento\Authorization\Model\RulesFactory; | ||
use Magento\Authorization\Model\UserContextInterface; | ||
use Magento\Backend\App\Area\FrontNameResolver; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
|
||
Bootstrap::getInstance()->loadArea(FrontNameResolver::AREA_CODE); | ||
$objectManager = Bootstrap::getObjectManager(); | ||
/** @var RoleResource $roleResource */ | ||
$roleResource = $objectManager->get(RoleResource::class); | ||
/** @var RoleFactory $roleFactory */ | ||
$roleFactory = $objectManager->get(RoleFactory::class); | ||
$role = $roleFactory->create(); | ||
$role->setName('test_role') | ||
->setPid(0) | ||
->setRoleType(RoleGroup::ROLE_TYPE) | ||
->setUserType(UserContextInterface::USER_TYPE_ADMIN); | ||
|
||
$roleResource->save($role); | ||
$ruleFactory = $objectManager->get(RulesFactory::class); | ||
$ruleFactory->create()->setRoleId($role->getId())->setResources(['Magento_Backend::all'])->saveRel(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
app/code/Magento/User/Console/AssignAdminToRoleCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\User\Console; | ||
|
||
use InvalidArgumentException; | ||
use Magento\Authorization\Model\GetAdminRolesList; | ||
use Magento\Framework\Exception\AlreadyExistsException; | ||
use Magento\User\Model\AssignUserToRole; | ||
use Magento\User\Model\GetAdminUserList; | ||
use Magento\User\Model\User; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\QuestionHelper; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\Question; | ||
|
||
/** | ||
* Class AssignAdminToRoleCommand assigns role to user | ||
*/ | ||
class AssignAdminToRoleCommand extends Command | ||
{ | ||
private const USER_NAME_OPTION = 'username'; | ||
private const ROLE_ID_OPTION = 'role-id'; | ||
|
||
/** | ||
* @var AssignUserToRole | ||
*/ | ||
private $assignUserToRoleService; | ||
|
||
/** | ||
* @var GetAdminRolesList | ||
*/ | ||
private $getAdminRolesListService; | ||
|
||
/** | ||
* @var GetAdminUserList | ||
*/ | ||
private $getAdminUserListService; | ||
|
||
/** | ||
* @param AssignUserToRole $assignUserToRoleService | ||
* @param GetAdminRolesList $getAdminRolesListService | ||
* @param GetAdminUserList $getAdminUserListService | ||
* @param string|null $name | ||
*/ | ||
public function __construct( | ||
AssignUserToRole $assignUserToRoleService, | ||
GetAdminRolesList $getAdminRolesListService, | ||
GetAdminUserList $getAdminUserListService, | ||
string $name = null | ||
) { | ||
parent::__construct($name); | ||
$this->assignUserToRoleService = $assignUserToRoleService; | ||
$this->getAdminRolesListService = $getAdminRolesListService; | ||
$this->getAdminUserListService = $getAdminUserListService; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this->setName("admin:user:assign-role"); | ||
$this->setDescription("Assigns user to role"); | ||
$this->setDefinition($this->getOptionsList()); | ||
|
||
parent::configure(); | ||
} | ||
|
||
/** | ||
* Assigns role to user | ||
* | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* | ||
* @return void | ||
* @throws AlreadyExistsException | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$this->assignUserToRoleService->execute( | ||
$input->getOption(self::USER_NAME_OPTION), | ||
(int)$input->getOption(self::ROLE_ID_OPTION) | ||
); | ||
$output->writeln("User assigned to the role."); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function interact(InputInterface $input, OutputInterface $output) | ||
{ | ||
/** @var QuestionHelper $questionHelper */ | ||
$questionHelper = $this->getHelper('question'); | ||
|
||
if (!$input->getOption(self::USER_NAME_OPTION)) { | ||
$question = new Question('<question>Admin username:</question> ', ''); | ||
$userList = $this->getAdminUserListService->execute(); | ||
$question->setValidator(function ($value) use ($userList) { | ||
/** @var User $user */ | ||
foreach ($userList as $user) { | ||
if ($user->getUserName() === $value) { | ||
return $value; | ||
} | ||
} | ||
throw new InvalidArgumentException(sprintf(AssignUserToRole::USER_DOES_NOT_EXISTS_ERROR_MSG, $value)); | ||
}); | ||
|
||
$input->setOption( | ||
self::USER_NAME_OPTION, | ||
$questionHelper->ask($input, $output, $question) | ||
); | ||
} | ||
|
||
if (!$input->getOption(self::ROLE_ID_OPTION)) { | ||
$question = new Question('<question>Role ID:</question> ', ''); | ||
$rolesList = $this->getAdminRolesListService->execute(); | ||
$question->setValidator(function ($value) use ($rolesList) { | ||
foreach ($rolesList as $role) { | ||
if ($role['role_id'] === $value) { | ||
return $value; | ||
} | ||
} | ||
throw new InvalidArgumentException(sprintf(AssignUserToRole::ROLE_DOES_NOT_EXISTS_ERROR_MSG, $value)); | ||
}); | ||
|
||
$input->setOption( | ||
self::ROLE_ID_OPTION, | ||
$questionHelper->ask($input, $output, $question) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Get list of arguments for the command | ||
* | ||
* @return InputOption[] | ||
*/ | ||
public function getOptionsList(): array | ||
{ | ||
return [ | ||
new InputOption( | ||
self::USER_NAME_OPTION, | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'(Required) Admin Username' | ||
), | ||
new InputOption( | ||
self::ROLE_ID_OPTION, | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'(Required) Role ID' | ||
) | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.