Implemented attribute sorting with configuration to determine sort order

This commit is contained in:
Deon George 2023-03-02 10:17:15 +11:00
parent 4767eb3a4f
commit e0fb057c84
4 changed files with 117 additions and 119 deletions

View File

@ -20,7 +20,33 @@ class Entry extends Model
$result->put($attribute,Factory::create($attribute,$value));
}
return $result->toArray();
$sort = collect(config('ldap.attr_display_order',[]))->transform(function($item) { return strtolower($item); });
// Order the attributes
return $result->sortBy([function($a,$b) use ($sort) {
if (! $sort->count() || $a === $b)
return 0;
// Check if $a/$b are in the configuration to be sorted first, if so get it's key
$a_key = $sort->search($a->name_lc);
$b_key = $sort->search($b->name_lc);
// If the keys were not in the sort list, set the key to be the count of elements (ie: so it is last to be sorted)
if ($a_key === FALSE)
$a_key = $sort->count()+1;
if ($b_key === FALSE)
$b_key = $sort->count()+1;
// Case where neither $a, nor $b are in ldap.attr_display_order, $a_key = $b_key = one greater than num elements.
// So we sort them alphabetically
if ($a_key === $b_key)
return strcasecmp($a->name,$b->name);
// Case where at least one attribute or its friendly name is in $attrs_display_order
// return -1 if $a before $b in $attrs_display_order
return ($a_key < $b_key) ? -1 : 1;
} ])->toArray();
}
/* ATTRIBUTES */

View File

@ -248,29 +248,6 @@ $config->custom->appearance['friendly_attrs'] = array(
// $config->custom->modify_member['posixfilter'] = '(uid=*)';
// $config->custom->modify_member['posixgroupattr'] = 'memberUid';
/*********************************************
* Support for attrs display order *
*********************************************/
/* Use this array if you want to have your attributes displayed in a specific
order. You can use default attribute names or their fridenly names.
For example, "sn" will be displayed right after "givenName". All the other
attributes that are not specified in this array will be displayed after in
alphabetical order. */
// $config->custom->appearance['attr_display_order'] = array();
# $config->custom->appearance['attr_display_order'] = array(
# 'givenName',
# 'sn',
# 'cn',
# 'displayName',
# 'uid',
# 'uidNumber',
# 'gidNumber',
# 'homeDirectory',
# 'mail',
# 'userPassword'
# );
/*********************************************
* Define your LDAP servers in this section *
*********************************************/

View File

@ -72,4 +72,34 @@ return [
'time' => env('LDAP_CACHE_TIME',5*60), // Seconds
],
/*
|--------------------------------------------------------------------------
| Support for attrs display order
|--------------------------------------------------------------------------
|
| Use this array if you want to have your attributes displayed in a specific
| order. Case is not important.
|
| For example, "sn" will be displayed right after "givenName". All the other
| attributes that are not specified in this array will be displayed after in
| alphabetical order.
|
*/
'attr_display_order' => [],
/*
'attr_display_order' => [
'givenName',
'sn',
'cn',
'displayName',
'uid',
'uidNumber',
'gidNumber',
'homeDirectory',
'mail',
'userPassword'
],
*/
];

View File

@ -2658,41 +2658,6 @@ function pla_reverse_dn($dn) {
return (implode(',',array_reverse(pla_explode_dn($dn))));
}
/**
* Attribute sorting
*/
function sortAttrs($a,$b) {
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
debug_log('Entered (%%)',1,0,__FILE__,__LINE__,__METHOD__,$fargs);
if ($a == $b)
return 0;
$server = $_SESSION[APPCONFIG]->getServer(get_request('server_id','REQUEST'));
$attrs_display_order = arrayLower($_SESSION[APPCONFIG]->getValue('appearance','attr_display_order'));
# Check if $a is in $attrs_display_order, get its key
$a_key = array_search($a->getName(),$attrs_display_order);
$b_key = array_search($b->getName(),$attrs_display_order);
if ((! $a_key) && ($a_key !== 0))
if ((! $a_key = array_search(strtolower($a->getFriendlyName()),$attrs_display_order)) && ($a_key !== 0))
$a_key = count($attrs_display_order)+1;
if ((! $b_key) && ($b_key !== 0))
if ((! $b_key = array_search(strtolower($b->getFriendlyName()),$attrs_display_order)) && ($b_key !== 0))
$b_key = count($attrs_display_order)+1;
# Case where neither $a, nor $b are in $attrs_display_order, $a_key = $b_key = one greater than num elements.
# So we sort them alphabetically
if ($a_key === $b_key)
return strcasecmp($a->getFriendlyName(),$b->getFriendlyName());
# Case where at least one attribute or its friendly name is in $attrs_display_order
# return -1 if $a before $b in $attrs_display_order
return ($a_key < $b_key) ? -1 : 1;
}
/**
* Reads an array and returns the array values back in lower case
*