kaliop-uk / ezmigrationbundle

This bundle makes it easy to handle eZPlatform / eZPublish5 content upgrades/migrations
GNU General Public License v2.0
53 stars 81 forks source link

User does not have access to 'translations' 'content' #165

Closed ajalovec closed 6 years ago

ajalovec commented 6 years ago

After updating eZ from 2.1 to 2.2 we are getting the following error on migration execution: On eZ 2.1 we had version 4.7.0 installed and then I updated eZ to 2.2 and got this error. I then tried to update migrations bundle to 5.2.0 but got same error.

In MigrationService.php line 419:

  Error in execution of step 1: User does not have access to 'translations' 'content' in file /data/apps/dev/vendor/ezsystems/ezpublish-kernel/eZ
  /Publish/Core/Repository/LanguageService.php line 87

We have our own command which collects all yml files and creates definitions which could potentially cause the problem but I think the issue is somewhere else.

Our custom code:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $this->io         = new SymfonyStyle($input, $output);
    $migrationService = $this->getMigrationsService();

    $installerPath  = $input->getOption('path');
    if (!file_exists($installerPath) || !is_dir($installerPath)) {
        throw new \LogicException(sprintf('Directory `%s` for migrations does not exist or is not directory!', $installerPath));
    }

    if ($input->getOption('seed')) {
        $installerPath .= '/' . self::SEED_DIR_NAME;
        if (!file_exists($installerPath) || !is_dir($installerPath)) {
            throw new \LogicException(sprintf('Directory `%s` does not exist or is not directory! Either remove option --seed or create directory.', $installerPath));
        }
    }

    $definitions = $this->getMigrationDefinitions($installerPath);
    if ($input->getOption('force')) {
        foreach ($definitions as $definition) {
            $migrationService->executeMigration($definition, true, $input->getOption('lang'));
        }
    }
}

protected function getMigrationDefinitions($path)
{
    $path             = realpath($path);
    $migrationService = $this->getMigrationsService();
    $definitions      = [];
    $data             = [];
    $i                = 0;

    /** @var SplFileInfo $file */
    foreach ($this->findFiles($path) as $file) {
        if ($file->isFile()) {
            $name                = sprintf('%s_%s', $file->getRelativePath(), $file->getFilename());
            $migrationDefinition = new MigrationDefinition(
                $name,
                $file->getRealPath(),
                $file->getContents()
            );
            $migrationDefinition = $migrationService->parseMigrationDefinition($migrationDefinition);

            if ($migrationDefinition->status != MigrationDefinition::STATUS_PARSED) {
                $notes = '<error>' . $migrationDefinition->parsingError . '</error>';
            } else {
                $migration = $this->getMigration($migrationDefinition->name);
                if (is_null($migration) || $migration->status === Migration::STATUS_TODO) {
                    $definitions[] = $migrationDefinition;
                    $notes              = 'Ok';
                } else {
                    switch ($migration->status) {
                        case Migration::STATUS_FAILED:
                            $notes              = '<error>Failed</error>';
                            $definitions[] = $migrationDefinition;
                            break;
                        case Migration::STATUS_PARTIALLY_DONE:
                            $notes = '<error>Partially done</error>';
                            break;
                        case Migration::STATUS_SKIPPED:
                            $notes = '<comment>Skipped</comment>';
                            break;
                        case Migration::STATUS_STARTED:
                            $notes = '<error>Started</error>';
                            break;
                        default:
                            $notes = 'Executed';
                    }
                }
            }

            if ($file->getRelativePath() === self::SEED_DIR_NAME) {
                $name = $file->getFilename() . ' <comment>(seed)</comment>';
            } else {
                $name = sprintf('%s (%s)', $file->getFilename(), $file->getRelativePath());
            }
            $data[] = array(
                $i++,
                $name,
                $notes,
            );
        }
    }

    $this->io->title($path);
    $this->io->table([ '#', 'Files', 'Notes' ], $data);

    return $definitions;
}

protected function getMigrationsService()
{
    return $this->getContainer()->get('ez_migration_bundle.migration_service');
}

protected function getMigration($name)
{
    if (is_null($this->migrationsCollection)) {
        $this->migrationsCollection = $this->getMigrationsService()->getMigrations();
    }

    return isset($this->migrationsCollection[$name]) ? $this->migrationsCollection[$name] : null;
}

private function findFiles($path)
{
    $files = Finder::create()
        ->in($path)
        ->name('/^[0-9]{3}_[A-z_0-9\-]+\.(yml|php)/')
        ->files()
        ->sort(function (SplFileInfo $f1, SplFileInfo $f2) {
            // modify file name so we add relative path between sorting numbers and name of the file so we get same sorting as directories are sorted
            // Example: _seed/010_my_migration.yml > 010__seed_my_migration.yml
            // Example: demo/013_my_migration.yml > 013_demo_my_migration.yml
            $f1Name = sprintf('%s_%s_%s', substr($f1->getFilename(), 0, 3), $f1->getRelativePath(), substr($f1->getFilename(), 4));
            $f2Name = sprintf('%s_%s_%s', substr($f2->getFilename(), 0, 3), $f2->getRelativePath(), substr($f2->getFilename(), 4));

            return strcmp($f1Name, $f2Name);
        })
    ;

    return $files;
}

I also tried to run our command with option --siteaccess=admin but I get same error.

fabian-markaban commented 6 years ago

We are experiencing a similar issue while running the following simple migration:

-
    mode: create
    type: content
    content_type: folder
    parent_location: 2
    attributes:
        name: iol

We get the following error:

Migration aborted! Reason: Error in execution of step 1: User does not have access to 'create' 'content' with: parentLocationId '2', sectionId '1'' in file /var/www/vendor/ezsystems/ezpublish-kernel/eZ/Publish/Core/Repository/ContentService.php line 492

We are on the v2.1.1 tag of the ezplatform-ee and using kaliop/ezmigrationbundle 5.2.0

gggeek commented 6 years ago

This test failure is also new, and seems to relate to a fresh problem with permissions and the latest version of eZPlatform: https://travis-ci.org/kaliop-uk/ezmigrationbundle/jobs/403934482

crevillo commented 6 years ago

interesting. why it fails only for php 7.2 job?

gggeek commented 6 years ago

Because of test matrix, it is the only version of php used with the latest ezplatform

crevillo commented 6 years ago

i'm doing some tests locally. it looks to me that when phpunit is executed, at some point it reachs this line in the ez kernel. https://github.com/ezsystems/ezpublish-kernel/blob/master/eZ/Publish/Core/Repository/Permission/PermissionResolver.php#L113

and that returns that we're executing it as anon users, not as the admin user... makes sense?

gggeek commented 6 years ago

Ok, so the last PR from @crevillo ( https://github.com/kaliop-uk/ezmigrationbundle/pull/168 ) seems to have identified what could be the root cause of this: SF 3.4.12 breaks lazy services, making them unshared. Bug report: https://github.com/symfony/symfony/issues/27756

@ajalovec @fabian-markaban would you be able to check the version of Sf that you use, and if it is 3.4.12 test if downgrading to 3.4.11 fixes the problem ?

ajalovec commented 6 years ago

@gggeek I can confirm that we are on Symfony 3.4.12 but can test it with 3.4.11 only at the end of the week when I am back from my vacation.

fabian-markaban commented 6 years ago

@gggeek I can verify that SF 3.4.12 is causing this and switching back to SF 3.4.11 resolves the problem. Thanks!

gggeek commented 6 years ago

Closing. Incompatibility with sf 3.4.12 has been declared in mig bundle 5.3