3 namespace Adldap\Models\Attributes
;
6 use Adldap\Schemas\ActiveDirectory
;
7 use Adldap\Schemas\SchemaInterface
;
9 class DistinguishedName
12 * The common names in the DN.
16 public $commonNames = [];
19 * The uid's in the DN.
26 * The organizational units in the DN.
30 public $organizationUnits = [];
33 * The domain components in the DN.
37 public $domainComponents = [];
40 * The organization names in the DN.
44 public $organizationNames = [];
47 * The current LDAP schema.
49 * @var SchemaInterface
54 * The RDN attribute types.
69 * @param mixed $baseDn
70 * @param SchemaInterface $schema
72 public function __construct($baseDn = null, SchemaInterface
$schema = null)
74 $this->setBase($baseDn)
79 * Returns the complete distinguished name.
83 public function __toString()
89 * Returns the complete distinguished name.
95 return $this->assemble();
99 * Adds a domain component.
103 * @return DistinguishedName
105 public function addDc($dc)
107 $this->domainComponents
[] = $dc;
113 * Removes a domain component.
117 * @return DistinguishedName
119 public function removeDc($dc)
121 $this->domainComponents
= array_diff($this->domainComponents
, [$dc]);
127 * Adds an organization name.
133 public function addO($o)
135 $this->organizationNames
[] = $o;
141 * Removes an organization name.
145 * @return DistinguishedName
147 public function removeO($o)
149 $this->organizationNames
= array_diff($this->organizationNames
, [$o]);
155 * Add a user identifier.
159 * @return DistinguishedName
161 public function addUid($uid)
163 $this->userIds
[] = $uid;
169 * Removes a user identifier.
173 * @return DistinguishedName
175 public function removeUid($uid)
177 $this->userIds
= array_diff($this->userIds
, [$uid]);
183 * Adds a common name.
187 * @return DistinguishedName
189 public function addCn($cn)
191 $this->commonNames
[] = $cn;
197 * Removes a common name.
201 * @return DistinguishedName
203 public function removeCn($cn)
205 $this->commonNames
= array_diff($this->commonNames
, [$cn]);
211 * Adds an organizational unit.
215 * @return DistinguishedName
217 public function addOu($ou)
219 $this->organizationUnits
[] = $ou;
225 * Removes an organizational unit.
229 * @return DistinguishedName
231 public function removeOu($ou)
233 $this->organizationUnits
= array_diff($this->organizationUnits
, [$ou]);
239 * Sets the base RDN of the distinguished name.
241 * @param string|DistinguishedName $base
243 * @return DistinguishedName
245 public function setBase($base)
247 // Typecast base to string in case we've been given
248 // an instance of the distinguished name object.
249 $base = (string) $base;
251 // If the base DN isn't null we'll try to explode it.
252 $base = Utilities
::explodeDn($base, false) ?
: [];
254 // Remove the count key from the exploded distinguished name.
255 unset($base['count']);
257 foreach ($base as $key => $rdn) {
258 // We'll break the RDN into pieces
259 $pieces = explode('=', $rdn) ?
: [];
261 // If there's exactly 2 pieces, then we can work with it.
262 if (count($pieces) === 2) {
263 $attribute = ucfirst($pieces[0]);
265 $method = 'add'.$attribute;
267 if (method_exists($this, $method)) {
268 // We see what type of RDN it is and add each accordingly.
269 call_user_func_array([$this, $method], [$pieces[1]]);
278 * Sets the schema for the distinguished name.
280 * @param SchemaInterface|null $schema
282 * @return DistinguishedName
284 public function setSchema(SchemaInterface
$schema = null)
286 $this->schema
= $schema ?
: new ActiveDirectory();
292 * Assembles all of the RDNs and returns the result.
296 public function assemble()
298 return implode(',', array_filter([
299 $this->assembleCns(),
300 $this->assembleUids(),
301 $this->assembleOus(),
302 $this->assembleDcs(),
308 * Assembles the common names in the distinguished name.
312 public function assembleCns()
314 return $this->assembleRdns($this->schema
->commonName(), $this->commonNames
);
318 * Assembles the user ID's in the distinguished name.
322 public function assembleUids()
324 return $this->assembleRdns($this->schema
->userId(), $this->userIds
);
328 * Assembles the organizational units in the distinguished Name.
332 public function assembleOus()
334 return $this->assembleRdns($this->schema
->organizationalUnitShort(), $this->organizationUnits
);
338 * Assembles the domain components in the distinguished Name.
342 public function assembleDcs()
344 return $this->assembleRdns($this->schema
->domainComponent(), $this->domainComponents
);
348 * Assembles the organization names in the distinguished name.
352 public function assembleOs()
354 return $this->assembleRdns($this->schema
->organizationName(), $this->organizationNames
);
358 * Assembles an RDN with the specified attribute and value.
360 * @param string $attribute
361 * @param array $values
365 protected function assembleRdns($attribute, array $values = [])
367 return implode(',', array_map(function ($value) use ($attribute) {
368 return sprintf('%s=%s', $attribute, Utilities
::escape($value, '', 2));