feat(simpleplugin) Improve ObjectsAttribute

issue #6083
parent e2ed8f86
......@@ -31,19 +31,45 @@ class ObjectsAttribute extends GenericDialogAttribute
protected $selectManagementParameters;
protected $filterElementDefinitions;
function __construct (string $label, string $description, string $ldapName, bool $required, array $objectTypes, array $defaultValue = [], string $store_attr = 'dn', string $display_attr = 'cn', array $filterElementDefinitions = NULL, string $acl = '')
function __construct (string $label, string $description, string $ldapName, bool $required, array $objectTypes, array $defaultValue = [], string $store_attr = 'dn', string $display_attr = 'nameAttr', array $filterElementDefinitions = NULL, string $acl = '')
{
parent::__construct($label, $description, $ldapName, $required, $defaultValue, $store_attr, $display_attr, $acl);
$this->selectManagementParameters = [
$objectTypes,
TRUE,
[
'objectClass' => '*',
$store_attr => (($store_attr == 'dn') ? 'raw' : '*'),
$display_attr => (($display_attr == 'dn') ? 'raw' : '*'),
]
$attributes = [
'objectClass' => '*',
'dn' => 'raw',
];
$this->filterElementDefinitions = $filterElementDefinitions;
if (!in_array($store_attr, ['dn','nameAttr','mainAttr'])) {
$attributes[$store_attr] = '*';
}
if (!in_array($display_attr, ['dn','nameAttr','mainAttr'])) {
$attributes[$display_attr] = '*';
}
$specialAttributes = array_intersect([$store_attr,$display_attr], ['nameAttr','mainAttr']);
foreach ($objectTypes as $type) {
try {
if (!empty($specialAttributes)) {
$infos = objects::infos($type);
foreach ($specialAttributes as $attribute) {
$attributes[$infos[$attribute]] = '*';
}
}
$filterAttributes = objects::getFilterObject($type)->listUsedAttributes();
foreach ($filterAttributes as $attribute) {
if ($attribute != 'dn') {
$attributes[$attribute] = '*';
}
}
} catch (NonExistingObjectTypeException $e) {
continue;
}
}
$this->selectManagementParameters = [$objectTypes,TRUE,$attributes];
$this->filterElementDefinitions = $filterElementDefinitions;
}
protected function ldapAttributesToGet (): array
{
return array_keys($this->selectManagementParameters[2]);
}
public function getSelectManagementParameters (): array
......@@ -60,4 +86,41 @@ class ObjectsAttribute extends GenericDialogAttribute
}
return $parameters;
}
protected function fillDisplayValueFrom ($i, $attrs)
{
if ($attrs) {
$objectType = NULL;
if (is_array($attrs)) {
foreach ($this->selectManagementParameters[0] as $type) {
try {
if (objects::isOfType($attrs, $type)) {
$objectType = $type;
}
} catch (NonExistingObjectTypeException $e) {
continue;
}
}
} else {
$objectType = $attrs->getTemplatedType();
}
if ($objectType !== NULL) {
$infos = objects::infos($objectType);
if (in_array($this->display_attr, ['nameAttr','mainAttr'])) {
$display = $attrs[$infos[$this->display_attr]][0];
} else {
$display = $attrs[$this->display_attr][0];
}
$this->displays[$i] = sprintf(_('%s (%s)'), trim($display), $infos['name']);
}
if (!isset($this->displays[$i])) {
trigger_error('Unkown type for "'.$this->value[$i].'"');
$this->displays[$i] = sprintf(_('Unknown type : %s'), $this->value[$i]);
}
} elseif (($attrs === NULL) && $this->isTemplate()) {
$this->displays[$i] = $this->value[$i];
} else {
$this->displays[$i] = sprintf(_('Non existing dn: %s'), $this->value[$i]);
}
}
}
......@@ -25,40 +25,8 @@
*/
class UsersGroupsRolesAttribute extends ObjectsAttribute
{
function __construct (string $label, string $description, string $ldapName, bool $required = FALSE, array $defaultValue = [], string $store_attr = 'dn', string $display_attr = 'cn', array $filterElementDefinitions = NULL, string $acl = '')
function __construct (string $label, string $description, string $ldapName, bool $required = FALSE, array $defaultValue = [], string $store_attr = 'dn', string $display_attr = 'nameAttr', array $filterElementDefinitions = NULL, string $acl = '')
{
parent::__construct($label, $description, $ldapName, $required, ['user', 'group', 'role', 'ogroup'], $defaultValue, $store_attr, $display_attr, $filterElementDefinitions, $acl);
$this->selectManagementParameters[2]['cn'] = '*';
$this->selectManagementParameters[2]['uid'] = '*';
$this->selectManagementParameters[2]['objectClass'] = '*';
}
protected function ldapAttributesToGet ()
{
return array_values(array_unique([$this->store_attr, $this->display_attr, 'cn', 'uid', 'objectClass']));
}
protected function fillDisplayValueFrom ($i, $attrs)
{
if ($this->isTemplate() && ($attrs === NULL)) {
$this->displays[$i] = $this->value[$i];
} elseif (!isset($attrs[$this->display_attr])) {
unset($this->value[$i]);
} else {
$cn = $attrs['cn'][0];
if ($this->display_attr == 'dn') {
$this->displays[$i] = $attrs['dn'];
} elseif (in_array('posixGroup', $attrs['objectClass'])) {
$this->displays[$i] = sprintf(_('POSIX group %s'), trim($cn));
} elseif (in_array('organizationalRole', $attrs['objectClass'])) {
$this->displays[$i] = sprintf(_('Role %s'), trim($cn));
} elseif (in_array('groupOfNames', $attrs['objectClass'])) {
$this->displays[$i] = sprintf(_('Group %s'), trim($cn));
} elseif (isset($attrs['uid'])) {
$this->displays[$i] = trim($attrs['uid'][0]);
} else {
$this->displays[$i] = $attrs[$this->display_attr][0];
}
}
}
}
......@@ -3,7 +3,7 @@
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003 Cajus Pollmeier
Copyright (C) 2011-2019 FusionDirectory
Copyright (C) 2011-2020 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
......@@ -40,13 +40,13 @@ class GroupMembersAttribute extends ObjectsAttribute
{
if ($attrs) {
$code = FALSE;
$objectType = '';
$objectType = NULL;
if (is_array($attrs)) {
foreach (ogroup::$objectTypes as $key => $value) {
foreach (ogroup::$objectTypes as $key => $type) {
try {
if (objects::isOfType($attrs, $value)) {
if (objects::isOfType($attrs, $type)) {
$code = $key;
$objectType = $value;
$objectType = $type;
}
} catch (NonExistingObjectTypeException $e) {
continue;
......@@ -58,31 +58,25 @@ class GroupMembersAttribute extends ObjectsAttribute
}
if ($code !== FALSE) {
$infos = objects::infos($objectType);
$this->types[$i] = $code;
$this->displays[$i] = $attrs[$infos['nameAttr']][0];
if (strtolower($objectType) == 'user') {
if (isset($attrs['description'][0])) {
$this->displays[$i] .= ' ['.$attrs['uid'][0].' - '.$attrs['description'][0].']';
} else {
$this->displays[$i] .= ' ['.$attrs['uid'][0].']';
}
} elseif (isset($attrs['description'][0])) {
$this->displays[$i] .= ' ['.$attrs['description'][0].']';
if (in_array($this->display_attr, ['nameAttr','mainAttr'])) {
$display = $attrs[$infos[$this->display_attr]][0];
} else {
$display = $attrs[$this->display_attr][0];
}
$this->displays[$i] = sprintf(_('%s (%s)'), trim($display), $infos['name']);
$this->types[$i] = $code;
}
if (!isset($this->displays[$i])) {
trigger_error('Unkown type for "'.$this->value[$i].'"');
$this->displays[$i] = sprintf(_('Unknown type : %s'), $this->value[$i]);
$this->types[$i] = 'I';
}
} elseif (($attrs === NULL) && $this->isTemplate()) {
$this->displays[$i] = $this->value[$i];
$this->types[$i] = 'I';
} else {
if (($attrs === NULL) && $this->isTemplate()) {
$this->displays[$i] = $this->value[$i];
$this->types[$i] = 'I';
} else {
$this->displays[$i] = sprintf(_('Non existing dn: %s'), $this->value[$i]);
$this->types[$i] = 'I';
}
$this->displays[$i] = sprintf(_('Non existing dn: %s'), $this->value[$i]);
$this->types[$i] = 'I';
}
}
......@@ -117,19 +111,6 @@ class GroupMembersAttribute extends ObjectsAttribute
return $display;
}
function ldapAttributesToGet ()
{
return [
'objectClass',
'cn',
'sn',
'ou',
'uid',
'givenName',
'description',
];
}
function listObjectTypes ()
{
/* Refresh types and displays */
......
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