エントリの操作(追加、更新、削除)は、Net::LDAPオブジェクトのadd, modify, deleteでも行えますが、いったん検索されたエントリNet::LDAP::Entryからも行えます。
Net::LDAP::Entiry
エントリの検索:
以下のコードでは、検索を行い$entryという変数にエントリを代入しています。
use Net::LDAP;
#
# bind as priviledged user for modify.
#
$ldap = Net::LDAP->new( "localhost" );
$message = $ldap->bind( "cn=Directory Manager", password=>"password" );
if( $message->code == 0 ) {
print "bind success\n";
} else {
warn "error with bind: " . $message->error ."\n";
exit( 1 );
}
$dn ="uid=test.0,ou=People,dc=example,dc=com";
$newRDN = "cn=test";
$result = $ldap->search( base=>$dn, scope=>'base', filter=>'objectclass=inetOrgPerson' );
if( $result->code == 0 ) {
@entries=$result->entries;
#取得されたエントリー
$entry=$entries[0];
}
$ldap->unbind;
上記の、$entryという変数(オブジェクト)では、以下のようなメソッドが使えます。
エントリのダンプ: dump
属性の追加: add( attr => value [,...] )
属性の変更: replace( attr => value )
エントリの削除: delete()
属性の削除: delete( attr -> [value[,...]] [,...] )
属性の取得: attributes
属性値の取得: get_value ( attr )
エントリー内のmail属性の値の取得:
...
@mail = $entry->get_value( 'mail' );
print "@mail" . "\n";
...
出力:
test.0@example.com test.0@example.net
Net::LDAP->searchの検索からではなく、新規にEntryオブジェクトを作成する際は、以下のようにコンストラクターが使えます。
use Net::LDAP;
use Net::LDAP::Entry;
...
$entry = Net::LDAP::Entry->new( "uid=test.99,ou=People,dc=example,dc=com",
objectclass => [ 'top', 'person', 'inetOrgPerson' ],
uid => 'test.99',
mail => 'test.99@example.com',
cn => 'test',
sn => 'test 99' );
$result = $ldap->add( $entry );
...