LdapRecord icon indicating copy to clipboard operation
LdapRecord copied to clipboard

[Support] mutate values when retrieved via getAttributeValue

Open zoriax opened this issue 5 years ago • 5 comments

Hi @stevebauman,

I'm looking for a way to preserver attributes when they are set in select() method. For exemple I need to retrieve attributes even if they are null or not set. For exemple

$this->select(['dn', 'extensionattribute2'])->paginate()

Actually, I have only the dn if extensionattribute2 is not set, but what I whant is the key => value (null) in my array.

I there a way to do that or is it an Ldap/ActiveDirectory limitation ? It could be really userfull to have this feature.

Thanks

zoriax avatar Jun 05 '20 13:06 zoriax

Hi @zoriax,

Unfortunately no. If the attribute is not set in your LDAP server, it is not returned even with it being selected via an ldap_search request.

I would suggest simply merging the attributes with default ones you need:

$user = User::findByAnr('cn', 'sbauman');

$default = [
    'extensionattribute2' => null,
];

$attributes = array_merge($default, $user->getAttributes());

Also, using the getAttribute() method will return null when the attribute does not exist, so you can use the PHP null coalesce operator to return a default value if you need:

$extensionAttribute = $user->getAttribute('extensionattribute2') ?? 'default';

// Or:

$extensionAttribute = $user->extensionattribute2 ?? 'default';

I hope this helps!

stevebauman avatar Jun 05 '20 13:06 stevebauman

Thanks @stevebauman,

Yes it's what I think about.... but with getAttribute() method, I loos the defautl conversion of atttributesToArray(), especially for dates...

If I do this like that, my lastlogontimestamp is an instance of Carbon and not the date as string... It sould be greate if getAttributeValue() returns the value as expecteed (forr exemple a date or the converted guid, ...)

For exemple

$this->getAttribute('objectguid') --> retruns the guid in raw format
$this->getAttributeValue('objectguid') --> retruns the guid in human readable format
$this->objectguid --> here what you want :-) or specified in config

zoriax avatar Jun 05 '20 13:06 zoriax

The getAttributeValue() does get date values as Carbon instances:

https://github.com/DirectoryTree/LdapRecord/blob/bf376762c10c51ef301bee64a0fdd1907d7ace44/src/Models/Concerns/HasAttributes.php#L235-L249

I'm not sure what your true objective is -- can you walk me through a full code example of what you would like to occur and the expected output?

stevebauman avatar Jun 05 '20 15:06 stevebauman

Hi @stevebauman

Thanks for your return. I'll try to explain a bit more what I'm looking for.

For exemple I search for one user :

$user = $this->select(['objectguid', 'name', 'memberof']))->first(1);

With $user->getAttribute('objectguid') I have as expected an array like that

[
  0 => b"«°îÆ3/àL¾Ô¿¿è\x1Er”"
]

With $user->getAttributeValue('objectguid') or $this->objectguid, same as above. For my undestanding, it could be more usefull to have the "translated" value :-)

So for exemple with : $user->getAttribute('attributename') : You should return the raw content (array of values)

$user->getAttributeValue('attributename', ...$options) : You should return the "translated" content of attribute and if options are set you can return the position of array (0 for exemple) and as second option you should do something, for exemple date format for attribute date or json for memberof...

$user->attributename : You should return what you want, raw or translated or only position 0 of array :-)

I know it's maybe not possible but it could be a must have in a future release :-). Actually I do it with accessors and set my own logic.

I hope it's more clear ! Thanks

zoriax avatar Jun 10 '20 11:06 zoriax

Hi @zoriax,

I've thought about this and I think you're right -- it should work like this and I think its a good change 👍

I'm keeping this open for implementation in LdapRecord v2 since it's a breaking change. Once it's fully implemented, I will close this issue.

Thanks for putting so much detail into your responses and helping me understand!

stevebauman avatar Jun 22 '20 01:06 stevebauman