Skip to content

Commit

Permalink
Permissions configuration for sftp adapter (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
wiistriker authored Aug 8, 2023
1 parent 85d2928 commit f805e24
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions src/DependencyInjection/Factory/Adapter/SftpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Oneup\FlysystemBundle\DependencyInjection\Factory\Adapter;

use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use Oneup\FlysystemBundle\DependencyInjection\Factory\AdapterFactoryInterface;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\ChildDefinition;
Expand All @@ -28,6 +29,14 @@ public function create(ContainerBuilder $container, string $id, array $config):
$config['options']['connectivityChecker'] = new Reference($config['options']['connectivityChecker']);
}

$visibilityConverter = null;

if (isset($config['permissions'])) {
$visibilityConverter = new Definition(PortableVisibilityConverter::class);
$visibilityConverter->setFactory([PortableVisibilityConverter::class, 'fromArray']);
$visibilityConverter->setArgument(0, $config['permissions']);
}

$container
->setDefinition($id, new ChildDefinition('oneup_flysystem.adapter.sftp'))
->replaceArgument(0, (new Definition(SftpConnectionProvider::class))
Expand All @@ -36,13 +45,15 @@ public function create(ContainerBuilder $container, string $id, array $config):
->setShared(false)
)
->replaceArgument(1, $root)
->replaceArgument(2, $config['visibilityConverter'])
->replaceArgument(2, $visibilityConverter)
->replaceArgument(3, $config['mimeTypeDetector'])
;
}

public function addConfiguration(NodeDefinition $node): void
{
$parseOctal = \Closure::fromCallable([self::class, 'parseOctal']);

$node
->children()
->arrayNode('options')->isRequired()
Expand All @@ -61,9 +72,68 @@ public function addConfiguration(NodeDefinition $node): void
->scalarNode('root')->isRequired()->end()
->end()
->end()
->scalarNode('visibilityConverter')->defaultNull()->end()
->arrayNode('permissions')
->children()
->arrayNode('file')
->children()
->integerNode('public')
->beforeNormalization()
->ifString()
->then($parseOctal)
->end()
->defaultNull()
->end()
->integerNode('private')
->beforeNormalization()
->ifString()
->then($parseOctal)
->end()
->defaultNull()
->end()
->end()
->end()
->arrayNode('dir')
->children()
->integerNode('public')
->beforeNormalization()
->ifString()
->then($parseOctal)
->end()
->defaultNull()
->end()
->integerNode('private')
->beforeNormalization()
->ifString()
->then($parseOctal)
->end()
->defaultNull()
->end()
->end()
->end()
->end()
->end()
->scalarNode('mimeTypeDetector')->defaultNull()->end()
->end()
;
}

/**
* Backward compatibility (BC) between symfony/yaml <= 5.4 and >= 6.0.
*
* @see https://github.com/symfony/symfony/pull/34813
*/
private static function parseOctal(string $scalar): int
{
if (!preg_match('/^(?:\+|-)?0o?(?P<value>[0-7_]++)$/', $scalar, $matches)) {
throw new \InvalidArgumentException("The scalar \"$scalar\" is not a valid octal number.");
}

$value = str_replace('_', '', $matches['value']);

if ('-' === $scalar[0]) {
return (int) -octdec($value);
}

return (int) octdec($value);
}
}

0 comments on commit f805e24

Please sign in to comment.