Author: Chad Sikorra (Chad.Sikorra@gmail.com)
Inheritance: use trait ConfigurationParseTrait
 function it_should_use_a_custom_format_definition()
 {
     $config = new DomainConfiguration('example.local');
     $config->setBindFormat('%username%');
     $this->beConstructedThrough('getInstance', [$config]);
     $this->getUsername('foo')->shouldBeEqualTo('foo');
 }
 function it_should_have_a_page_size_as_specified_from_the_config()
 {
     $config = new DomainConfiguration('example.com');
     $config->setServers(['test'])->setBaseDn('dc=example,dc=local')->setLazyBind(true)->setPageSize(250);
     $this->beConstructedWith($config);
     $this->getConfig()->getPageSize()->shouldBeEqualTo(250);
 }
 function let()
 {
     $config = new DomainConfiguration('foo.bar');
     $config->setLazyBind(true);
     $connection = new LdapConnection($config);
     $operation = new AddOperation('dc=foo,dc=bar');
     $this->beConstructedWith('foo', $operation, $connection);
 }
 function it_should_use_a_custom_format_definition()
 {
     $config = new DomainConfiguration('example.local');
     $config->setLdapType(LdapConnection::TYPE_OPENLDAP);
     $config->setBindFormat('CN=%username%,DC=foo,DC=bar');
     $this->beConstructedThrough('getInstance', [$config]);
     $this->getUsername('foo')->shouldBeEqualTo('CN=foo,DC=foo,DC=bar');
 }
 function it_should_throw_a_SchemaParserException_when_the_schema_file_is_not_readable()
 {
     $fakePath = '/this/path/should/never/really/exist/I/would/hope';
     $this->beConstructedWith($fakePath);
     $domain = new DomainConfiguration('example.com');
     $domain->setLdapType('ad');
     $this->shouldThrow(new SchemaParserException('Cannot find schema for "ad" in "' . $fakePath . '"'))->duringParse($domain->getLdapType(), 'user');
 }
 function let(LdapConnectionInterface $connection, LdapObject $rootdse)
 {
     $domain = new DomainConfiguration('example.local');
     $domain->setUseTls(true);
     $connection->getConfig()->willReturn($domain);
     $connection->getRootDse()->willReturn($rootdse);
     $config = new Configuration();
     $this->parser = new SchemaYamlParser($config->getSchemaFolder());
     $this->schema = $this->parser->parse('ad', 'user');
 }
 function let(\LdapTools\Connection\LdapConnectionInterface $connection)
 {
     $config = new DomainConfiguration('foo.bar');
     $config->setBaseDn('dc=foo,dc=bar');
     $connection->getConfig()->willReturn($config);
     $options = ['uacMap' => ['disabled' => '2', 'enabled' => '2', 'passwordNeverExpires' => '65536', 'smartCardRequired' => '262144', 'trustedForAllDelegation' => '262144', 'trustedForAnyAuthDelegation' => '16777216', 'passwordIsReversible' => '128'], 'defaultValue' => '512', 'invert' => ['enabled']];
     $this->setOptions($options);
     $this->setLdapConnection($connection);
     $this->setDn('cn=foo,dc=foo,dc=bar');
     $this->expectedOp = function ($operation) {
         return $operation->getFilter() == '(&(objectClass=*))' && $operation->getAttributes() == ['userAccountControl'] && $operation->getBaseDn() == 'cn=foo,dc=foo,dc=bar';
     };
 }
 function it_should_throw_an_exception_if_ssl_or_tls_is_not_enabled(\LdapTools\Connection\LdapConnectionInterface $connection)
 {
     $this->toLdap('test')->shouldNotThrow('\\LdapTools\\Exception\\LdapConnectionException');
     $config = new DomainConfiguration('example.local');
     $config->setUseTls(true);
     $connection->getConfig()->willReturn($config);
     $this->setLdapConnection($connection);
     $this->toLdap('test')->shouldNotThrow('\\LdapTools\\Exception\\LdapConnectionException');
     $config->setUseTls(false);
     $this->shouldThrow('\\LdapTools\\Exception\\LdapConnectionException')->duringToLdap('test');
     $config->setUseSsl(true);
     $this->toLdap('test')->shouldNotThrow('\\LdapTools\\Exception\\LdapConnectionException');
 }
 /**
  * Sets the needed objects on the operation invoker.
  */
 protected function setupOperationInvoker()
 {
     $this->config->getOperationInvoker()->setEventDispatcher($this->dispatcher);
     $this->config->getOperationInvoker()->setConnection($this);
     if ($this->logger) {
         $this->config->getOperationInvoker()->setLogger($this->logger);
     }
 }
 /**
  * Attempt to lookup the LDAP servers from the DNS name.
  *
  * @return array The LDAP servers.
  * @throws LdapConnectionException
  */
 protected function getServersFromDns()
 {
     $servers = $this->dns->getRecord(LdapUtilities::SRV_PREFIX . $this->config->getDomainName(), DNS_SRV);
     if ($servers === false || empty($servers)) {
         throw new LdapConnectionException(sprintf('No LDAP servers found via DNS for "%s".', $this->config->getDomainName()));
     }
     array_multisort(array_column($servers, 'pri'), SORT_ASC | SORT_NUMERIC, array_column($servers, 'weight'), SORT_DESC | SORT_NUMERIC, $servers);
     return array_column($servers, 'target');
 }
 function it_should_call_creation_events_when_creating_a_ldap_object(EventDispatcherInterface $dispatcher, $connection)
 {
     $this->addOperation->setLocation('dc=foo,dc=bar');
     $connection->execute($this->addOperation)->willReturn(true);
     $beforeEvent = new LdapObjectCreationEvent(Event::LDAP_OBJECT_BEFORE_CREATE);
     $beforeEvent->setContainer('dc=foo,dc=bar');
     $beforeEvent->setData(['username' => '%foo%', 'password' => '%bar%']);
     $beforeEvent->setDn('');
     $afterEvent = new LdapObjectCreationEvent(Event::LDAP_OBJECT_AFTER_CREATE);
     $afterEvent->setContainer('dc=foo,dc=bar');
     $afterEvent->setData(['username' => 'somedude', 'password' => '12345']);
     $afterEvent->setDn('cn=somedude,dc=foo,dc=bar');
     $dispatcher->dispatch($beforeEvent)->shouldBeCalled();
     $dispatcher->dispatch($afterEvent)->shouldBeCalled();
     $this->config->setSchemaName('ad');
     $this->beConstructedWith($connection, $this->schemaFactory, $dispatcher);
     $this->createUser()->with(['username' => '%foo%', 'password' => '%bar%'])->in('dc=foo,dc=bar')->setParameter('foo', 'somedude')->setParameter('bar', '12345');
     $this->execute();
 }
 function it_should_switch_to_the_domain_defined_in_the_annotation_when_loading_a_ldap_object($eventArgs, $schema, $ldap, $qb, $query, $entity, $schemaFactory, $reader, $metadata, \ReflectionProperty $rp)
 {
     $value = 'foo';
     $annotation = new LdapObject();
     $ldapObject = new \LdapTools\Object\LdapObject(['foo', 'bar'], ['user'], 'user', 'user');
     $metadata->getReflectionProperties()->shouldBeCalled()->willReturn([$rp]);
     $reader->getPropertyAnnotation($rp, Argument::any())->shouldBeCalled()->willReturn($annotation);
     $rp->getValue($entity)->shouldBeCalled()->willReturn($value);
     $schemaFactory->get($this->config->getSchemaName(), $annotation->type)->shouldBeCalled()->willReturn($schema);
     $schema->getAttributesToSelect()->shouldBeCalled()->willReturn(['foo', 'bar']);
     $qb->select(['foo', 'bar', 'guid'])->shouldBeCalled()->willReturn($qb);
     $qb->from($annotation->type)->shouldBeCalled()->willReturn($qb);
     $qb->where([$annotation->id => $value])->shouldBeCalled()->willReturn($qb);
     $query->getOneOrNullResult()->shouldBeCalled()->willReturn($ldapObject);
     $rp->setValue($entity, $ldapObject)->shouldBeCalled();
     $domain = 'example.local';
     $annotation->domain = $domain;
     $ldap->switchDomain($domain)->shouldBeCalledTimes(1);
     $ldap->switchDomain('foo.bar')->shouldBeCalledTimes(1);
     $this->postLoad($eventArgs);
 }
 function it_should_adjust_the_port_if_it_changes_in_the_domain_config(TcpSocket $tcp)
 {
     $tcp->connect('foo', 389, 1)->shouldBeCalled()->willReturn(true);
     $tcp->close()->shouldBeCalled();
     $config = new DomainConfiguration('example.com');
     $config->setServers(['foo']);
     $this->beConstructedWith($config, $tcp);
     $this->getServer()->shouldReturn('foo');
     $config->setPort(9001);
     $tcp->connect('foo', 9001, 1)->shouldBeCalled()->willReturn(true);
     $this->getServer()->shouldReturn('foo');
 }
 /**
  * Given the connection resource and the other required parameters, attempt the bind and return the result.
  *
  * @param string $username
  * @return string
  */
 public function getUsername($username)
 {
     $replacements = [$username, $this->config->getDomainName()];
     return preg_replace($this->params, $replacements, $this->bindFormat);
 }
