1<?php 2/** 3 * This file is part of the FreeDSx LDAP package. 4 * 5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11namespace FreeDSx\Ldap\Server\RequestHandler; 12 13use FreeDSx\Ldap\Entry\Entries; 14use FreeDSx\Ldap\Exception\BindException; 15use FreeDSx\Ldap\Exception\OperationException; 16use FreeDSx\Ldap\LdapClient; 17use FreeDSx\Ldap\Operation\LdapResult; 18use FreeDSx\Ldap\Operation\Request\AddRequest; 19use FreeDSx\Ldap\Operation\Request\CompareRequest; 20use FreeDSx\Ldap\Operation\Request\DeleteRequest; 21use FreeDSx\Ldap\Operation\Request\ExtendedRequest; 22use FreeDSx\Ldap\Operation\Request\ModifyDnRequest; 23use FreeDSx\Ldap\Operation\Request\ModifyRequest; 24use FreeDSx\Ldap\Operation\Request\SearchRequest; 25use FreeDSx\Ldap\Operation\ResultCode; 26use FreeDSx\Ldap\Server\RequestContext; 27 28/** 29 * Proxies requests to an LDAP server and returns the response. You should extend this to add your own constructor and 30 * set the LDAP client options variable. 31 * 32 * @author Chad Sikorra <Chad.Sikorra@gmail.com> 33 */ 34class ProxyRequestHandler implements RequestHandlerInterface 35{ 36 /** 37 * @var LdapClient|null 38 */ 39 protected $ldap; 40 41 /** 42 * @var array 43 */ 44 protected $options = []; 45 46 /** 47 * {@inheritdoc} 48 */ 49 public function bind(string $username, string $password): bool 50 { 51 try { 52 return (bool) $this->ldap()->bind($username, $password); 53 } catch (BindException $e) { 54 throw new OperationException($e->getMessage(), $e->getCode()); 55 } 56 } 57 58 /** 59 * {@inheritdoc} 60 */ 61 public function modify(RequestContext $context, ModifyRequest $modify): void 62 { 63 $this->ldap()->sendAndReceive($modify, ...$context->controls()->toArray()); 64 } 65 66 /** 67 * {@inheritdoc} 68 */ 69 public function modifyDn(RequestContext $context, ModifyDnRequest $modifyDn): void 70 { 71 $this->ldap()->sendAndReceive($modifyDn, ...$context->controls()->toArray()); 72 } 73 74 /** 75 * {@inheritdoc} 76 */ 77 public function delete(RequestContext $context, DeleteRequest $delete): void 78 { 79 $this->ldap()->sendAndReceive($delete, ...$context->controls()->toArray()); 80 } 81 82 /** 83 * {@inheritdoc} 84 */ 85 public function add(RequestContext $context, AddRequest $add): void 86 { 87 $this->ldap()->sendAndReceive($add, ...$context->controls()->toArray()); 88 } 89 90 /** 91 * {@inheritdoc} 92 */ 93 public function search(RequestContext $context, SearchRequest $search): Entries 94 { 95 return $this->ldap()->search($search, ...$context->controls()->toArray()); 96 } 97 98 /** 99 * {@inheritdoc} 100 */ 101 public function compare(RequestContext $context, CompareRequest $compare): bool 102 { 103 $response = $this->ldap()->sendAndReceive($compare, ...$context->controls()->toArray())->getResponse(); 104 if (!$response instanceof LdapResult) { 105 throw new OperationException('The result was malformed.', ResultCode::PROTOCOL_ERROR); 106 } 107 108 return $response->getResultCode() === ResultCode::COMPARE_TRUE; 109 } 110 111 /** 112 * {@inheritdoc} 113 */ 114 public function extended(RequestContext $context, ExtendedRequest $extended): void 115 { 116 $this->ldap()->send($extended, ...$context->controls()->toArray()); 117 } 118 119 /** 120 * @param LdapClient $client 121 */ 122 public function setLdapClient(LdapClient $client): void 123 { 124 $this->ldap = $client; 125 } 126 127 /** 128 * @return LdapClient 129 */ 130 protected function ldap(): LdapClient 131 { 132 if ($this->ldap === null) { 133 $this->ldap = new LdapClient($this->options); 134 } 135 136 return $this->ldap; 137 } 138} 139