/** * Compiles the Builder instance into an LDAP query string. * * @param \Adldap\Query\Builder $builder * * @return string */ public function compileQuery(Builder $builder) { // Retrieve the query 'where' bindings. $wheres = $builder->getWheres(); // Retrieve the query 'orWhere' bindings. $orWheres = $builder->getOrWheres(); // Retrieve the query filter bindings. $filters = $builder->getFilters(); // We'll combine all raw filters together first. $query = implode(null, $filters); // Compile wheres. $query = $this->compileWheres($wheres, $query); // Compile or wheres. $query = $this->compileOrWheres($orWheres, $query); // Count the total amount of filters. $total = count($wheres) + count($filters); // Make sure we wrap the query in an 'and' if using // multiple filters. We also need to check if only // one where is used with multiple orWheres, that // we wrap it in an `and` query. if ($total > 1 || count($wheres) === 1 && count($orWheres) > 0) { $query = $this->compileAnd($query); } return $query; }
/** * Sorts LDAP search results. * * @param array $models * * @return array */ private function processSort(array $models = []) { $collection = $this->newCollection($models); $sort = [$this->builder->getSortByField() => $this->builder->getSortByDirection()]; $criteria = (new Criteria())->orderBy($sort); return $collection->matching($criteria)->toArray(); }
/** * Sorts LDAP search results. * * @param array $models * * @return Collection */ protected function processSort(array $models = []) { $field = $this->builder->getSortByField(); $flags = $this->builder->getSortByFlags(); $direction = $this->builder->getSortByDirection(); $desc = $direction === 'desc' ? true : false; return $this->newCollection($models)->sortBy(function (Model $model) use($field) { return $model->getAttribute($field, 0); }, $flags, $desc); }
/** * Sorts LDAP search results. * * @param array $models * * @return Collection */ protected function processSort(array $models = []) { $collection = $this->newCollection($models); $field = $this->builder->getSortByField(); $direction = $this->builder->getSortByDirection(); if ($direction === 'desc') { $sorted = $collection->sortByDesc($field); } else { $sorted = $collection->sortBy($field); } return $sorted; }
/** * Moves the current model to a new RDN and new parent. * * @param string $rdn * @param string|null $newParentDn * @param bool|true $deleteOldRdn * * @return bool */ public function move($rdn, $newParentDn = null, $deleteOldRdn = true) { $moved = $this->query->getConnection()->rename($this->getDn(), $rdn, $newParentDn, $deleteOldRdn); if ($moved) { // If the model was successfully moved, we'll set its // new DN so we can sync it's attributes properly. $this->setDn("{$rdn},{$newParentDn}"); $this->syncRaw(); return true; } return false; }
/** * Deletes the current entry. * * @throws ModelNotFoundException * @throws AdldapException * * @return bool */ public function delete() { $dn = $this->getDn(); if (!$this->exists) { // Make sure the record exists before we can delete it $message = 'Model does not exist in active directory.'; throw new ModelNotFoundException($message); } elseif (is_null($dn) || empty($dn)) { // If the record exists but the DN attribute does // not exist, we can't process a delete. $message = 'Unable to delete. The current model does not have a distinguished name present.'; throw new AdldapException($message); } $deleted = $this->query->getConnection()->delete($dn); if ($deleted) { // We'll set the exists property to false on delete // so the dev can run create operations $this->exists = false; return true; } return false; }
/** * Returns the current LDAP query string. * * @return string */ public function getQuery() { return $this->query->get(); }
/** * Get the RootDSE properties from a domain controller. * * @return AbstractModel|bool */ protected function getRootDse() { return $this->query->setDn(null)->read(true)->whereHas($this->schema->objectClass())->first(); }
/** * Assembles all or where clauses in the current orWheres property. * * @param Builder $builder * @param string $query * * @return string */ protected function compileOrWheres(Builder $builder, $query = '') { $ors = ''; foreach ($builder->getOrWheres() as $where) { $ors .= $this->compileWhere($where); } // Make sure we wrap the query in an 'and' if using // multiple wheres. For example (&QUERY). if (count($builder->getOrWheres()) > 0) { $query .= $this->compileOr($ors); } return $query; }
/** * Moves the current model to a new RDN and new parent. * * @param string $rdn * @param string $newParentDn * @param bool|true $deleteOldRdn * * @return bool */ public function move($rdn, $newParentDn, $deleteOldRdn = true) { return $this->query->getConnection()->rename($this->getDn(), $rdn, $newParentDn, $deleteOldRdn); }
/** * Performs a global 'all' search query on the * current connection. * * @return array|bool */ public function all() { return $this->query->whereHas(ActiveDirectory::COMMON_NAME)->get(); }
/** * Handle dynamic method calls on the query builder object. * * @param string $method * @param array $parameters * * @return mixed */ public function __call($method, $parameters) { $query = $this->query->newInstance(); return call_user_func_array([$query, $method], $parameters); }