Commit bca8a84a authored by Côme Chilliet's avatar Côme Chilliet
Browse files

:sparkles: feat(simpleplugin) Use as many attributes as needed to build a unique DN

issue #5817
Showing with 106 additions and 9 deletions
+106 -9
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2018 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!brief Iterator that returns all combinations of $size element from $input array
*/
class Combinations implements Iterator
{
protected $current = NULL;
protected $input = NULL;
protected $n = 0;
protected $size = 0;
protected $pos = 0;
function __construct($input, $size)
{
$this->input = array_values($input);
$this->n = count($this->input);
$this->size = $size;
$this->rewind();
}
function key()
{
return $this->pos;
}
function current()
{
$r = array();
for ($i = 0; $i < $this->size; $i++) {
$r[] = $this->input[$this->current[$i]];
}
return $r;
}
function next()
{
if ($this->_next()) {
$this->pos++;
} else {
$this->pos = -1;
}
}
function rewind()
{
$this->current = range(0, $this->size);
$this->pos = 0;
}
function valid()
{
return ($this->pos >= 0);
}
protected function _next()
{
$i = $this->size - 1;
while (($i >= 0) && ($this->current[$i] == $this->n - $this->size + $i)) {
$i--;
}
if ($i < 0) {
return FALSE;
}
$this->current[$i]++;
while ($i++ < $this->size - 1) {
$this->current[$i] = $this->current[$i - 1] + 1;
}
return TRUE;
}
}
...@@ -1813,19 +1813,28 @@ class simplePlugin ...@@ -1813,19 +1813,28 @@ class simplePlugin
return $dn; return $dn;
} }
/* Look for additional attributes */ /* Build DN with multiple attributes */
$usableAttributes = array();
foreach ($this->attributes as $attr) { foreach ($this->attributes as $attr) {
if (($attr == $attribute) || ($this->$attr == '') || is_array($this->$attr)) { if (($attr == $attribute) || ($this->$attr == '') || is_array($this->$attr)) {
continue; continue;
} }
$usableAttributes[] = $attr;
$dn = $attribute.'='.ldap_escape_dn($this->$attribute).'+'.$attr.'='.ldap_escape_dn($this->$attr).','.$base; }
if ($dn == $this->orig_dn) { for ($i = 2; $i < count($usableAttributes); $i++) {
return $dn; foreach (new Combinations($usableAttributes, $i) as $attrs) {
} $dn = $attribute.'='.ldap_escape_dn($this->$attribute);
$ldap->cat($dn, array('dn')); foreach ($attrs as $attr) {
if (!$ldap->fetch()) { $dn .= '+'.$attr.'='.ldap_escape_dn($this->$attr);
return $dn; }
$dn .= ','.$base;
if ($dn == $this->orig_dn) {
return $dn;
}
$ldap->cat($dn, array('dn'));
if (!$ldap->fetch()) {
return $dn;
}
} }
} }
......
  • SonarQube analysis reported 1 issue

    • :no_entry_sign: 1 critical

    Note: The following issues were found on lines that were not modified in the commit. Because these issues can't be reported as line comments, they are summarized here:

    1. :no_entry_sign: Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed. :blue_book:

    By Ghost User on 2018-04-17T15:38:09 (imported from GitLab)

  • bmortier @bmortier

    mentioned in commit 855c6d20

    By Côme Chilliet on 2018-04-25T09:27:26 (imported from GitLab)

    ·

    mentioned in commit 855c6d20

    By Côme Chilliet on 2018-04-25T09:27:26 (imported from GitLab)

    Toggle commit list
  • bmortier @bmortier

    mentioned in merge request !249

    By Côme Chilliet on 2018-04-25T09:27:45 (imported from GitLab)

    ·

    mentioned in merge request !249

    By Côme Chilliet on 2018-04-25T09:27:45 (imported from GitLab)

    Toggle commit list
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment