2020import java .util .ArrayList ;
2121import java .util .Arrays ;
2222import java .util .Collection ;
23+ import java .util .Collections ;
2324import java .util .Comparator ;
2425import java .util .HashMap ;
2526import java .util .HashSet ;
3031import java .util .Map ;
3132import java .util .Map .Entry ;
3233import java .util .Set ;
34+ import java .util .StringTokenizer ;
3335import java .util .TreeMap ;
3436
37+ import javax .naming .directory .Attributes ;
38+ import javax .naming .directory .DirContext ;
39+ import javax .naming .ldap .Rdn ;
40+
3541import org .dom4j .DocumentHelper ;
3642import org .dom4j .Element ;
3743import org .dom4j .QName ;
4349import org .jivesoftware .openfire .disco .IQDiscoItemsHandler ;
4450import org .jivesoftware .openfire .group .Group ;
4551import org .jivesoftware .openfire .group .GroupManager ;
52+ import org .jivesoftware .openfire .ldap .LdapManager ;
4653import org .jivesoftware .openfire .user .User ;
4754import org .jivesoftware .openfire .user .UserManager ;
55+ import org .jivesoftware .util .Base64 ;
4856import org .jivesoftware .util .JiveGlobals ;
4957import org .jivesoftware .util .LocaleUtils ;
5058import org .jivesoftware .util .PropertyEventDispatcher ;
@@ -403,7 +411,15 @@ private IQ processGetPacket(IQ packet) {
403411 field .setVariable (searchField );
404412 field .setType (FormField .Type .boolean_type );
405413 field .addValue ("1" );
406- field .setLabel (LocaleUtils .getLocalizedString ("advance.user.search." + searchField .toLowerCase (), "search" ));
414+ String strlabel = LocaleUtils .getLocalizedString ("advance.user.search." + searchField .toLowerCase (), "search" );
415+ if (strlabel !=null &&!strlabel .startsWith ("???advance.user.search" ))
416+ {
417+ field .setLabel (strlabel );
418+ }
419+ else
420+ {
421+ field .setLabel (searchField );
422+ }
407423 field .setRequired (false );
408424 }
409425
@@ -736,23 +752,66 @@ private IQ replyDataFormResult(Collection<User> users, IQ packet) {
736752 searchResults .addReportedField ("jid" , "JID" , FormField .Type .jid_single );
737753
738754 for (final String fieldName : getFilteredSearchFields ()) {
739- searchResults .addReportedField (fieldName ,
740- LocaleUtils .getLocalizedString ("advance.user.search." + fieldName .toLowerCase (), "search" ), FormField .Type .text_single );
755+ String strlabel = LocaleUtils .getLocalizedString ("advance.user.search." + fieldName .toLowerCase (), "search" );
756+ if (strlabel !=null &&!strlabel .startsWith ("???advance.user.search" ))
757+ {
758+ searchResults .addReportedField (fieldName ,strlabel , FormField .Type .text_single );
759+ }
760+ else
761+ {
762+ searchResults .addReportedField (fieldName ,fieldName , FormField .Type .text_single );
763+ }
741764 }
742765
743766 for (final User user : users ) {
744767 final String username = JID .unescapeNode (user .getUsername ());
745768
746769 final LinkedHashMap <String , Object > item = new LinkedHashMap <String , Object >();
770+
747771 item .put ("jid" , username + "@" + serverName );
748772
749- item .put (LocaleUtils .getLocalizedString ("advance.user.search.username" , "search" ), username );
773+ String ldapsearchfields = JiveGlobals .getProperty ("ldap.searchFields" ,null );
774+ if (ldapsearchfields !=null )
775+ {
776+ HashMap <String ,String > fields = new LinkedHashMap <String ,String >();
777+ ArrayList <String > attrList = new ArrayList <String >();
778+ try
779+ {
780+ for (StringTokenizer i =new StringTokenizer (ldapsearchfields , "," ); i .hasMoreTokens ();)
781+ {
782+ String [] field = i .nextToken ().split ("/" );
783+ fields .put (field [0 ], field [1 ]);
784+ attrList .add (field [1 ]);
785+ }
786+ Log .debug ("fill fields: " +fields .toString ());
787+ }
788+ catch (Exception e )
789+ {
790+ fields =null ;
791+ Log .error ("Error parsing LDAP search fields: " + ldapsearchfields , e );
792+ }
793+
794+ if (fields !=null )
795+ {
796+ Map <String ,String > listattr = getLdapAttributes (username ,attrList .toArray (new String [attrList .size ()]));
797+
798+ for (String str : userManager .getSearchFields ())
799+ {
800+ Log .debug ("ATTR: " +fields .get (str )+" = " +listattr .get (fields .get (str )));
801+ item .put (str , listattr .get (fields .get (str )));
802+ }
803+ }
804+ }
805+ else
806+ {
807+ item .put (LocaleUtils .getLocalizedString ("advance.user.search.username" , "search" ), username );
750808
751- item .put (LocaleUtils .getLocalizedString ("advance.user.search.name" , "search" ),
752- (user .isNameVisible () ? removeNull (user .getName ()) : "" ));
809+ item .put (LocaleUtils .getLocalizedString ("advance.user.search.name" , "search" ),
810+ (user .isNameVisible () ? removeNull (user .getName ()) : "" ));
753811
754- item .put (LocaleUtils .getLocalizedString ("advance.user.search.email" , "search" ),
755- (user .isEmailVisible () ? removeNull (user .getEmail ()) : "" ));
812+ item .put (LocaleUtils .getLocalizedString ("advance.user.search.email" , "search" ),
813+ (user .isEmailVisible () ? removeNull (user .getEmail ()) : "" ));
814+ }
756815
757816 searchResults .addItemFields (item );
758817 }
@@ -764,6 +823,58 @@ private IQ replyDataFormResult(Collection<User> users, IQ packet) {
764823 return replyPacket ;
765824 }
766825
826+ private Map <String , String > getLdapAttributes (String username , String [] attrlist ) {
827+ // Un-escape username
828+ username = JID .unescapeNode (username );
829+ Map <String , String > map = new HashMap <>();
830+
831+ DirContext ctx = null ;
832+
833+ LdapManager manager = LdapManager .getInstance ();
834+ try {
835+ Log .debug ("find userDN from " +username );
836+ Rdn [] userRDN = manager .findUserRDN (username );
837+
838+ ctx = manager .getContext (manager .getUsersBaseDN (username ));
839+ Attributes attrs = ctx .getAttributes (LdapManager .escapeForJNDI (userRDN ), attrlist );
840+
841+ for (String attribute : attrlist ) {
842+ javax .naming .directory .Attribute attr = attrs .get (attribute );
843+ String value ;
844+ if (attr == null ) {
845+ Log .debug ("No ldap value found for attribute '" + attribute + "'" );
846+ value = "" ;
847+ }
848+ else {
849+ Object ob = attrs .get (attribute ).get ();
850+ Log .debug ("Found attribute " +attribute +" of type: " +ob .getClass ());
851+ if (ob instanceof String ) {
852+ value = (String )ob ;
853+ } else {
854+ value = Base64 .encodeBytes ((byte [])ob );
855+ }
856+ }
857+ Log .debug ("Ldap attribute '" + attribute + "'=>'" + value + "'" );
858+ map .put (attribute , value );
859+ }
860+ return map ;
861+ }
862+ catch (Exception e ) {
863+ Log .error (e .getMessage (), e );
864+ return Collections .emptyMap ();
865+ }
866+ finally {
867+ try {
868+ if (ctx != null ) {
869+ ctx .close ();
870+ }
871+ }
872+ catch (Exception e ) {
873+ // Ignore.
874+ }
875+ }
876+ }
877+
767878 /**
768879 * Constructs a query that is returned as an IQ packet that contains the search results.
769880 *
0 commit comments