-
Notifications
You must be signed in to change notification settings - Fork 27
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
Feature: add Shopmon ACL role #251
Closed
MelvinAchterhuis
wants to merge
3
commits into
FriendsOfShopware:main
from
MelvinAchterhuis:feature/add-shopmon-acl-role
Closed
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
This file contains 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,168 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Frosh\Tools\Command; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Shopware\Core\Framework\Api\Acl\Role\AclRoleCollection; | ||
use Shopware\Core\Framework\Context; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteException; | ||
use Shopware\Core\Framework\Uuid\Uuid; | ||
use Shopware\Core\Framework\Validation\WriteConstraintViolationException; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
#[AsCommand('frosh:shopmon:create-acl-role', 'Create Shopmon ACL role')] | ||
class ShopmonCreateAclRole extends Command | ||
{ | ||
private const SHOPMON_ACL_ROLE_NAME = 'shopmon_acl_role'; | ||
private const SHOPMON_ACL_ROLE_DESCRIPTION = 'This role has the necessary permissions to use Shopmon'; | ||
private const SHOPMON_ACL_ROLE_ID = '018dd6ae4c4072b1b5887fe8d3b9b95a'; | ||
|
||
/** | ||
* @param EntityRepository<AclRoleCollection> $aclRoleRepository | ||
* @param EntityRepository<EntityCollection> $userRepository | ||
* @param Connection $connection | ||
*/ | ||
public function __construct( | ||
Check failure on line 37 in src/Command/ShopmonCreateAclRole.php GitHub Actions / phpstan / Static Analyse
|
||
private readonly EntityRepository $aclRoleRepository, | ||
private readonly EntityRepository $userRepository, | ||
private readonly Connection $connection | ||
) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->addOption('username', null, InputOption::VALUE_OPTIONAL, 'User that will be assigned the role'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
if ($this->roleExists()) { | ||
$io->warning(sprintf('ACL role with the name `%s` already exists', self::SHOPMON_ACL_ROLE_NAME)); | ||
return Command::SUCCESS; | ||
} | ||
|
||
$userId = null; | ||
if ($input->getOption('username') !== null) { | ||
$userId = $this->userExists($input->getOption('username')); | ||
if (!$userId) { | ||
$io->error(sprintf('User with the username `%s` does not exist', $input->getOption('username'))); | ||
return Command::FAILURE; | ||
} | ||
} | ||
|
||
try { | ||
$this->createRole(); | ||
} catch (WriteException $exception) { | ||
$io->error('Something went wrong.'); | ||
$messages = $this->createWriteExceptionMessages($exception); | ||
$io->listing($messages); | ||
return Command::FAILURE; | ||
} | ||
$io->success(sprintf('ACL role with the name `%s` has been created', self::SHOPMON_ACL_ROLE_NAME)); | ||
|
||
if ($userId === null) { | ||
return Command::SUCCESS; | ||
} | ||
|
||
try { | ||
$this->assignRoleToUser($userId); | ||
} catch (\Exception $e) { | ||
$io->error($e->getMessage()); | ||
return Command::FAILURE; | ||
} | ||
$io->success(sprintf('ACL role been assigned to the user with the username `%s`', $input->getOption('username'))); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
private function roleExists(): bool | ||
{ | ||
$context = Context::createDefaultContext(); | ||
$criteria = new Criteria(); | ||
$criteria->addFilter(new EqualsFilter('name', self::SHOPMON_ACL_ROLE_NAME)); | ||
$criteria->setLimit(1); | ||
|
||
return $this->aclRoleRepository->search($criteria, $context)->getTotal() > 0; | ||
} | ||
|
||
private function userExists(string $username): ?string | ||
{ | ||
$context = Context::createDefaultContext(); | ||
$criteria = new Criteria(); | ||
$criteria->addFilter(new EqualsFilter('username', $username)); | ||
$criteria->setLimit(1); | ||
|
||
return $this->userRepository->searchIds($criteria, $context)->firstId(); | ||
} | ||
|
||
private function createRole(): void | ||
{ | ||
$aclPermissions = [ | ||
'app:read', | ||
'product:read', | ||
'product:write', | ||
'system_config:read', | ||
'scheduled_task:read', | ||
'frosh_tools:read', | ||
'system:clear:cache', | ||
'system:cache:info' | ||
]; | ||
|
||
$aclRole = [ | ||
'id' => self::SHOPMON_ACL_ROLE_ID, | ||
'name' => self::SHOPMON_ACL_ROLE_NAME, | ||
'description' => self::SHOPMON_ACL_ROLE_DESCRIPTION, | ||
'privileges' => $aclPermissions, | ||
]; | ||
|
||
$context = Context::createDefaultContext(); | ||
$this->aclRoleRepository->create([$aclRole], $context); | ||
} | ||
|
||
private function assignRoleToUser(string $userId): void | ||
{ | ||
$sql = <<<'SQL' | ||
INSERT INTO `acl_user_role` (`user_id`, `acl_role_id`, `created_at`) | ||
VALUES (:user_id, :acl_role_id, :created_at) | ||
SQL; | ||
|
||
$data = [ | ||
'user_id' => Uuid::fromHexToBytes($userId), | ||
'acl_role_id' => Uuid::fromHexToBytes(self::SHOPMON_ACL_ROLE_ID), | ||
'created_at' => (new \DateTime())->format('Y-m-d H:i:s') | ||
]; | ||
|
||
$query = new RetryableQuery($this->connection, $this->connection->prepare($sql)); | ||
$query->execute($data); | ||
} | ||
|
||
private function createWriteExceptionMessages(WriteException $exception): array | ||
{ | ||
$messages = []; | ||
foreach ($exception->getExceptions() as $err) { | ||
if ($err instanceof WriteConstraintViolationException) { | ||
foreach ($err->getViolations() as $violation) { | ||
$messages[] = $violation->getPropertyPath() . ': ' . $violation->getMessage(); | ||
} | ||
} | ||
} | ||
|
||
return $messages; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why a user, we should only create a integration? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
user:create command in core doesn't support ACL/roles
so in a CI/CD setup first create a shopmon user with user:create and create+assign the role with this command