/** * Returns the group's member names only. * * @return array */ public function getMemberNames() { $members = []; $dns = $this->getAttribute($this->schema->member()) ?: []; foreach ($dns as $dn) { $exploded = Utilities::explodeDn($dn); if (array_key_exists(0, $exploded)) { $members[] = $exploded[0]; } } return $members; }
/** * Returns the model's object category DN in an exploded array. * * @return array|false */ public function getObjectCategoryArray() { return Utilities::explodeDn($this->getObjectCategoryDn()); }
/** * Validates if the specified group is the given parent instance. * * @param Group|string $group * @param Group $parent * * @return bool */ protected function validateGroup($group, Group $parent) { if ($group instanceof Group) { // We've been given a group instance, we'll compare their DNs. return $parent->getDn() === $group->getDn(); } if (Utilities::explodeDn($group)) { // We've been given a DN, we'll compare it to the parents. return $parent->getDn() === $group; } if (!empty($group)) { // We've been given just a string, we'll // compare it to the parents name. return $parent->getCommonName() === $group; } return false; }
/** * Sets the base DB string. * * @param string $base * * @return DistinguishedName */ public function setBase($base) { if (!is_null($base)) { // If the base DN isn't null we'll try to explode it. $base = Utilities::explodeDn($base, false); // Since exploding a DN returns false on failure, // we'll double check to make sure it's an array if (is_array($base)) { // We need to reverse the base to keep the order in tact since RDNs // are already reversed to follow the right to left pattern $base = array_reverse($base); foreach ($base as $key => $rdn) { // We'll avoid going through the count key as it's // automatically created when exploding a DN if ($key !== 'count') { // We'll break the RDN into pieces $pieces = explode('=', $rdn); // If there's exactly 2 pieces, then we can work with it. if (count($pieces) === 2) { // We see what type of RDN it is and add each accordingly switch (strtoupper($pieces[0])) { case 'DC': $this->addDc($pieces[1]); break; case 'OU': $this->addOu($pieces[1]); break; case 'CN': $this->addCn($pieces[1]); break; } } } } } } return $this; }
/** * Returns the models memberOf names only. * * @return array */ public function getMemberOfNames() { $names = []; $dns = $this->getAttribute(ActiveDirectory::MEMBER_OF); if (is_array($dns)) { foreach ($dns as $dn) { $exploded = Utilities::explodeDn($dn); if (array_key_exists(0, $exploded)) { $names[] = $exploded[0]; } } } return $names; }
/** * Returns a new LDAP Entry instance. * * @param array $attributes * * @return Entry */ public function newLdapEntry(array $attributes = []) { $attribute = ActiveDirectory::OBJECT_CATEGORY; if (array_key_exists($attribute, $attributes) && array_key_exists(0, $attributes[$attribute])) { // We'll explode the DN so we can grab it's object category. $category = Utilities::explodeDn($attributes[$attribute][0]); // Make sure the category string exists in the attribute array. if (array_key_exists(0, $category)) { $category = strtolower($category[0]); // Retrieve the category model mapping. if (array_key_exists($category, $this->mappings)) { $model = $this->mappings[$category]; // Check that the model actually exists // before trying to instantiate it. if (class_exists($model)) { return (new $model([], $this))->setRawAttributes($attributes); } } } } // A default entry object if the object category isn't found. return (new Entry([], $this))->setRawAttributes($attributes); }
/** * Returns a new LDAP Entry instance. * * @param array $attributes * * @return Entry */ public function newLdapEntry(array $attributes = []) { $attribute = $this->schema->objectCategory(); if (array_key_exists($attribute, $attributes) && array_key_exists(0, $attributes[$attribute])) { // We'll explode the DN so we can grab it's object category. $category = Utilities::explodeDn($attributes[$attribute][0]); // Make sure the category string exists in the attribute array. if (array_key_exists(0, $category)) { $category = strtolower($category[0]); $mappings = $this->map(); // Retrieve the category model mapping. if (array_key_exists($category, $mappings)) { $model = $mappings[$category]; return $this->newModel([], $model)->setRawAttributes($attributes); } } } // A default entry model if the object category isn't found. return $this->newModel()->setRawAttributes($attributes); }
/** * Returns the models memberOf names only. * * @return array */ public function getMemberOfNames() { $names = []; $dns = $this->getAttribute($this->getSchema()->memberOf()); if (is_array($dns)) { foreach ($dns as $dn) { $exploded = Utilities::explodeDn($dn); if (array_key_exists(0, $exploded)) { $names[] = $exploded[0]; } } } return $names; }
/** * Sets the base RDN of the distinguished name. * * @param string|DistinguishedName $base * * @return DistinguishedName */ public function setBase($base) { // Typecast base to string in case we've been given // an instance of the distinguished name object. $base = (string) $base; // If the base DN isn't null we'll try to explode it. $base = Utilities::explodeDn($base, false) ?: []; // Remove the count key from the exploded distinguished name. unset($base['count']); foreach ($base as $key => $rdn) { // We'll break the RDN into pieces $pieces = explode('=', $rdn) ?: []; // If there's exactly 2 pieces, then we can work with it. if (count($pieces) === 2) { $attribute = ucfirst($pieces[0]); $method = 'add' . $attribute; if (method_exists($this, $method)) { // We see what type of RDN it is and add each accordingly. call_user_func_array([$this, $method], [$pieces[1]]); } } } return $this; }