You have to directly modify PHPBB so remember this in the future if you do any upgrades.
For this to work I created a custom profile field called display_name (you can change this if you wish), this is where a user can modify there name.
Now the bit you need to modify.
Find the file functions_content.php (its in the includes directory).
On line 1355 there is a function called get_username_string.
Add the following bit of code after the global bit.
global $phpbb_container;
if(!empty($user_id))
{
$cp = $phpbb_container->get('profilefields.manager');
$userProfileFields = $cp->grab_profile_fields_data($user_id);
if(!empty($userProfileFields[$user_id]["display_name"]["value"]))
{
$username = $userProfileFields[$user_id]["display_name"]["value"];
}
}
This basically replaces the username on most pages with the one in display_name if it is set.
Awesome this did the trick!!! Screw F'ing with templates!
ReplyDeleteWe have LDAP enable on our Active Directory so I expanded on the example you posted.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete// replace username with custom field displayname START
ReplyDeleteif(!empty($user_id))
{
$cp = $phpbb_container->get('profilefields.manager');
$userProfileFields = $cp->grab_profile_fields_data($user_id);
if(!empty($userProfileFields[$user_id]["displayname"]["value"]))
{
$username = $userProfileFields[$user_id]["displayname"]["value"];
}
else
{
$username = getldapname($username);
if (!empty($username)) {
$cp_data = array('pf_displayname' => $username);
$cp->update_profile_field_data($user_id, $cp_data);
}
}
}
// replace username with custom field displayname END
FYI, I used displayname instead of display_name.
ReplyDeleteThen added a function to pull the canonical name or cn from AD via LDAP.
function getldapname($username) {
ReplyDelete$ldapDomian = 'DC=domain,DC=com';
$ntDomain = 'LOCAL.COM\\';
// $adServer is holds the AD SERVER address running
// LDAP directory service this can also be an IP address
$adServer = "ldap://domaincontroller.local.com";
// This is where the server connection happens
$ldap = ldap_connect($adServer);
$ldaprdn = $ntDomain . "ldapuser";
$password = "ldapuserpassword";
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
// This is where the Authentication happens
$bind = @ldap_bind($ldap, $ldaprdn, $password);
$userDn="";
if ($bind) {
// Filter LDAP objectClass for "user & person", the "sAMAccountName" to equal the $usename varible
// and "UserAccountControl" for active accounts only
$filter="(&(objectClass=user)(objectCategory=Person)(sAMAccountName=$username)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))";
$result = ldap_search($ldap,$ldapDomian,$filter);
ldap_sort($ldap,$result,"sn");
$info = ldap_get_entries($ldap, $result);
for ($i=0; $i<$info["count"]; $i++)
{
if($info['count'] > 1)
break;
$userDn = $info[$i]["cn"][0];
}
@ldap_close($ldap);
}
return $userDn;
}
The above code pull the name via LDAP and updates the custom "displayname" field.
ReplyDeleteThis is exactly what I am looking for however it brings one problem. When I am in the ACP and I want to manage a user, I would normally click the button to find a user (Which brings up the user list) the issue is when I click on a user their it fills in the custom name not their actual username giving an error that said user could not be found.
ReplyDeleteWhy edit core code when there is a PHP event for just this very thing? Edits will be lost on update if overwriting files.
ReplyDeleteI found the get_username on line 1452 on verison 3.1.10
ReplyDeleteAlso didn't work for me
ReplyDeleteWorking perfectly for me...
ReplyDelete############
global $user, $auth;
/** Begin replace username with screen_name for display /*toggle on/off*/
global $phpbb_container;
if(!empty($user_id)) {
$cp = $phpbb_container->get('profilefields.manager');
$userProfileFields = $cp->grab_profile_fields_data($user_id);
if(!empty($userProfileFields[$user_id]["screen_name"]["value"]))
{ $username = $userProfileFields[$user_id]["screen_name"]["value"]; } }
/** End replace username with screen_name for display */
// This switch makes sure we only run code required for the mode
switch ($mode)
############
Many thanks!
helow i have phpBB3.2 i use ldap unth and Change Display Name insted of username
ReplyDeletemy email: skyforce_2030@hotmail.com