Skip to content

Commit 9101912

Browse files
committed
2 command to work with admin users and its roles
1 parent 3a65cdc commit 9101912

File tree

11 files changed

+716
-3
lines changed

11 files changed

+716
-3
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Authorization\Command;
9+
10+
use Magento\Authorization\Model\GetAdminRolesList;
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
use Symfony\Component\Console\Helper\Table;
15+
use Symfony\Component\Console\Helper\TableFactory;
16+
17+
/**
18+
* Class AdminRolesListCommand is cli command for showing all existing admin role ids and names
19+
*/
20+
class AdminRolesListCommand extends Command
21+
{
22+
/**
23+
* @var GetAdminRolesList
24+
*/
25+
private $getAdminRolesListService;
26+
27+
/**
28+
* @var TableFactory
29+
*/
30+
private $tableFactory;
31+
32+
/**
33+
* @param GetAdminRolesList $getAdminRolesListService
34+
* @param TableFactory $tableFactory
35+
* @param string|null $name
36+
*/
37+
public function __construct(
38+
GetAdminRolesList $getAdminRolesListService,
39+
TableFactory $tableFactory,
40+
string $name = null
41+
) {
42+
parent::__construct($name);
43+
$this->getAdminRolesListService = $getAdminRolesListService;
44+
$this->tableFactory = $tableFactory;
45+
}
46+
47+
/**
48+
* @inheritDoc
49+
*/
50+
protected function configure(): void
51+
{
52+
$this->setName("admin:role:list");
53+
$this->setDescription("Shows all existing admin role ids and names");
54+
parent::configure();
55+
}
56+
57+
/**
58+
* Provides table with admin roles
59+
*
60+
* @param InputInterface $input
61+
* @param OutputInterface $output
62+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
63+
*/
64+
protected function execute(InputInterface $input, OutputInterface $output): void
65+
{
66+
$output->writeln('Existing admin roles:');
67+
$table = $this->tableFactory->create(['output' => $output]);
68+
$table->setHeaders(['Role ID', 'Role Name']);
69+
$table->setRows($this->getAdminRolesListService->execute());
70+
$table->render();
71+
}
72+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\Authorization\Model;
10+
11+
use Magento\Authorization\Model\ResourceModel\Role\Grid\CollectionFactory;
12+
13+
/**
14+
* Class GetAdminRolesList provides information about all existing group roles
15+
*/
16+
class GetAdminRolesList
17+
{
18+
/**
19+
* @var CollectionFactory
20+
*/
21+
private $rolesGridCollectionFactory;
22+
23+
/**
24+
* @param CollectionFactory $rolesGridCollectionFactory
25+
*/
26+
public function __construct(
27+
CollectionFactory $rolesGridCollectionFactory
28+
) {
29+
$this->rolesGridCollectionFactory = $rolesGridCollectionFactory;
30+
}
31+
32+
/**
33+
* Provides information about all existing group roles
34+
*
35+
* @return array
36+
*/
37+
public function execute(): array
38+
{
39+
return $this->rolesGridCollectionFactory->create()
40+
->addFieldToSelect('role_id')
41+
->addFieldToSelect('role_name')
42+
->load()
43+
->toArray()['items'];
44+
}
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Authorization\Test\Integration\Console\Command;
9+
10+
use Magento\Authorization\Command\AdminRolesListCommand;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Console\Tester\CommandTester;
14+
15+
/**
16+
* Class AdminRolesListCommandTes
17+
*/
18+
class AdminRolesListCommandTest extends TestCase
19+
{
20+
/**
21+
* @magentoDataFixture ./../../../../app/code/Magento/Authorization/Test/Integration/_files/test_role.php
22+
*/
23+
public function testExecute(): void
24+
{
25+
$objectManager = Bootstrap::getObjectManager();
26+
$command = $objectManager->get(AdminRolesListCommand::class);
27+
$commandTester = new CommandTester($command);
28+
$commandTester->execute([]);
29+
$this->assertStringContainsString('Role ID ', $commandTester->getDisplay());
30+
$this->assertStringContainsString('Role Name', $commandTester->getDisplay());
31+
$this->assertStringContainsString('test_role', $commandTester->getDisplay());
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Authorization\Model\Acl\Role\Group as RoleGroup;
9+
use Magento\Authorization\Model\ResourceModel\Role as RoleResource;
10+
use Magento\Authorization\Model\RoleFactory;
11+
use Magento\Authorization\Model\RulesFactory;
12+
use Magento\Authorization\Model\UserContextInterface;
13+
use Magento\Backend\App\Area\FrontNameResolver;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
16+
Bootstrap::getInstance()->loadArea(FrontNameResolver::AREA_CODE);
17+
$objectManager = Bootstrap::getObjectManager();
18+
/** @var RoleResource $roleResource */
19+
$roleResource = $objectManager->get(RoleResource::class);
20+
/** @var RoleFactory $roleFactory */
21+
$roleFactory = $objectManager->get(RoleFactory::class);
22+
$role = $roleFactory->create();
23+
$role->setName('test_role')
24+
->setPid(0)
25+
->setRoleType(RoleGroup::ROLE_TYPE)
26+
->setUserType(UserContextInterface::USER_TYPE_ADMIN);
27+
28+
$roleResource->save($role);
29+
$ruleFactory = $objectManager->get(RulesFactory::class);
30+
$ruleFactory->create()->setRoleId($role->getId())->setResources(['Magento_Backend::all'])->saveRel();

app/code/Magento/Authorization/etc/di.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
910
<type name="Magento\Authorization\Model\Role" shared="false" />
1011
<type name="Magento\Authorization\Model\ResourceModel\Rules">
1112
<arguments>
@@ -24,4 +25,11 @@
2425
</arguments>
2526
</type>
2627
<preference for="Magento\Authorization\Model\UserContextInterface" type="Magento\Authorization\Model\CompositeUserContext"/>
28+
<type name="Magento\Framework\Console\CommandListInterface">
29+
<arguments>
30+
<argument name="commands" xsi:type="array">
31+
<item name="adminRolesListCommand" xsi:type="object">Magento\Authorization\Command\AdminRolesListCommand</item>
32+
</argument>
33+
</arguments>
34+
</type>
2735
</config>
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\User\Console;
9+
10+
use InvalidArgumentException;
11+
use Magento\Authorization\Model\GetAdminRolesList;
12+
use Magento\Framework\Exception\AlreadyExistsException;
13+
use Magento\User\Model\AssignUserToRole;
14+
use Magento\User\Model\GetAdminUserList;
15+
use Magento\User\Model\User;
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Helper\QuestionHelper;
18+
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Console\Input\InputOption;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
use Symfony\Component\Console\Question\Question;
22+
23+
/**
24+
* Class AssignAdminToRoleCommand assigns role to user
25+
*/
26+
class AssignAdminToRoleCommand extends Command
27+
{
28+
private const USER_NAME_OPTION = 'username';
29+
private const ROLE_ID_OPTION = 'role-id';
30+
31+
/**
32+
* @var AssignUserToRole
33+
*/
34+
private $assignUserToRoleService;
35+
36+
/**
37+
* @var GetAdminRolesList
38+
*/
39+
private $getAdminRolesListService;
40+
41+
/**
42+
* @var GetAdminUserList
43+
*/
44+
private $getAdminUserListService;
45+
46+
/**
47+
* @param AssignUserToRole $assignUserToRoleService
48+
* @param GetAdminRolesList $getAdminRolesListService
49+
* @param GetAdminUserList $getAdminUserListService
50+
* @param string|null $name
51+
*/
52+
public function __construct(
53+
AssignUserToRole $assignUserToRoleService,
54+
GetAdminRolesList $getAdminRolesListService,
55+
GetAdminUserList $getAdminUserListService,
56+
string $name = null
57+
) {
58+
parent::__construct($name);
59+
$this->assignUserToRoleService = $assignUserToRoleService;
60+
$this->getAdminRolesListService = $getAdminRolesListService;
61+
$this->getAdminUserListService = $getAdminUserListService;
62+
}
63+
64+
/**
65+
* @inheritDoc
66+
*/
67+
protected function configure(): void
68+
{
69+
$this->setName("admin:user:assign-role");
70+
$this->setDescription("Assigns user to role");
71+
$this->setDefinition($this->getOptionsList());
72+
73+
parent::configure();
74+
}
75+
76+
/**
77+
* Assigns role to user
78+
*
79+
* @param InputInterface $input
80+
* @param OutputInterface $output
81+
*
82+
* @return void
83+
* @throws AlreadyExistsException
84+
*/
85+
protected function execute(InputInterface $input, OutputInterface $output): void
86+
{
87+
$this->assignUserToRoleService->execute(
88+
$input->getOption(self::USER_NAME_OPTION),
89+
(int)$input->getOption(self::ROLE_ID_OPTION)
90+
);
91+
$output->writeln("User assigned to the role.");
92+
}
93+
94+
/**
95+
* @inheritDoc
96+
*/
97+
protected function interact(InputInterface $input, OutputInterface $output)
98+
{
99+
/** @var QuestionHelper $questionHelper */
100+
$questionHelper = $this->getHelper('question');
101+
102+
if (!$input->getOption(self::USER_NAME_OPTION)) {
103+
$question = new Question('<question>Admin username:</question> ', '');
104+
$userList = $this->getAdminUserListService->execute();
105+
$question->setValidator(function ($value) use ($userList) {
106+
/** @var User $user */
107+
foreach ($userList as $user) {
108+
if ($user->getUserName() === $value) {
109+
return $value;
110+
}
111+
}
112+
throw new InvalidArgumentException(sprintf(AssignUserToRole::USER_DOES_NOT_EXISTS_ERROR_MSG, $value));
113+
});
114+
115+
$input->setOption(
116+
self::USER_NAME_OPTION,
117+
$questionHelper->ask($input, $output, $question)
118+
);
119+
}
120+
121+
if (!$input->getOption(self::ROLE_ID_OPTION)) {
122+
$question = new Question('<question>Role ID:</question> ', '');
123+
$rolesList = $this->getAdminRolesListService->execute();
124+
$question->setValidator(function ($value) use ($rolesList) {
125+
foreach ($rolesList as $role) {
126+
if ($role['role_id'] === $value) {
127+
return $value;
128+
}
129+
}
130+
throw new InvalidArgumentException(sprintf(AssignUserToRole::ROLE_DOES_NOT_EXISTS_ERROR_MSG, $value));
131+
});
132+
133+
$input->setOption(
134+
self::ROLE_ID_OPTION,
135+
$questionHelper->ask($input, $output, $question)
136+
);
137+
}
138+
}
139+
140+
/**
141+
* Get list of arguments for the command
142+
*
143+
* @return InputOption[]
144+
*/
145+
public function getOptionsList(): array
146+
{
147+
return [
148+
new InputOption(
149+
self::USER_NAME_OPTION,
150+
null,
151+
InputOption::VALUE_REQUIRED,
152+
'(Required) Admin Username'
153+
),
154+
new InputOption(
155+
self::ROLE_ID_OPTION,
156+
null,
157+
InputOption::VALUE_REQUIRED,
158+
'(Required) Role ID'
159+
)
160+
];
161+
}
162+
}

0 commit comments

Comments
 (0)