Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
2.4k views
in Q2A Core by

Hi,

I'm trying to set up q2a connected to active directory with sso.

I currently work on the file get-external-users.php and completing the necessary functions. There wasn't a problem with most of the functions, but I have a problem with qa_get_userids_from_public().

In my AD we use textual identifyers to identify each user. (lets say its something like 'abc1234'). We also use a display name, which is usualy the person's full name.

So, I'm using the textual identifiers as userid (abc1234) and the full name as display name - public user name ('Michael John').

The problem is:

I cant get userid from public because the db might have more than one Michael John!

 

how can I solve this? I dont want to do something ugly like setting the public name as "Michael John (abc1234)".

 

Any suggestions? enlightened

Please help!

3 Answers

+1 vote
by

From a community perspective isn't there a problem if two users can have the same visible username? It means that two people on the site will appear to others as if they're the same person.

Anyway, the answer is that you should just return the first user that matches. It will cause the following problems:

  • When viewing a user profile page, it will show the user profile for that first user only, and the profile for the second user won't be visible.
  • When searching for posts by a user, by entering that user's name, it will find posts by that first user only.

Perhaps this will be good enough for you?

by
Thanks for the answer! but I fount another solution.
We have two people with the same name because the display name is their full name - so there will be always more than one person per name.
Eventually, I've added the userid in brackets as a prefix to the full name, and i've also changed the method that returns the html name, and removed the id from there. So all the links are just fine and it's exactly as I wanted it :)

Thanks!
by
Hello Arieljannai,

I have same problem with my SSO integration, I am not a developer but I have enough understanding to modify existing code. Is it possible for you to share code? may be you can share generic code for this problem. It will be great help.
+2 votes
by

I modified " function qa_get_public_from_userids($userids)" to get username & userid information in format of {username}[userid]

here is code :



while ($result=mysql_fetch_assoc($results))
                        {
                            $test1 = $result['first_name'];
                            $test2 = $result['user_id'];
                            $lastname= $result['last_name'];
                            $test= '{'.$test1." ".$lastname.'}['.$test2.']';
                $useridtopublic[$result['user_id']]=$test;
            }



So my result will be "{Shivraj Sawant}[shivrajsa]".

Then I modified function "function qa_get_users_html($userids, $should_include_link, $relative_url_prefix)". I used some string manipulation functions to separate username & userid

Here is my code :



        foreach ($userids as $userid) {
                       $publicusername=$useridtopublic[$userid];
                       $nametest= strpbrk($publicusername,"[");
                       $nametest=trim($nametest,"[]");
                       $test3='['.$nametest.']';
                       $Pnametest= chop($publicusername,$test3);
                       $Pnametest=trim($Pnametest,"{}");
                       $publicusername=$nametest;
                       $publicusername1=$Pnametest;
            
            $usershtml[$userid]=htmlspecialchars($publicusername1);
            
            if ($should_include_link)
                $usershtml[$userid]='<a href="'.qa_path_html('user/'.$publicusername).'" class="qa-user-link">'.$usershtml[$userid].'</a>';
        }
            
        return $usershtml;



So "$publicusername1" contains the username(Shivraj Sawant) which will be stroed in "$usershtml" & "$publicusername" contains userid(shivrajsa).

Then I modified "function qa_get_userids_from_public($publicusernames)".
Below is my code :



           if (count($publicusernames)) {
           $qa_db_connection=qa_db_connection();
           $escapedusernames=array();
           
    foreach ($publicusernames as $publicusername)
              {
                 $escapedusernames[]="'".mysql_real_escape_string($publicusername, $qa_db_connection)."'";
              }
            
$results=mysql_query(
                'SELECT first_name, user_id FROM user WHERE user_id IN ('.implode(',', $escapedusernames).')',
                $qa_db_connection
            );
            
while ($result=mysql_fetch_assoc($results))
      {
       $publictouserid[$result['first_name']]=$result['user_id'];
       }
    }
        
return $publictouserid;

  


 
My code is working, correct userid is fetched even if there are many users with same user name.

One thing may be considered that username should not contain "{" or "]" character for given code.
I am not a developement expert,  it is just a way I tried & it is working, there could be more optimistic way to do it

0 votes
by
edited by

You could also change the path for the links from using the username into using directly their userid

like so:

function qa_get_logged_in_user_html($logged_in_user, $relative_url_prefix)

{

$publicusername = $logged_in_user['publicusername'];
        $userid = $logged_in_user['userid'];
        
        return '<a href="'.qa_path_html('user/'.$userid).'" class="qa-user-link">'.htmlspecialchars($publicusername).'</a>';

}

 

function qa_get_users_html($userids, $should_include_link, $relative_url_prefix)
{

        $useridtopublic=qa_get_public_from_userids($userids);
        
        $usershtml=array();

        foreach ($userids as $userid) {
            $publicusername=$useridtopublic[$userid];
            
            $usershtml[$userid]=htmlspecialchars($publicusername);
            
            if ($should_include_link)
                $usershtml[$userid]='<a href="'.qa_path_html('user/'.$userid).'" class="qa-user-link">'.$usershtml[$userid].'</a>';
        }
            
        return $usershtml;

}

 

DONT FORGET TO MAKE THE CHANGE IN

function qa_get_userids_from_public($userids)
{
      //AND QUERY FROM THE IDs AND NOT FROM THE USERNAMES

    $useridtopublic=array();
        
        if (count($userids)) {
            
            $escapeduserids=array();
            foreach ($userids as $userid)
                $escapeduserids[]="'".DB::getInstance()->escape($userid)."'";
            
            $sql = "SELECT user_id, nickname
                      FROM user
                     WHERE user_id IN (".implode(',', $escapeduserids).")";
            $result = DB::getInstance()->query($sql);
            
            while($row = $result->fetch_object())
            {    
                $publictouserid[$row->user_id] = $row->user_id;
            }
        }
        
        return $publictouserid;
    }

 

 

by
Thank you very much for your guidance.
...