Commit 406234de authored by Côme Chilliet's avatar Côme Chilliet

feat(simpleplugin) Improve ObjectSelectAttribute

Make sure ObjectAttribute, ObjectsAttribute and ObjectSelectAttribute
 behave consistently, making it easier to switch from one to the other.

issue #6083
parent 8d558d6f
......@@ -30,14 +30,16 @@
class ObjectSelectAttribute extends SelectAttribute
{
protected $objectType;
protected $objectAttrs = NULL;
protected $storeAttr;
protected $displayAttr;
protected $objectFilter = '';
function __construct ($label, $description, $ldapName, $required, $objectType, $objectAttrs = NULL, $objectFilter = '', $acl = "")
function __construct (string $label, string $description, string $ldapName, bool $required, array $objectTypes, string $storeAttr = 'dn', string $displayAttr = 'nameAttr', $objectFilter = '', $acl = '')
{
parent::__construct($label, $description, $ldapName, $required, [], "", NULL, $acl);
$this->objectType = $objectType;
$this->objectAttrs = $objectAttrs;
parent::__construct($label, $description, $ldapName, $required, [], '', NULL, $acl);
$this->objectTypes = $objectTypes;
$this->storeAttr = $storeAttr;
$this->displayAttr = $displayAttr;
$this->objectFilter = $objectFilter;
}
......@@ -51,7 +53,30 @@ class ObjectSelectAttribute extends SelectAttribute
function updateChoices ()
{
$objects = objects::ls($this->objectType, $this->objectAttrs, NULL, $this->objectFilter);
$this->setChoices(array_keys($objects), array_values($objects));
$choices = [];
$outputs = [];
foreach ($this->objectTypes as $objectType) {
try {
$infos = objects::infos($this->objectType);
$realStoreAttr = $this->storeAttr;
if (in_array($this->storeAttr, ['nameAttr','mainAttr'])) {
$realStoreAttr = $infos[$this->storeAttr];
}
$realDisplayAttr = $this->displayAttr;
if (in_array($this->displayAttr, ['nameAttr','mainAttr'])) {
$realDisplayAttr = $infos[$this->displayAttr];
}
$attributes = [
$realStoreAttr => (($realStoreAttr == 'dn') ? 'raw' : '1'),
$realDisplayAttr => (($realDisplayAttr == 'dn') ? 'raw' : '1'),
];
$objects = objects::ls($objectType, $attributes, NULL, $this->objectFilter);
array_push($choices, ...array_column($objects, $realStoreAttr));
array_push($outputs, ...array_column($objects, $realDisplayAttr));
} catch (NonExistingObjectTypeException $e) {
continue;
}
}
$this->setChoices($choices, $outputs);
}
}
......@@ -24,13 +24,14 @@
class ObjectAttribute extends DialogButtonAttribute
{
protected $objectType;
protected $storeAttr;
protected $displayAttr;
function __construct ($label, $description, $ldapName, $required, $objectType, $defaultValue = '', $acl = '')
function __construct (string $label, string $description, string $ldapName, bool $required, string $objectType, string $storeAttr = 'dn', string $displayAttr = 'nameAttr', $defaultValue = '', $acl = '')
{
parent::__construct($label, $description, $ldapName, '', 'GenericSingleSelectManagementDialog', $defaultValue, $acl);
$this->setRequired($required);
$this->objectType = $objectType;
$this->objectType = $objectType;
}
public function getSelectManagementParameters (): array
......@@ -42,12 +43,21 @@ class ObjectAttribute extends DialogButtonAttribute
{
parent::setParent($plugin);
$infos = objects::infos($this->objectType);
$this->displayAttr = $infos['nameAttr'];
if (in_array($this->storeAttr, ['nameAttr','mainAttr'])) {
$this->storeAttr = $infos[$this->storeAttr];
}
if (in_array($this->displayAttr, ['nameAttr','mainAttr'])) {
$this->displayAttr = $infos[$this->displayAttr];
}
}
function handleDialogResult ($dn, $entry)
{
$this->setValue($dn);
$value = $entry[$this->storeAttr];
if (is_array($value)) {
$value = $value[0];
}
$this->setValue($value);
$this->buttonText = $entry[$this->displayAttr];
if (is_array($this->buttonText)) {
......
......@@ -23,8 +23,8 @@
*/
class UserAttribute extends ObjectAttribute
{
function __construct ($label, $description, $ldapName, $required, $defaultValue = '', $acl = '')
function __construct (string $label, string $description, string $ldapName, bool $required, string $storeAttr = 'dn', string $displayAttr = 'nameAttr', $defaultValue = '', $acl = '')
{
parent::__construct($label, $description, $ldapName, $required, 'user', $defaultValue = '', $acl = '');
parent::__construct($label, $description, $ldapName, $required, 'user', $storeAttr, $displayAttr, $defaultValue, $acl);
}
}
  • SonarQube analysis reported 3 issues

    • 2 major
    • 1 info

    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. Remove this commented out code. 📘
    2. Update this method so that its implementation is not identical to "getFilterBlackList" on line 41. 📘
    3. Remove the unused function parameter "$simplePlugin". 📘
Markdown is supported
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