-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fetra Harinjatovo
committed
Nov 10, 2023
0 parents
commit 659b288
Showing
11 changed files
with
3,396 additions
and
0 deletions.
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,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the TounafLdapBundle package. | ||
* | ||
* (c) Fetraharinjatovo Nambinina <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Tounaf\Ldap\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class LdapServicePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('Symfony\Component\Ldap\Adapter\ExtLdap\Adapter')) { | ||
$definition = new Definition('Symfony\Component\Ldap\Adapter\ExtLdap\Adapter'); | ||
$arguments = [ | ||
[ | ||
'host' => 'localhost', | ||
'port' => 389, | ||
'options' => [ | ||
'protocol_version' => 3, | ||
'referrals' => true, | ||
], | ||
], | ||
]; | ||
$definition->setArguments($arguments); | ||
// Register the service definition | ||
$container->setDefinition('Symfony\Component\Ldap\Adapter\ExtLdap\Adapter', $definition); | ||
} | ||
|
||
if (!$container->hasDefinition('Symfony\Component\Ldap\Ldap')) { | ||
// Define the service | ||
$ldapDefinition = new Definition('Symfony\Component\Ldap\Ldap'); | ||
$ldapDefinition->setArguments([new Reference('Symfony\Component\Ldap\Adapter\ExtLdap\Adapter')]); | ||
$ldapDefinition->addTag('ldap'); | ||
$container->setDefinition('Symfony\Component\Ldap\Ldap', $ldapDefinition); | ||
} | ||
|
||
} | ||
} |
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,148 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the TounafLdapBundle package. | ||
* | ||
* (c) Fetraharinjatovo Nambinina <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Tounaf\Ldap\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
class Configuration implements ConfigurationInterface | ||
{ | ||
public function __construct(private array $factories) | ||
{ | ||
|
||
} | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder("tounaf_ldap"); | ||
$rootNode = $treeBuilder->getRootNode(); | ||
$this->addConnectionLdapSection($rootNode); | ||
$this->addProviderLdapSection($rootNode); | ||
$this->addFormLoginLdapSection($rootNode); | ||
|
||
|
||
return $treeBuilder; | ||
} | ||
|
||
private function addConnectionLdapSection(ArrayNodeDefinition $rootNode) | ||
{ | ||
$rootNode | ||
->children() | ||
->arrayNode('connection') | ||
->children() | ||
->scalarNode('host') | ||
->isRequired() | ||
->info("Ldap host") | ||
->end() | ||
->scalarNode("port") | ||
->isRequired() | ||
->defaultValue('389') | ||
->info('The port used by ldap') | ||
->end() | ||
->scalarNode("encryption") | ||
->defaultValue('none') | ||
->info('the encryption method: none/tls/ssl') | ||
->end() | ||
->arrayNode('options') | ||
->children() | ||
->scalarNode("protocol_version") | ||
->defaultValue("3") | ||
->info("Verstion of protocole") | ||
->end() | ||
->booleanNode("referrals") | ||
->defaultValue(false) | ||
->info("Verstion of protocole") | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
|
||
private function addProviderLdapSection(ArrayNodeDefinition $rootNode) | ||
{ | ||
$rootNode | ||
->children() | ||
->arrayNode("providers") | ||
->useAttributeAsKey('name') | ||
->arrayPrototype() | ||
->children() | ||
->arrayNode('ldap') | ||
->children() | ||
->scalarNode("service") | ||
->defaultValue("Symfony\Component\Ldap\Ldap") | ||
->end() | ||
->scalarNode("base_dn") | ||
->isRequired() | ||
->example("OU=Utilisateurs,DC=makeitpulse,DC=in,DC=axian-group,DC=com") | ||
->end() | ||
->scalarNode("search_dn") | ||
->isRequired() | ||
->example("CN=ogc-ass,OU=Service accounts,OU=Administrations,DC=ass,DC=in,DC=axian-group,DC=com") | ||
->end() | ||
->scalarNode("search_password") | ||
->defaultNull() | ||
->isRequired() | ||
->example("your_favorit_password") | ||
->end() | ||
->scalarNode("default_roles") | ||
->defaultValue("ROLE_USER") | ||
->isRequired() | ||
->example("ROLE_USER") | ||
->end() | ||
->scalarNode("uid_key") | ||
->defaultValue("sAMAccountName") | ||
->example("sAMAccountName") | ||
->end() | ||
->arrayNode('extra_fields') | ||
->scalarPrototype()->example(['mail','name','username'])->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
|
||
private function addFormLoginLdapSection(ArrayNodeDefinition $rootNode) | ||
{ | ||
$rootNode | ||
->children() | ||
->arrayNode('form_login_ldap') | ||
->children() | ||
->scalarNode('dn_string') | ||
->isRequired() | ||
->defaultValue('{username}') | ||
->example('DOMAINE\{username}') | ||
->info('ds_string peut est de type: DOMAIN\{username} ou simplement {username}') | ||
->end() | ||
->scalarNode('login_path') | ||
->defaultValue('app_login') | ||
->info('route to login user') | ||
->end() | ||
->scalarNode('check_path') | ||
->defaultValue('app_login') | ||
->info('route to process login') | ||
->end() | ||
->scalarNode('default_target_path') | ||
->info('route to redirect user when login success') | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
DependencyInjection/Factory/ConfiguratorFactoryInterface.php
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,10 @@ | ||
<?php | ||
|
||
namespace Tounaf\Ldap\DependencyInjection\Factory; | ||
|
||
use Symfony\Component\Config\Definition\Builder\NodeDefinition; | ||
|
||
interface ConfiguratorFactoryInterface | ||
{ | ||
public function addDefinition(NodeDefinition $builder); | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace Tounaf\Ldap\DependencyInjection\Factory; | ||
|
||
use Symfony\Component\Config\Definition\Builder\NodeDefinition; | ||
|
||
class SecurityDefinitionFactory implements ConfiguratorFactoryInterface | ||
{ | ||
|
||
public function addDefinition(NodeDefinition $builder) | ||
{ | ||
|
||
} | ||
} |
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,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the TounafLdapBundle package. | ||
* | ||
* (c) Fetraharinjatovo Nambinina <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Tounaf\Ldap\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
use Symfony\Component\Config\Definition\Processor; | ||
use Symfony\Component\Config\FileLocator; | ||
use Tounaf\Ldap\DependencyInjection\Configuration; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; | ||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Tounaf\Ldap\DependencyInjection\Factory\ConfiguratorFactoryInterface; | ||
|
||
class TounafLdapExtension extends Extension implements PrependExtensionInterface | ||
{ | ||
private array $factories = []; | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
$loader->load('services.yaml'); | ||
$this->getSecurityExtensionConfig($container); | ||
$configuration = $this->getConfiguration($configs, $container); | ||
$config = $this->processConfiguration($configuration, $configs); | ||
if($container->hasDefinition('Symfony\Component\Ldap\Adapter\ExtLdap\Adapter')) { | ||
$container->getDefinition('Symfony\Component\Ldap\Adapter\ExtLdap\Adapter')->setArguments([$config['connection']]); | ||
} | ||
} | ||
|
||
public function getConfiguration(array $configs, ContainerBuilder $container): ?ConfigurationInterface | ||
{ | ||
return new Configuration($this->factories); | ||
} | ||
|
||
public function prepend(ContainerBuilder $container) | ||
{ | ||
$ldapConf = $container->getExtensionConfig('tounaf_ldap'); | ||
if($container->hasExtension('security')) { | ||
$providerConfig = array( | ||
'providers' => $ldapConf[0]['providers'] | ||
); | ||
|
||
$container->prependExtensionConfig('security', $providerConfig); | ||
$ldapProviderName = array_keys($ldapConf[0]['providers']); | ||
$container->loadFromExtension('security', [ | ||
'firewalls' => [ | ||
'main' => [ | ||
'provider' => array_shift($ldapProviderName), | ||
'form_login_ldap' => array_merge(["service" => "Symfony\Component\Ldap\Ldap"], $ldapConf[0]['form_login_ldap']) | ||
] | ||
] | ||
]); | ||
} | ||
|
||
|
||
} | ||
|
||
public function addConfigurator(ConfiguratorFactoryInterface $configurator) | ||
{ | ||
$this->factories[] = $configurator; | ||
} | ||
|
||
private function getSecurityExtensionConfig(ContainerBuilder $container) | ||
{ | ||
if($container->hasExtension('security')) { | ||
$extension = $container->getExtension('security'); | ||
$configs = $container->getExtensionConfig('security'); | ||
$extension = $container->getExtension('security'); | ||
$configuration = $extension->getConfiguration($configs, $container); | ||
return (new Processor())->processConfiguration($configuration, $configs); | ||
} | ||
|
||
} | ||
} |
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,19 @@ | ||
Copyright (c) 2011-2021 TounafLdapBundle <http://tounaf.github.com/> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Oops, something went wrong.