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

[PermissionSetup] add category to Permission #1101

Merged
merged 3 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace CoreShop\Bundle\CoreBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Pimcore\Migrations\Migration\AbstractPimcoreMigration;

class Version20190913065635 extends AbstractPimcoreMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
if (!$schema->hasTable('users_permission_definitions')) {
return;
}

$table = $schema->getTable('users_permission_definitions');

if ($table->hasColumn('category')) {
$this->addSql('UPDATE users_permission_definitions SET category=\'coreshop_permission_group_coreshop\' WHERE `key` LIKE \'coreshop_%\'');
}
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ coreshop_permission_order_detail: 'CoreShop: Order Detail'
coreshop_permission_quote_create: 'CoreShop: Quote Create'
coreshop_permission_quote_list: 'CoreShop: Quote List'
coreshop_permission_quote_detail: 'CoreShop: Quote Detail'
coreshop_permission_cart_create: 'CoreShop: Cart Create'
coreshop_permission_cart_list: 'CoreShop: Cart List'
coreshop_condition_nested: 'Nested Rules'
coreshop_condition_categories: 'Categories'
coreshop_condition_products: 'Products'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ coreshop_condition_weight_minWeight: 'Min Weight'
coreshop_condition_weight_maxWeight: 'Max Weight'
coreshop_priority: 'Priority'
coreshop_permission_product_price_rule: 'CoreShop: Product Price Rule'
coreshop_permission_product_unit: 'CoreShop: Product Unit'
coreshop_condition_nested: 'Nested Rules'
coreshop_condition_products: 'Products'
coreshop_condition_timespan: 'Timespan'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,14 @@ protected function registerPimcoreResources($applicationName, $bundleResources,
$applicationParameter = sprintf('%s.permissions', $applicationName);
$resourcePermissions = [];
$globalParameter = 'coreshop.all.permissions';
$globalPermissions = [];

if ($container->hasParameter($applicationParameter)) {
$applicationPermissions = $container->getParameter($applicationParameter);
}

if ($container->hasParameter($globalParameter)) {
$resourcePermissions = $container->getParameter($globalParameter);
$globalPermissions = $container->getParameter($globalParameter);
}

$permissions = [];
Expand All @@ -159,7 +160,11 @@ protected function registerPimcoreResources($applicationName, $bundleResources,
$resourcePermissions[] = $identifier;
}

$container->setParameter($globalParameter, array_merge($applicationPermissions, $permissions));
$globalApplicationPermissions = array_key_exists($applicationName, $globalPermissions) ? $globalPermissions[$applicationName] : [];
$globalApplicationPermissions = array_merge($globalApplicationPermissions, $resourcePermissions);
$globalPermissions[$applicationName] = $globalApplicationPermissions;

$container->setParameter($globalParameter, $globalPermissions);
$container->setParameter($applicationParameter, array_merge($applicationPermissions, $permissions));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace CoreShop\Bundle\ResourceBundle\Installer;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Column;
use Pimcore\Model\User\Permission;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -24,12 +26,19 @@ final class PimcorePermissionInstaller implements ResourceInstallerInterface
*/
private $kernel;

/**<
/**
* @var Connection
*/
private $connection;

/**
* @param KernelInterface $kernel
* @param Connection $connection
*/
public function __construct(KernelInterface $kernel)
public function __construct(KernelInterface $kernel, Connection $connection)
{
$this->kernel = $kernel;
$this->connection = $connection;
}

/**
Expand All @@ -40,27 +49,47 @@ public function installResources(OutputInterface $output, $applicationName = nul
$parameter = $applicationName ? sprintf('%s.permissions', $applicationName) : 'coreshop.all.permissions';

if ($this->kernel->getContainer()->hasParameter($parameter)) {
$permissions = $this->kernel->getContainer()->getParameter($parameter);
$permissionGroups = $this->kernel->getContainer()->getParameter($parameter);

if ($parameter !== 'coreshop.all.permissions') {
$permissionGroups = [
$applicationName => $permissionGroups
];
}

$progress = new ProgressBar($output);
$progress->setBarCharacter('<info>░</info>');
$progress->setEmptyBarCharacter(' ');
$progress->setProgressCharacter('<comment>░</comment>');
$progress->setFormat(' %current%/%max% [%bar%] %percent:3s%% %message%');
$progress->start(count($permissions));
$progress->start(count($permissionGroups, COUNT_RECURSIVE));

foreach ($permissions as $permission) {
$progress->setMessage(sprintf('<error>Install Permission %s</error>', $permission));
$columns = array_map(function(Column $column) {
return $column->getName();
}, $this->connection->getSchemaManager()->listTableColumns('users_permission_definitions'));

$permissionDefinition = Permission\Definition::getByKey($permission);
foreach ($permissionGroups as $group => $permissions) {
foreach ($permissions as $permission) {
$progress->setMessage(sprintf('<error>Install Permission %s</error>', $permission));

if (!$permissionDefinition instanceof Permission\Definition) {
$permissionDefinition = new Permission\Definition();
$permissionDefinition->setKey($permission);
$permissionDefinition->save();
}
$permissionDefinition = Permission\Definition::getByKey($permission);

$progress->advance();
if (!$permissionDefinition instanceof Permission\Definition) {
if (in_array('category', $columns, true)) {
$this->connection->insert('users_permission_definitions', [
'key' => $permission,
'category' => sprintf('coreshop_permission_group_%s', $group)
]);
}
else {
$this->connection->insert('users_permission_definitions', [
'key' => $permission
]);
}
}

$progress->advance();
}
}

$progress->finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ services:
CoreShop\Bundle\ResourceBundle\Installer\PimcorePermissionInstaller:
arguments:
- '@kernel'
- '@doctrine.dbal.default_connection'
tags:
- { name: coreshop.resource.installer, type: permissions, priority: 350}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ coreshop_save_success: 'Item has been successfully saved'
coreshop_enter_the_name: 'Enter the name'
coreshop_save_error: 'Error saving item'
coreshop_multistore: 'Multistore'
coreshop_total_items: 'Total {0}'
coreshop_total_items: 'Total {0}'
coreshop_permission_group_coreshop: 'CoreShop'