Monday, May 12, 2008

LDAP User Search (Who's Who)

Well, it's been a while since my last post, but in the last couple of weeks people have been asking me a flew things that I have posted before in this blog and others that should be here, so I'm going to try to put them up.

First, This is a small procedure that can help you create a who's who or people finder within your portal, its a small variation of one I put in the blog some months ago to get the members of a group.

Basically, it uses DBMS_LDAP to create an ldap search command, it recieves a parameter that defines what you want to search for. for example if you are looking for people whose last name starts with a "w" you would call the procedure as follows:

http::/pls/portal/.GET_ALL_USERS?p_filter=(sn=w*)

I hope it helps you, enjoy..




PROCEDURE GET_ALL_USERS (p_filter in varchar2 default 'objectclass=*')

IS

search_filter VARCHAR2(512);

retval PLS_INTEGER;

my_session DBMS_LDAP.session;

v_row NUMBER:=0;

my_attrs DBMS_LDAP.STRING_COLLECTION;

my_message DBMS_LDAP.message;

my_entry DBMS_LDAP.message;

entry_index PLS_INTEGER;

temp_vals DBMS_LDAP.STRING_COLLECTION;

subscriber_handle DBMS_LDAP_UTL.HANDLE;

gv_ldap_host VARCHAR2(256) := '127.0.0.1';

gv_ldap_port pls_integer := '389';

gv_ldap_user VARCHAR2(256) := 'cn=orcladmin';

gv_ldap_passwd VARCHAR2(256) := 'oracle10g';

gv_user_base VARCHAR2(256) := 'cn=Users,dc=redrock,dc=com,dc=au';



BEGIN



retval := -1;

DBMS_LDAP.USE_EXCEPTION := TRUE;



--Initialize ldap connection

my_session := DBMS_LDAP.init(gv_ldap_host,gv_ldap_port);

retval := DBMS_LDAP.simple_bind_s(my_session,gv_ldap_user, gv_ldap_passwd);



--Define Attributes for the search

--my_attrs(1) := 'cn';

my_attrs(1) := 'uid';

my_attrs(2) := 'givenname';

my_attrs(3) := 'sn';

my_attrs(4) := 'mail';

my_attrs(5) := 'telephoneNumber';









retval := DBMS_LDAP.search_s

( my_session,

gv_user_base,

DBMS_LDAP.SCOPE_SUBTREE,

p_filter,

my_attrs,

0,

my_message);



-- count the number of entries returned

retval := DBMS_LDAP.count_entries(my_session, my_message);



htp.p('<html><head><style>

td {font-family: Arial, Helvetica, sans-serif; color:#000000; font-size: 9pt;}

body {font-family: Arial, Helvetica, sans-serif; font-size: 9pt; }

.tableheading {font-weight: bold; font-family:Arial, Helvetica, sans-serif;
font-size: 9pt; color: #FFFFFF; background-color: #6C8FA0; padding:4px;}

.tableborder {border: 1px solid #D6D6EB;}

.row1 {background-color: #FFFFFF; }

.row2 {background-color: #E8E8E8; }

</style></head><body>');

htp.p('<table border="0" width="100%" class="tableborder" cellpadding="3"
cellspacing="3" >');

htp.p('<tr>');

htp.p('<td class="tableheading" >DN</td>');

FOR i IN my_attrs.FIRST..my_attrs.LAST LOOP

htp.p('<td class="tableheading" >'||my_attrs(i)||'</td>');

end loop;

htp.p('</tr>');

v_row := 0;

--Loop throught the results

my_entry := DBMS_LDAP.first_entry(my_session, my_message);



WHILE my_entry IS NOT NULL loop

IF MOD(v_row, 2) = 0 THEN htp.p('<tr class="row1">');

ELSE htp.p('<tr class="row2">'); END IF;

v_row:=v_row+1;



htp.p('<td>'||DBMS_LDAP.get_dn(my_session, my_entry)||'</td>');

FOR i IN my_attrs.FIRST..my_attrs.LAST LOOP

temp_vals := DBMS_LDAP.GET_VALUES(my_session, my_entry, my_attrs(i));

IF temp_vals.COUNT > 0 THEN

htp.p('<td>'||temp_vals(0)||'</td>');

ELSE

htp.p('<td>--</td>');

END IF;

END LOOP;

htp.p('</tr>');

my_entry := DBMS_LDAP.next_entry(my_session, my_entry);

entry_index := entry_index+1;

end loop;



htp.p('</table>');

htp.p(retval||' Results');

htp.p('</body></html>');

--Close the connection

retval := DBMS_LDAP.unbind_s(my_session);



EXCEPTION

WHEN OTHERS THEN htp.p(sqlerrm);

END;





No comments: