Duplicate UID always generated in generate_uid.

Louis Carreiro quantumschema at outlook.com
Mon Oct 7 15:24:44 CEST 2013


Hey all!

So I'm doing a new implementation of Kolab and during setup, I wanted to change the UID being generated from just the lastname ("sn") to first name dot last name ("givenname"."sn").

In the process of going over the generate_uid function, I noticed that it is acting a bit funny and always allowing for a duplicate UID to be generated, regardless of changing the how the UID is formed. In case anyone was to duplicate it, I've already got a user with a UID of "test.user" in the 389 server. And a bit more information, this is a full install (all packages) of Kolab 3.0 on CentOS and the kolab-webadmin version is 3.0.4-1.el6.kolab_3.0.

Here's what my generate_uid function looks like:

private function generate_uid($postdata, $attribs = array())
{

if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['uid'])) {
 // Use Data Please
 foreach ($attribs['auto_form_fields']['uid']['data'] as $key) {
 if (!isset($postdata[$key])) {
 throw new Exception("Key not set: " . $key, 12356);
 }
 }

// TODO: Use preferredlanguage
if (isset($postdata['preferredlanguage'])) {
 //console("Using locale for " . $postdata['preferredlanguage']);
 setlocale(LC_ALL, $postdata['preferredlanguage']);
 }
/* else {
 //console("No locale specified...!");
 }
*/

/*
 $uid = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);
 $uid = strtolower($uid);
 $uid = preg_replace('/[^a-z-_]/i', '', $uid);
*/

$firstName = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['givenname']);
 $firstName = strtolower($firstName);
 $firstName = preg_replace('/[^a-z-_]/i', '', $firstName);
 $lastName = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);
 $lastName = strtolower($lastName);
 $lastName = preg_replace('/[^a-z-_]/i', '', $lastName);

if ($firstName && $lastName) {
 $uid = $firstName . '.' . $lastName;
 } elseif ($firstName && !$lastName) {
 $uid = $firstName;
 } elseif (!$firstName && $lastName) {
 $uid = $lastName;
 };

console("Before While Loop: " . $uid);

$orig_uid = $uid;
 $auth = Auth::get_instance($_SESSION['user']->get_domain());
 $unique_attr = $this->unique_attribute();
 $testArray = $auth->user_find_by_attribute(array('uid' => $uid));
 console("Count of array: " . count($testArray));

$x = 2;

while (($user_found = $auth->user_find_by_attribute(array('uid' => $uid)))) {
 if (!empty($postdata['id'])) {
 $user_found_dn = key($user_found);
 $user_found_unique_attr = $auth->get_entry_attribute($user_found_dn, $unique_attr);
 console("user with uid $uid found", $user_found_unique_attr);
 if ($user_found_unique_attr == $postdata['id']) {
 console("that's us.");
 break;
 }
 }
 $uid = $orig_uid . $x;
 console("Still in while loop: " . $uid);
 $x++;
 }

console("Returned uid: " . $uid);
 return $uid;
 }
 }

And here's what the console shows:

//----------- First name entered --------------
[07-Oct-2013 09:03:29 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Before While Loop: test
[07-Oct-2013 09:03:29 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Count of array: 1
[07-Oct-2013 09:03:29 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Returned uid: test
[07-Oct-2013 09:03:29 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Before While Loop: test
[07-Oct-2013 09:03:29 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Count of array: 1
[07-Oct-2013 09:03:29 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Returned uid: test
[07-Oct-2013 09:03:29 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Before While Loop: test
[07-Oct-2013 09:03:29 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Count of array: 1
[07-Oct-2013 09:03:29 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Returned uid: test
//---------- Last name entered ---------------
[07-Oct-2013 09:03:35 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Before While Loop: test.user
[07-Oct-2013 09:03:35 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Count of array: 1
[07-Oct-2013 09:03:35 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Still in while loop: test.user2
[07-Oct-2013 09:03:35 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Returned uid: test.user2
[07-Oct-2013 09:03:35 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Before While Loop: test.user
[07-Oct-2013 09:03:35 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Count of array: 1
[07-Oct-2013 09:03:35 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Returned uid: test.user
[07-Oct-2013 09:03:35 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Before While Loop: test.user
[07-Oct-2013 09:03:35 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Count of array: 1
[07-Oct-2013 09:03:35 -0400](77c0k7hp0qu73ba1jjj5c1kon0): Returned uid: test.user

I guess I understand why the function could be used over and over like that but what I don't get is why the second and third time through, the while loop doesn't answer true when it did the first time. The $uid variable is constructed off of the postdata['givenname'] and postdata['sn'] values each time through so it should match to the existing user "test.user" each time. I'm completely puzzled on why the while loop isn't true on the subsequent tries. I'd love to take a look at the contents of the "$auth->user_find_by_attribute(array('uid' => $uid)" array but it I'm not sure how and its odd the count is always 1. 
Any thoughts?

v/r,
Louis 		 	   		  


More information about the users mailing list