Ruby-ldap

提供:Dev Guides
移動先:案内検索

Ruby-LDAPチュートリアル

Ruby/LDAPはRubyの拡張ライブラリです。 OpenLDAP、UMich LDAP、Netscape SDK、ActiveDirectoryなどの一部のLDAPライブラリへのインターフェースを提供します。

アプリケーション開発用の共通APIはRFC1823で説明されており、Ruby/LDAPでサポートされています。

Ruby/LDAPインストール

完全なRuby/LDAPパッケージは、https://sourceforge.net/projects/ruby-ldap/[SOURCEFORGE.NET]からダウンロードしてインストールできます。

Ruby/LDAPをインストールする前に、次のコンポーネントがあることを確認してください-

  • Ruby 1.8.x(ldap/controlを使用する場合は少なくとも1.8.2)。
  • OpenLDAP、Netscape SDK、Windows 2003またはWindows XP。

これで、標準のRubyインストール方法を使用できます。 開始する前に、extconf.rbで利用可能なオプションを確認したい場合は、 '-help’オプションで実行します。

$ ruby extconf.rb [--with-openldap1|--with-openldap2| \
                   --with-netscape|--with-wldap32]
$ make
$ make install

-Windowsでソフトウェアをビルドする場合は、_make_の代わりに_nmake_を使用する必要がある場合があります。

LDAP接続を確立する

これは2段階のプロセスです-

ステップ1-接続オブジェクトの作成

LDAPディレクトリへの接続を作成する構文は次のとおりです。

LDAP::Conn.new(host = 'localhost', port = LDAP_PORT)
  • host -これは、LDAPディレクトリを実行しているホストIDです。 _localhost_とします。
  • port -これは、LDAPサービスに使用されているポートです。 標準のLDAPポートは636および389です。 サーバーで使用されているポートを確認してください。使用していない場合は、LDAP :: LDAP_PORTを使用できます。
    この呼び出しは、ポート_port_上のサーバー_host_への新しい_LDAP
    Conn_接続を返します。

ステップ2-バインド

これは通常、セッションの残りに使用するユーザー名とパスワードを指定する場所です。

以下は、DN、 dn 、資格情報、 pwd 、およびバインド方法、 method を使用して、LDAP接続をバインドする構文です-

conn.bind(dn = nil, password = nil, method = LDAP::LDAP_AUTH_SIMPLE)do
....
end

You can use the same method without a code block. In this case, you would need to unbind the connection explicitly as follows −

conn.bind(dn = nil、パスワード= nil、method = LDAP :: LDAP_AUTH_SIMPLE)
....
conn.unbind

コードブロックが指定されている場合、_self_がブロックに渡されます。

適切な権限があれば、バインドメソッドのブロック内(バインドとバインド解除の間)で、検索、追加、変更、または削除の操作を実行できます。

ローカルサーバーで作業していると仮定して、適切なホスト、ドメイン、ユーザーID、パスワードなどを組み合わせてみましょう。

#/usr/bin/ruby -w

require 'ldap'

$HOST =    'localhost'
$PORT =    LDAP::LDAP_PORT
$SSLPORT = LDAP::LDAPS_PORT

conn = LDAP::Conn.new($HOST, $PORT)
conn.bind('cn = root, dc = localhost, dc = localdomain','secret')
....
conn.unbind

Adding an LDAP Entry

Adding an LDPA entry is a two step process −

Step 1 − Creating LDAP::Mod object

We need LDAP::Mod object pass to conn.add method to create an entry. Here is a simple syntax to create LDAP::Mod object −

Mod.new(mod_type、attr、vals)
  • mod_type − One or more option LDAP_MOD_ADD, LDAP_MOD_REPLACE or LDAP_MOD_DELETE.
  • attr − should be the name of the attribute on which to operate.
  • vals − is an array of values pertaining to attr. If vals contains binary data, mod_type should be logically OR’ed (|) with LDAP_MOD_BVALUES.

This call returns LDAP::Mod object, which can be passed to methods in the LDAP::Conn class, such as Conn#add, Conn#add_ext, Conn#modify and Conn#modify_ext.

Step 2 − Calling conn.add Method

Once we are ready with LDAP::Mod object, we can call conn.add method to create an entry. Here is a syntax to call this method −

conn.add(dn、attrs)

This method adds an entry with the DN, dn, and the attributes, attrs. Here, attrs should be either an array of LDAP::Mod objects or a hash of attribute/value array pairs.

Example

Here is a complete example, which will create two directory entries −

#/usr/bin/ruby​​ -w

「ldap」が必要

$ HOST = 'localhost' $ PORT = LDAP :: LDAP_PORT $ SSLPORT = LDAP :: LDAPS_PORT

conn = LDAP :: Conn.new($ HOST、$ PORT)conn.bind( 'cn = root、dc = localhost、dc = localdomain'、 'secret')

conn.perror( "bind")entry1 = [LDAP.mod(LDAP :: LDAP_MOD_ADD、 'objectclass'、['top'、 'domain'])、LDAP.mod(LDAP :: LDAP_MOD_ADD、 'o'、[' TTSKY.NET '])、LDAP.mod(LDAP :: LDAP_MOD_ADD、' dc '、[' localhost '])、]

entry2 = [LDAP.mod(LDAP :: LDAP_MOD_ADD、 'objectclass'、['top'、 'person'])、LDAP.mod(LDAP :: LDAP_MOD_ADD、 'cn'、['Zara Ali'])、LDAP。 mod(LDAP :: LDAP_MOD_ADD | LDAP :: LDAP_MOD_BVALUES、 'sn'、['ttate'、 'ALI'、 "zero \ 000zero"])、]

