Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28908
  • 博文数量: 8
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 08:51
文章分类
文章存档

2014年(8)

我的朋友

分类: Oracle

2014-05-25 17:40:52

在plsql中有時會需要查看ldap Server上的數據,
如判段email帳戶是否存在,等等情形.

以Table函數返回ldap server上的數據.
導入使用時需要修改包體里面的l_ldap_host, l_ldap_user,l_ldap_passwd

使用範例:

点击(此处)折叠或打开

  1. select * from table(pkg_ldap.ldap_table_s('(&(objectClass=person)(mail=ganj*))'))
查詢效果圖如下


Oracle包的PL/SQL代碼如下:
該包中包含2個函數:
ldap_table_s: 取出ldap_server中的所有的attribute和值
get_user_table_s : 取出ldap_server特定的欄位值

点击(此处)折叠或打开

  1. create or replace package PKG_LDAP AUTHID CURRENT_USER as
  2. /****************************************
  3.    Ver Date Author Description
  4.    --------- ---------- --------------- ------------------------------------
  5.    1.0 2013-11-10 gangjh 1. 獲取ldap 帳戶信息
  6. *****************************************************************/


  7.  type ldap_user_info is record (
  8.  search_pk varchar2(256),
  9.  dn varchar2(256),
  10.  sn varchar2(256),
  11.  cn varchar2(256),
  12.  mail varchar2(256),
  13.  givenName varchar2(256),
  14.  department varchar2(256),
  15.  description varchar2(256),
  16.  title varchar2(256),
  17.  st varchar2(256),
  18.  memberOf varchar2(4000),
  19.  telephoneNumber varchar2(256),
  20.  lastLogonTimestamp date,
  21.  pwdLastSet date
  22.  ) ;

  23.  type ldap_user_table is table of ldap_user_info ;

  24.  type ldap_element is record
  25.  (
  26.    dn varchar2(256),
  27.    attribute varchar2(256),
  28.    string_val varchar2(256)
  29.  );
  30.  
  31.  type ldap_table is table of ldap_element;
  32.  

  33. /*********************
  34. @get_ldap_table_s : Table函數返回ldap查詢結果,類型為ldap_table
  35. ex:
  36.    select * from table(
  37.       pkg_ldap.ldap_table_s('(&(objectClass=person)(objectClass=top)(mail=g*))'))
  38. ********************/
  39.  function ldap_table_s(filter varchar2)
  40.    return ldap_table pipelined ;

  41.  
  42. /*********************
  43. @get_ldap_table_s :
  44.    select * from table(pkg_ldap.get_user_table_s('(&(objectClass=person)(mail=gan*))'))
  45. ********************/
  46.  function get_user_table_s(filter varchar2)
  47.    return ldap_user_table pipelined ;



  48. end PKG_LDAP;

  49.  
  50. /
  51. create or replace package body PKG_LDAP as
  52. /**************** ldap connection **********/
  53.   l_ldap_host constant VARCHAR2(256) := 'ldapserver';
  54.   l_ldap_port constant PLS_INTEGER := '389';
  55.   l_ldap_user constant VARCHAR2(256) := 'user';
  56.   l_ldap_passwd constant VARCHAR2(256) := 'pass';
  57. -- l_ldap_base constant varchar2(256) := 'DC=yydg,DC=com,DC=cn';
  58.   l_ldap_base constant varchar2(256) := '';
  59. /*********************************************/


  60. TYPE MESSAGE_COLLECTION IS TABLE OF DBMS_LDAP.MESSAGE
  61.       INDEX BY BINARY_INTEGER;


  62. procedure print(m varchar2) is
  63. begin
  64.   return;
  65.   dbms_output.put_line(m) ;
  66. end ;

  67. function get_values(ld IN DBMS_LDAP.SESSION,
  68.                     ldapentry IN DBMS_LDAP.MESSAGE,
  69.                     attr IN VARCHAR2)
  70.       RETURN DBMS_LDAP.STRING_COLLECTION IS
  71.  aa DBMS_LDAP.STRING_COLLECTION ;
  72. BEGIN
  73.     RETURN DBMS_LDAP.get_values (ld, ldapentry, attr);
  74.     EXCEPTION
  75.        WHEN OTHERS THEN
  76.          RETURN aa;

  77. end ;


  78. function all_attributes(ld DBMS_LDAP.session,
  79.                         ldapentry DBMS_LDAP.message)
  80.          return DBMS_LDAP.string_collection is
  81.          
  82.   l_attr DBMS_LDAP.string_collection;
  83.   ber_elem DBMS_LDAP.ber_element;
  84.   v_attr varchar2(300) ;
  85. begin
  86.     v_attr := DBMS_LDAP.first_attribute(ld, ldapentry, ber_elem);
  87.     
  88.     WHILE v_attr IS NOT NULL LOOP
  89.         l_attr(l_attr.count()+1) := v_attr ;
  90.         v_attr := dbms_ldap.next_attribute(ld, ldapentry, ber_elem);
  91.     END LOOP ;
  92.   
  93.   return l_attr;
  94. end;


  95. function all_messages(ld DBMS_LDAP.session,
  96.                       msg DBMS_LDAP.message)
  97.          return MESSAGE_COLLECTION is
  98.          
  99.   l_msg MESSAGE_COLLECTION;
  100.   v_msg DBMS_LDAP.message ;
  101. begin
  102.     v_msg := DBMS_LDAP.first_entry(ld, msg);
  103.     
  104.     WHILE v_msg IS NOT NULL LOOP
  105.         l_msg(l_msg.count()+1) := v_msg ;
  106.         v_msg := dbms_ldap.next_entry(ld, v_msg);
  107.     END LOOP ;
  108.   
  109.   return l_msg;
  110. end;



  111. function logon(l_host varchar2,
  112.                l_port PLS_INTEGER,
  113.                l_user varchar2,
  114.                l_passwd varchar2)
  115.          return DBMS_LDAP.session is
  116.          
  117.   l_sess DBMS_LDAP.session;
  118.   l_retval PLS_INTEGER ;
  119. begin
  120.   -- Connect to the LDAP server.
  121.   l_sess := DBMS_LDAP.init(hostname => l_host,
  122.                            portnum => l_port);

  123.   l_retval := DBMS_LDAP.simple_bind_s(ld => l_sess,
  124.                                       dn => l_user,
  125.                                       passwd => l_passwd );
  126.   print(l_retval);
  127.   return l_sess;
  128. end;

  129. -- Disconnect from the LDAP server.
  130. procedure logout(l_session in out DBMS_LDAP.session) is
  131.   l_retval pls_integer ;
  132. begin
  133.   l_retval := DBMS_LDAP.unbind_s(ld => l_session);
  134.   print(l_retval);
  135. end ;

  136. function search_s(ld DBMS_LDAP.SESSION,
  137.                   filter varchar2)
  138.          return MESSAGE_COLLECTION is
  139.  l_msg DBMS_LDAP.message;
  140.  l_retval PLS_INTEGER;
  141.  l_attrs DBMS_LDAP.string_collection;
  142. begin
  143.     l_retval := DBMS_LDAP.search_s(ld => ld,
  144.                                    base => l_ldap_base,
  145.                                    scope => DBMS_LDAP.SCOPE_SUBTREE,
  146.                                    filter => filter ,
  147.                                    attrs => l_attrs,
  148.                                    attronly => 0,
  149.                                    res => l_msg);
  150.     print(dbms_ldap.err2string(l_retval));
  151.     return all_messages(ld, l_msg) ;
  152. end;




  153. function ldap_table_s(filter varchar2)
  154.    return ldap_table pipelined is
  155.  
  156.   l_session DBMS_LDAP.session;
  157.   l_msgs MESSAGE_COLLECTION ;
  158.   l_attrs DBMS_LDAP.string_collection;
  159.   l_vals DBMS_LDAP.string_collection;
  160.   v_one_attr ldap_element;
  161.   
  162. BEGIN
  163.     l_session := logon(l_ldap_host, l_ldap_port, l_ldap_user, l_ldap_passwd) ;
  164.     
  165.     l_msgs := search_s(l_session, filter) ;
  166.     
  167.     for m1 in 1..l_msgs.count() LOOP --循環取出多個dn數據

  168.         v_one_attr.dn := DBMS_LDAP.get_dn(ld => l_session, ldapentry => l_msgs(m1)) ;
  169.         l_attrs := all_attributes(l_session, l_msgs(m1));
  170.         
  171.         for t1 in 1.. l_attrs.count() loop
  172.             v_one_attr.attribute := l_attrs(t1);
  173.             l_vals := get_values (ld => l_session, ldapentry => l_msgs(m1), attr => v_one_attr.attribute);
  174.             FOR i in 0.. l_vals.count()-1 LOOP
  175.                 v_one_attr.string_val := l_vals(i);
  176.                 pipe row (v_one_attr);
  177.             END LOOP;
  178.         END LOOP ;
  179.     END LOOP ;

  180.     logout(l_session);

  181.     return ;
  182. end ldap_table_s;



  183. --n: 這個數字代表表1601年1月1日(星期一)0點開始,已經經過多少個100納秒
  184. function to_timestamp(n number) return date is
  185.   v_start constant date := to_date('16010101','yyyymmdd') ;
  186. begin
  187.   return v_start + n /10000000/60/1440;
  188.   exception when others then
  189.     dbms_output.put_line('n val:'||n);
  190.     return null;
  191. end;

  192. /***********************
  193. @get_userinfo 轉換entry數據為 ldap_user_info record,
  194.   有多個值時只取第一個值
  195. *********************/
  196. function get_userinfo(ld DBMS_LDAP.session,
  197.                       l_entry DBMS_LDAP.message)
  198.          return ldap_user_info is
  199.   
  200.   v_user ldap_user_info := null;
  201.     
  202.   function l_get_value(v_entry DBMS_LDAP.message, v_attr varchar2) return varchar2 is
  203.   l_vals DBMS_LDAP.string_collection;
  204.   v1 varchar2(4000) :='' ;
  205.   begin
  206.     l_vals := DBMS_LDAP.get_values (ld => ld,
  207.                                     ldapentry => v_entry,
  208.                                     attr => v_attr);
  209.     for r in 0.. l_vals.count() -1 loop
  210.         v1 :=v1 ||chr(10)|| l_vals(r);
  211.     end loop;
  212.     return substr(v1, 2) ;
  213.   end ;
  214. begin

  215. /* l_attrs(1) := 'sn'; -- 中文名
  216.   l_attrs(2) := 'cn';
  217.   l_attrs(3) := 'mail';
  218.   l_attrs(4) := 'telephoneNumber';
  219.   l_attrs(5) := 'givenName';
  220.   l_attrs(6) := 'lastLogonTimestamp';
  221.   l_attrs(7) := 'pwdLastSet'; */

  222.   v_user.dn := dbms_ldap.get_dn(ld, l_entry) ;
  223.   v_user.sn := l_get_value(l_entry, 'sn') ;
  224.   v_user.cn := l_get_value(l_entry, 'cn') ;
  225.   v_user.mail := l_get_value(l_entry, 'mail') ;
  226.   v_user.givenName := l_get_value(l_entry, 'givenName') ;
  227.   v_user.telephoneNumber := l_get_value(l_entry, 'telephoneNumber') ;
  228.   v_user.department := l_get_value(l_entry, 'department') ;
  229.   v_user.description := l_get_value(l_entry, 'description') ;
  230.   v_user.title := l_get_value(l_entry, 'title') ;
  231.   v_user.st := l_get_value(l_entry, 'st') ;
  232.   v_user.memberOf := l_get_value(l_entry, 'memberOf') ;
  233.   v_user.lastLogonTimestamp := to_timestamp(l_get_value(l_entry, 'lastLogonTimestamp')) ;
  234.   v_user.pwdLastSet := to_timestamp(l_get_value(l_entry, 'pwdLastSet')) ;

  235.   return v_user;
  236. end;


  237.  
  238. /******************
  239.   @get_ldap_table_s -- 查詢ldap數據,
  240. *****************/

  241.  function get_user_table_s(filter varchar2)
  242.            return ldap_user_table pipelined is
  243.   
  244.   v_userinfo ldap_user_info := null ;
  245.   
  246.   l_session DBMS_LDAP.session;
  247.   l_msgs MESSAGE_COLLECTION;
  248. BEGIN

  249.     l_session := logon(l_ldap_host, l_ldap_port, l_ldap_user, l_ldap_passwd) ;

  250.     l_msgs := search_s(l_session, filter) ;
  251.        
  252.     FOR r in 1..l_msgs.count() LOOP
  253.         v_userinfo := get_userinfo(l_session, l_msgs(r)) ;
  254.         v_userinfo.search_pk := filter ;
  255.         pipe row(v_userinfo) ;
  256.     END LOOP ;
  257.     
  258.   logout(l_session);

  259.   return ;
  260. end get_user_table_s;



  261. BEGIN
  262.     DBMS_LDAP.USE_EXCEPTION := TRUE;
  263. end PKG_LDAP;
  264. /

阿飛
2015/05/19 修改

阅读(1020) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~