class LdapQuery in Lightweight Directory Access Protocol (LDAP) 8.3
Same name and namespace in other branches
- 8.4 ldap_query/src/Plugin/views/query/LdapQuery.php \Drupal\ldap_query\Plugin\views\query\LdapQuery
Views query plugin for an SQL query.
Plugin annotation
@ViewsQuery(
id = "ldap_query",
title = @Translation("LDAP Query"),
help = @Translation("Query will be generated and run via LDAP.")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\views\Plugin\views\PluginBase implements DependentPluginInterface, ContainerFactoryPluginInterface, TrustedCallbackInterface, ViewsPluginInterface
- class \Drupal\views\Plugin\views\query\QueryPluginBase implements CacheableDependencyInterface
- class \Drupal\ldap_query\Plugin\views\query\LdapQuery
- class \Drupal\views\Plugin\views\query\QueryPluginBase implements CacheableDependencyInterface
- class \Drupal\views\Plugin\views\PluginBase implements DependentPluginInterface, ContainerFactoryPluginInterface, TrustedCallbackInterface, ViewsPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of LdapQuery
File
- ldap_query/
src/ Plugin/ views/ query/ LdapQuery.php, line 23
Namespace
Drupal\ldap_query\Plugin\views\queryView source
class LdapQuery extends QueryPluginBase {
/**
* Collection of filter criteria.
*
* @var array
*/
public $where = [];
/**
* Collection of sort criteria.
*
* @var array
*/
public $orderby = [];
/**
* Maps SQL operators to LDAP operators.
*
* @var array
*/
private const LDAP_FILTER_OPERATORS = [
'AND' => '&',
'OR' => '|',
];
/**
* {@inheritdoc}
*/
public function build(ViewExecutable $view) {
// Store the view in the object to be able to use it later.
$this->view = $view;
$view
->initPager();
// Let the pager modify the query to add limits.
$view->pager
->query();
$view->build_info['query'] = $this
->query();
$view->build_info['count_query'] = $this
->query(TRUE);
}
/**
* Execute the query.
*
* @param \Drupal\views\ViewExecutable $view
* The view.
*
* @return bool|void
* Nothing if query can be executed.
*/
public function execute(ViewExecutable $view) {
if (!isset($this->options['query_id']) || empty($this->options['query_id'])) {
\Drupal::logger('ldap')
->error('You are trying to use Views without having chosen an LDAP Query under Advanced => Query settings.');
return FALSE;
}
$start = microtime(TRUE);
$controller = new QueryController($this->options['query_id']);
$filter = $this
->buildLdapFilter($controller
->getFilter());
$controller
->execute($filter);
$results = $controller
->getRawResults();
$fields = $controller
->availableFields();
$index = 0;
unset($results['count']);
$rows = [];
foreach ($results as $result) {
$row = [];
// TODO: Try to only fetch requested fields instead of all available.
foreach ($fields as $field_key => $void) {
if (isset($result[$field_key])) {
unset($result[$field_key]['count']);
$row[$field_key] = $result[$field_key];
}
}
$row['index'] = $index++;
$rows[] = $row;
}
if (!empty($this->orderby) && !empty($rows)) {
$rows = $this
->sortResults($rows);
}
foreach ($rows as $row) {
$view->result[] = new ResultRow($row);
}
// Pager.
$totalItems = count($view->result);
$offset = $view->pager
->getCurrentPage() * $view->pager
->getItemsPerPage() + $view->pager
->getOffset();
$length = NULL;
if ($view->pager
->getItemsPerPage() > 0) {
$length = $view->pager
->getItemsPerPage();
}
if ($offset > 0 || $length > 0) {
$view->result = array_splice($view->result, $offset, $length);
}
$view->pager
->postExecute($view->result);
$view->pager->total_items = $totalItems;
$view->pager
->updatePageInfo();
$view->total_rows = $view->pager
->getTotalItems();
// Timing information.
$view->execute_time = microtime(TRUE) - $start;
}
/**
* Sort the results.
*
* @param array $results
* Results to operate on.
*
* @return array
* Result data.
*/
private function sortResults(array $results) {
$parameters = [];
$orders = $this->orderby;
$set = [];
foreach ($orders as $orderCriterion) {
foreach ($results as $key => $row) {
// TODO: Could be improved by making the element index configurable.
$orderCriterion['data'][$key] = $row[$orderCriterion['field']][0];
$set[$key][$orderCriterion['field']] = $row[$orderCriterion['field']][0];
$set[$key]['index'] = $key;
}
$parameters[] = $orderCriterion['data'];
if ($orderCriterion['direction'] == 'ASC') {
$parameters[] = SORT_ASC;
}
else {
$parameters[] = SORT_DESC;
}
}
$parameters[] =& $set;
call_user_func_array('array_multisort', $parameters);
$processedResults = [];
foreach ($set as $row) {
$processedResults[] = $results[$row['index']];
}
return $processedResults;
}
/**
* {@inheritdoc}
*/
public function ensureTable($table, $relationship = NULL) {
return '';
}
/**
* {@inheritdoc}
*/
public function addField($table, $field, $alias = '', $params = []) {
return $field;
}
/**
* {@inheritdoc}
*/
public function addOrderBy($table, $field, $order, $alias = '', $params = []) {
$this->orderby[] = [
'field' => $field,
'direction' => $order,
];
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['query_id'] = [
'default' => NULL,
];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$qids = \Drupal::EntityQuery('ldap_query_entity')
->condition('status', 1)
->execute();
$form['query_id'] = [
'#type' => 'select',
'#options' => $qids,
'#title' => $this
->t('Ldap Query'),
'#default_value' => $this->options['query_id'],
'#description' => $this
->t('The LDAP query you want Views to use.'),
'#required' => TRUE,
];
}
/**
* {@inheritdoc}
*/
public function addWhere($group, $field, $value = NULL, $operator = NULL) {
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all
// the default group.
if (empty($group)) {
$group = 0;
}
// Check for a group.
if (!isset($this->where[$group])) {
$this
->setWhereGroup('AND', $group);
}
if (!empty($operator) && $operator != 'LIKE') {
$this->where[$group]['conditions'][] = [
'field' => $field,
'value' => $value,
'operator' => $operator,
];
}
}
/**
* Compiles all conditions into a set of LDAP requirements.
*
* @return string
* Condition string.
*/
public function buildConditions() {
$groups = [];
foreach ($this->where as $group) {
if (!empty($group['conditions'])) {
$conditions = '';
foreach ($group['conditions'] as $clause) {
$conditions .= $this
->translateCondition($clause['field'], $clause['value'], $clause['operator']);
}
if (count($group['conditions']) > 1) {
$groups[] = '(' . self::LDAP_FILTER_OPERATORS[$group['type']] . $conditions . ')';
}
else {
$groups[] = $conditions;
}
}
}
if (count($groups) > 1) {
$output = '(' . self::LDAP_FILTER_OPERATORS[$this->groupOperator] . implode($groups) . ')';
}
else {
$output = array_pop($groups);
}
return $output;
}
/**
* Collates Views arguments and filters for a modified query.
*
* @param string $standardFilter
* The filter in LDAP query which gets overwritten.
*
* @return string
* Combined string.
*/
private function buildLdapFilter($standardFilter) {
$searchFilter = $this
->buildConditions();
if (!empty($searchFilter)) {
$finalFilter = '(&' . $standardFilter . $searchFilter . ')';
}
else {
$finalFilter = $standardFilter;
}
return $finalFilter;
}
/**
* Produces a filter condition and adds optional negation.
*
* @param string $field
* LDAP attribute name.
* @param string $value
* Field value.
* @param string $operator
* Negation operator.
*
* @return string
* LDAP filter such as (cn=Example).
*/
private function translateCondition($field, $value, $operator) {
$item = '(' . $field . '=' . SafeMarkup::checkPlain($value) . ')';
if (mb_substr($operator, 0, 1) == '!') {
$condition = "(!{$item})";
}
else {
$condition = $item;
}
return $condition;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
LdapQuery:: |
public | property | Collection of sort criteria. | |
LdapQuery:: |
public | property | Collection of filter criteria. | |
LdapQuery:: |
public | function | ||
LdapQuery:: |
public | function | ||
LdapQuery:: |
public | function | ||
LdapQuery:: |
public | function |
Builds the necessary info to execute the query. Overrides QueryPluginBase:: |
|
LdapQuery:: |
public | function | Compiles all conditions into a set of LDAP requirements. | |
LdapQuery:: |
private | function | Collates Views arguments and filters for a modified query. | |
LdapQuery:: |
public | function |
Provide a form to edit options for this plugin. Overrides PluginBase:: |
|
LdapQuery:: |
protected | function |
Information about options for all kinds of purposes will be held here. Overrides PluginBase:: |
|
LdapQuery:: |
public | function | ||
LdapQuery:: |
public | function |
Execute the query. Overrides QueryPluginBase:: |
|
LdapQuery:: |
private | constant | Maps SQL operators to LDAP operators. | |
LdapQuery:: |
private | function | Sort the results. | |
LdapQuery:: |
private | function | Produces a filter condition and adds optional negation. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
public | property | Plugins's definition | |
PluginBase:: |
public | property | The display object this plugin is for. | |
PluginBase:: |
public | property | Options for this plugin will be held here. | |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
protected | property | Stores the render API renderer. | 3 |
PluginBase:: |
protected | property | Denotes whether the plugin has an additional options form. | 8 |
PluginBase:: |
public | property | The top object of a view. | 1 |
PluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
62 |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Clears a plugin. Overrides ViewsPluginInterface:: |
2 |
PluginBase:: |
protected | function | Do the work to filter out stored options depending on the defined options. | |
PluginBase:: |
public | function |
Filter out stored options depending on the defined options. Overrides ViewsPluginInterface:: |
|
PluginBase:: |
public | function |
Returns an array of available token replacements. Overrides ViewsPluginInterface:: |
|
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function |
Returns the plugin provider. Overrides ViewsPluginInterface:: |
|
PluginBase:: |
protected | function | Returns the render API renderer. | 1 |
PluginBase:: |
public | function |
Adds elements for available core tokens to a form. Overrides ViewsPluginInterface:: |
|
PluginBase:: |
public | function |
Returns a string with any core tokens replaced. Overrides ViewsPluginInterface:: |
|
PluginBase:: |
constant | Include entity row languages when listing languages. | ||
PluginBase:: |
constant | Include negotiated languages when listing languages. | ||
PluginBase:: |
public | function |
Initialize the plugin. Overrides ViewsPluginInterface:: |
8 |
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
protected | function | Makes an array of languages, optionally including special languages. | |
PluginBase:: |
public | function |
Return the human readable name of the display. Overrides ViewsPluginInterface:: |
|
PluginBase:: |
public static | function |
Moves form elements into fieldsets for presentation purposes. Overrides ViewsPluginInterface:: |
|
PluginBase:: |
public static | function |
Flattens the structure of form elements. Overrides ViewsPluginInterface:: |
|
PluginBase:: |
public static | function | Returns substitutions for Views queries for languages. | |
PluginBase:: |
protected | function | Fills up the options of the plugin with defaults. | |
PluginBase:: |
public | function |
Provide a full list of possible theme templates used by this style. Overrides ViewsPluginInterface:: |
1 |
PluginBase:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
6 |
PluginBase:: |
public | function |
Unpack options over our existing defaults, drilling down into arrays
so that defaults don't get totally blown away. Overrides ViewsPluginInterface:: |
|
PluginBase:: |
public | function |
Returns the usesOptions property. Overrides ViewsPluginInterface:: |
8 |
PluginBase:: |
public | function |
Validate that the plugin is correct and can be saved. Overrides ViewsPluginInterface:: |
6 |
PluginBase:: |
protected | function | Replaces Views' tokens in a given string. The resulting string will be sanitized with Xss::filterAdmin. | 1 |
PluginBase:: |
constant | Query string to indicate the site default language. | ||
PluginBase:: |
public | function |
Constructs a PluginBase object. Overrides PluginBase:: |
|
QueryPluginBase:: |
protected | property | Stores the limit of items that should be requested in the query. | |
QueryPluginBase:: |
public | property | A pager plugin that should be provided by the display. | |
QueryPluginBase:: |
public | function | Add a signature to the query, if such a thing is feasible. | 1 |
QueryPluginBase:: |
public | function | Let modules modify the query just prior to finalizing it. | 1 |
QueryPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides PluginBase:: |
1 |
QueryPluginBase:: |
public | function | Get aggregation info for group by queries. | 1 |
QueryPluginBase:: |
public | function |
The cache contexts associated with this object. Overrides CacheableDependencyInterface:: |
|
QueryPluginBase:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyInterface:: |
1 |
QueryPluginBase:: |
public | function |
The cache tags associated with this object. Overrides CacheableDependencyInterface:: |
1 |
QueryPluginBase:: |
public | function | Returns a Unix timestamp to database native timestamp expression. | 1 |
QueryPluginBase:: |
public | function | Creates cross-database date formatting. | 1 |
QueryPluginBase:: |
public | function | Returns an array of all tables from the query that map to an entity type. | |
QueryPluginBase:: |
public | function | Returns the limit of the query. | |
QueryPluginBase:: |
public | function | Get the timezone offset in seconds. | |
QueryPluginBase:: |
public | function | Loads all entities contained in the passed-in $results. . If the entity belongs to the base table, then it gets stored in $result->_entity. Otherwise, it gets stored in $result->_relationship_entities[$relationship_id]; | 1 |
QueryPluginBase:: |
public | function |
Generate a query and a countquery from all of the information supplied
to the object. Overrides PluginBase:: |
1 |
QueryPluginBase:: |
public | function | Applies a timezone offset to the given field. | 2 |
QueryPluginBase:: |
public | function | Control how all WHERE and HAVING groups are put together. | |
QueryPluginBase:: |
public | function | Set a LIMIT on the query, specifying a maximum number of results. | |
QueryPluginBase:: |
public | function | Set an OFFSET on the query, specifying a number of results to skip | |
QueryPluginBase:: |
public | function | Set the database to the current user timezone. | 1 |
QueryPluginBase:: |
public | function | Create a new grouping for the WHERE or HAVING clause. | |
QueryPluginBase:: |
public | function |
Handle any special handling on the validate form. Overrides PluginBase:: |
1 |
QueryPluginBase:: |
public | function |
Returns the summary of the settings in the display. Overrides PluginBase:: |
|
QueryPluginBase:: |
public | function |
Validate the options form. Overrides PluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. |