begin conn.add( "dc = localhost、dc = localdomain"、entry1)conn.add( "cn = Zara Ali、dc = localhost、dc = localdomain"、entry2)rescue LDAP :: ResultError conn.perror( "add" )exit end conn.perror( "add")conn.unbind

Modifying an LDAP Entry

Modifying an entry is similar to adding one. Just call the modify method instead of add with the attributes to modify. Here is a simple syntax of modify method.

conn.modify(dn、mods)

This method modifies an entry with the DN, dn, and the attributes, mods. Here, mods should be either an array of LDAP::Mod objects or a hash of attribute/value array pairs.

Example

To modify the surname of the entry, which we added in the previous section, we would write −

#/usr/bin/ruby​​ -w

「ldap」が必要

$ HOST = 'localhost' $ PORT = LDAP :: LDAP_PORT $ SSLPORT = LDAP :: LDAPS_PORT

conn = LDAP :: Conn.new($ HOST、$ PORT)conn.bind( 'cn = root、dc = localhost、dc = localdomain'、 'secret')

conn.perror( "bind")entry1 = [LDAP.mod(LDAP :: LDAP_MOD_REPLACE、 'sn'、['Mohtashim'])、]

begin conn.modify( "cn = Zara Ali、dc = localhost、dc = localdomain"、entry1)rescue LDAP :: ResultError conn.perror( "modify")exit end conn.perror( "modify")conn.unbind

Deleting an LDAP Entry

To delete an entry, call the delete method with the distinguished name as parameter. Here is a simple syntax of delete method.

conn.delete(dn)

This method deletes an entry with the DN, dn.

Example

To delete Zara Mohtashim entry, which we added in the previous section, we would write −

#/usr/bin/ruby​​ -w

「ldap」が必要

$ HOST = 'localhost' $ PORT = LDAP :: LDAP_PORT $ SSLPORT = LDAP :: LDAPS_PORT

conn = LDAP :: Conn.new($ HOST、$ PORT)conn.bind( 'cn = root、dc = localhost、dc = localdomain'、 'secret')

conn.perror( "bind")begin conn.delete( "cn = Zara-Mohtashim、dc = localhost、dc = localdomain")rescue LDAP :: ResultError conn.perror( "delete")exit end conn.perror( "delete ")conn.unbind

Modifying the Distinguished Name

It’s not possible to modify the distinguished name of an entry with the modify method. Instead, use the modrdn method. Here is simple syntax of modrdn method −

conn.modrdn(dn、new_rdn、delete_old_rdn)

This method modifies the RDN of the entry with DN, dn, giving it the new RDN, new_rdn. If delete_old_rdn is true, the old RDN value will be deleted from the entry.

Example

Suppose we have the following entry −

dn:cn = Zara Ali、dc = localhost、dc = localdomain cn:Zara Ali sn:Aliオブジェクトクラス:person

Then, we can modify its distinguished name with the following code −

#/usr/bin/ruby​​ -w

「ldap」が必要

$ HOST = 'localhost' $ PORT = LDAP :: LDAP_PORT $ SSLPORT = LDAP :: LDAPS_PORT

conn = LDAP :: Conn.new($ HOST、$ PORT)conn.bind( 'cn = root、dc = localhost、dc = localdomain'、 'secret')

conn.perror( "bind")begin conn.modrdn( "cn = Zara Ali、dc = localhost、dc = localdomain"、 "cn = Zara Mohtashim"、true)rescue LDAP :: ResultError conn.perror( "modrdn") exit end conn.perror( "modrdn")conn.unbind

Performing a Search

To perform a search on a LDAP directory, use the search method with one of the three different search modes −

  • LDAP_SCOPE_BASEM − Search only the base node.
  • LDAP_SCOPE_ONELEVEL − Search all children of the base node.
  • *LDAP_SCOPE_SUBTREE *− Search the whole subtree including the base node.

Example

Here, we are going to search the whole subtree of entry dc = localhost, dc = localdomain for person objects −

#/usr/bin/ruby​​ -w

「ldap」が必要

$ HOST = 'localhost' $ PORT = LDAP :: LDAP_PORT $ SSLPORT = LDAP :: LDAPS_PORT

base = 'dc = localhost、dc = localdomain' scope = LDAP :: LDAP_SCOPE_SUBTREE filter = '(objectclass = person)' attrs = ['sn'、 'cn']

conn = LDAP :: Conn.new($ HOST、$ PORT)conn.bind( 'cn = root、dc = localhost、dc = localdomain'、 'secret')

conn.perror( "bind")begin conn.search(base、scope、filter、attrs){| entry | #識別名p entry.dnを出力#すべての属性名を出力p entry.attrs#属性 'sn'の値を出力p entry.vals( 'sn')#エントリをハッシュとして出力p entry.to_hash} rescue LDAP :: ResultError conn .perror( "search")exit end conn.perror( "search")conn.unbind

This invokes the given code block for each matching entry where the LDAP entry is represented by an instance of the LDAP::Entry class. With the last parameter of search, you can specify the attributes in which you are interested, omitting all others. If you pass nil here, all attributes are returned same as "SELECT* " in relational databases.

The dn method (alias for get_dn) of the LDAP::Entry class returns the distinguished name of the entry, and with the to_hash method, you can get a hash representation of its attributes (including the distinguished name). To get a list of an entry’s attributes, use the attrs method (alias for get_attributes). Also, to get the list of one specific attribute’s values, use the vals method (alias for get_values).

Handling Errors

Ruby/LDAP defines two different exception classes −

  • In case of an error, the new, bind or unbind methods raise an LDAP::Error exception.
  • In case of add, modify, delete or searching an LDAP directory raise an LDAP::ResultError.

Further Reading

For complete details on LDAP methods, please refer to the standard documentation for LDAP Documentation.