29 Mart 2016 Salı

DBMS_LDAP Example. Sample code to work with Service DBMS_LDAP


How to write a simple code that works with LDAP-directory using DBMS_LDAP package
Perhaps you have had to deal with a situation where we must directly from the Oracle database to ask for data that resides in the LDAP-directory.
For this purpose there exists a wonderful standard DBMS_LDAP package.
Below is a sample code where we want to get the cn user, knowing his DN.

Declare
  ldap_session DBMS_LDAP.session: = null;
  ErrorMsg VARCHAR2 (4000);
- The attribute value
  my_attr_value varchar2 (256);
  entry_dn varchar2 (256);
  my_vals DBMS_LDAP.STRING_COLLECTION;
  my_ber_elmt DBMS_LDAP.ber_element;
  my_attrs DBMS_LDAP.STRING_COLLECTION;
  my_attr_name varchar2 (256);
  my_message DBMS_LDAP.message;
  my_entry DBMS_LDAP.message;
  ret_val PLS_INTEGER: = - 1;
- Distinguished name user
  userDN varchar2 (2000): = 'uid = iivanov, ou = People, dc = maxcrc, dc = com';
BEGIN
  my_attrs (1): = 'cn';

- The creation of LDAP session handle (server address, port)
  ldap_session: = DBMS_LDAP.init ( 'asusnote', '389');

- Admin, password
  ret_val: = DBMS_LDAP.simple_bind_s (ldap_session, 'cn = Manager, dc = maxcrc, dc = com', 'secret');
 
- Looking user
  ret_val: = DBMS_LDAP.search_s (ldap_session,
                                userDN,
                                DBMS_LDAP.SCOPE_BASE,
                                'Objectclass = inetorgperson',
                                my_attrs,
                                0
                                my_message);

  dbms_output.put_line ( 'Debug :: ret_Val expected is 0, found =' || ret_val);
  dbms_output.put_line ( '! Debug :: if ret_Val = 0 then ckeck dbms_ldap package for error code');

- To get an attribute value from the search results
  my_entry: = DBMS_LDAP.first_entry (ldap_session, my_message);
  my_attr_name: = DBMS_LDAP.first_attribute (ldap_session, my_entry, my_ber_elmt);
  my_vals: = DBMS_LDAP.get_values ​​(ldap_session, my_entry, my_attr_name);
  my_attr_value: = my_vals (0);

  dbms_output.put_line ( 'Debug ::' || my_attr_name || '=' || my_attr_value);

EXCEPTION
 WHEN others THEN
   ErrorMsg: = SQLERRM;
   dbms_output.put_line ( 'Error ::' || ErrorMsg);
END;

Hiç yorum yok:

Yorum Gönder