Exemple #15
0
 function it_should_sort_results_for_multiple_aliases($connection)
 {
     $config = new Configuration();
     $domain = new DomainConfiguration('example.com');
     $domain->setServers(['example'])->setBaseDn('dc=example,dc=com')->setLazyBind(true)->setPageSize(500);
     $connection->getConfig()->willReturn($domain);
     $config->setCacheType('none');
     $parser = SchemaParserFactory::get($config->getSchemaFormat(), $config->getSchemaFolder());
     $cache = CacheFactory::get($config->getCacheType(), []);
     $dispatcher = new SymfonyEventDispatcher();
     $schemaFactory = new LdapObjectSchemaFactory($cache, $parser, $dispatcher);
     $ou = $schemaFactory->get('ad', 'ou');
     $container = $schemaFactory->get('ad', 'container');
     $filter = new OperatorCollection();
     $filter->addLdapObjectSchema($ou);
     $filter->addLdapObjectSchema($container);
     $this->operation->setFilter($filter);
     $this->operation->setAttributes([]);
     $connection->execute(Argument::that(function ($op) {
         return $op->getFilter() == '(objectClass=organizationalUnit)';
     }))->shouldBeCalled()->willReturn($this->ous);
     $connection->execute(Argument::that(function ($op) {
         return $op->getFilter() == '(&(objectCategory=container))';
     }))->shouldBeCalled()->willReturn($this->containers);
     $this->setOrderBy(['Name' => LdapQuery::ORDER['ASC'], 'ou.Description' => LdapQuery::ORDER['DESC']]);
     $this->getResult()->shouldHavePlaceKeyAndValue(0, 'name', 'Computers');
     $this->getResult()->shouldHavePlaceKeyAndValue(1, 'name', 'Employees');
     $this->getResult()->shouldHavePlaceKeyAndValue(2, 'name', 'Users');
     $this->getResult()->shouldHavePlaceKeyAndValue(3, 'name', 'West');
 }
 function it_should_return_FilterBuilder_when_calling_filter_and_the_ldap_type_is_not_ActiveDirectory()
 {
     $domain = new DomainConfiguration('example.com');
     $domain->setServers(['example'])->setBaseDn('dc=example,dc=com')->setLazyBind(true)->setPageSize(500)->setLdapType(LdapConnection::TYPE_OPENLDAP);
     $connection = new LdapConnection($domain);
     $this->beConstructedWith($connection);
     $this->filter()->shouldBeLike(new FilterBuilder());
 }
 function it_should_get_the_ldif_string_representation_in_the_context_of_a_type_and_a_schema(LdapConnectionInterface $connection, LdapObject $rootdse)
 {
     $domain = new DomainConfiguration('example.local');
     $domain->setUseTls(true);
     $connection->getConfig()->willReturn($domain);
     $connection->getRootDse()->willReturn($rootdse);
     $config = new Configuration();
     $parser = new SchemaYamlParser($config->getSchemaFolder());
     $schema = $parser->parse('ad', 'user');
     $dn = 'cn=foo,dc=foo,dc=bar';
     $this->beConstructedWith($dn);
     $this->setLdapObjectSchema($schema);
     $this->setLdapConnection($connection);
     $this->add('phoneNumber', '555-5555');
     $this->reset('lastName');
     $this->replace('firstName', 'bar');
     $this->delete('password', 'foo');
     $this->add('password', 'bar');
     $ldif = "dn: {$dn}\r\n" . "changetype: modify\r\n" . "add: telephoneNumber\r\n" . "telephoneNumber: 555-5555\r\n" . "-\r\n" . "delete: sn\r\n" . "-\r\n" . "replace: givenName\r\n" . "givenName: bar\r\n" . "-\r\n" . "delete: unicodePwd\r\n" . "unicodePwd: IgBmAG8AbwAiAA==\r\n" . "-\r\n" . "add: unicodePwd\r\n" . "unicodePwd: IgBiAGEAcgAiAA==\r\n" . "-\r\n";
     $this->toString()->shouldBeEqualTo($ldif);
 }
 function it_should_register_converters_listed_in_the_config()
 {
     $config = new Configuration();
     $config->setSchemaFolder(__DIR__ . '/../resources/schema');
     $config->setAttributeConverters(['my_bool' => '\\LdapTools\\AttributeConverter\\ConvertBoolean']);
     $domain = new DomainConfiguration('example.com');
     $domain->setServers(['example'])->setLazyBind(true)->setBaseDn('dc=example,dc=com')->setSchemaName('example');
     $config->addDomain($domain);
     $this->beConstructedWith($config);
     $this->buildLdapQuery()->from('custom_converter')->where(['foo' => true])->toLdapFilter()->shouldBeEqualTo('(&(objectClass=foo)(&(bar=TRUE)))');
 }
Exemple #19
0
 /**
  * Iterates through and loads the domain section of the configuration.
  *
  * @param $config
  * @throws ConfigurationException
  */
 protected function loadDomainConfiguration(array $config)
 {
     try {
         foreach ($config['domains'] as $domain => $options) {
             $domain = new DomainConfiguration();
             $domain->load($options);
             $this->addDomain($domain);
         }
     } catch (ConfigurationException $e) {
         throw new ConfigurationException(sprintf("Error in domain config section: %s", $e->getMessage()));
     }
 }
 function it_should_get_the_ldif_representation_in_the_context_of_a_type_and_schema(LdapConnectionInterface $connection, LdapObject $rootdse)
 {
     $domain = new DomainConfiguration('example.local');
     $domain->setUseTls(true);
     $connection->getConfig()->willReturn($domain);
     $connection->getRootDse()->willReturn($rootdse);
     $config = new Configuration();
     $parser = new SchemaYamlParser($config->getSchemaFolder());
     $schema = $parser->parse('ad', 'user');
     $this->beConstructedWith(null);
     $this->setLdapObjectSchema($schema);
     $this->setLdapConnection($connection);
     $this->setAttributes(['username' => 'John', 'password' => '12345']);
     $this->setLocation('ou=employees,dc=example,dc=local');
     $ldif = "dn: cn=John,ou=employees,dc=example,dc=local\r\n" . "changetype: add\r\n" . "cn: John\r\n" . "displayname: John\r\n" . "givenName: John\r\n" . "userPrincipalName: John@example.local\r\n" . "objectclass: top\r\n" . "objectclass: person\r\n" . "objectclass: organizationalPerson\r\n" . "objectclass: user\r\n" . "sAMAccountName: John\r\n" . "unicodePwd: IgAxADIAMwA0ADUAIgA=\r\n" . "userAccountControl: 512\r\n";
     $this->toString()->shouldBeEqualTo($ldif);
 }