my $ldap = get_my_ldap_object(); $result = $ldap->add( 'cn=Barbara Jensen, o=University of Michigan, c=US', attr => [ 'cn' => ['Barbara Jensen', 'Barb Jensen'], 'sn' => 'Jensen', 'mail' => 'b.jensen@umich.edu', 'objectclass' => ['top', 'person', 'organizationalPerson', 'inetOrgPerson' ], ] ); $result->code && warn "failed to add entry: ", $result->error ; $mesg = $ldap->unbind;
use MyLDAPUser; my $ldap = get_my_ldap_object(); my $person = MyLDAPUser->new( ldap => $ldap ); $person->cn(['Barbara Jensen', 'Barb Jensen']); $person->sn('Jensen'); $person->mail('b.jensen@umich.edu'); $person->create;
Lord, save me from writing [SQL|LDAP filters].
package MyLDAPUser; use base qw( Net::LDAP::Class ); __PACKAGE__->metadata->setup( use_loader => 1, object_classes => [qw( posixAccount )], # optional ); sub init_ldap { return Net::LDAP->new( 'ldap.mycompany.org' ); } 1;
use MyLDAPUser; use MyORMClass; my @attrs = qw( uidNumber gidNumber lastModified ); MyLDAPUser->act_on_all( sub { my $ldapuser = shift; my $dbuser = MyORMClass->new( username => $ldapuser->username ) ->load_or_insert; for my $attr (@attrs) { $dbuser->$attr( $ldapuser->$attr ); } $dbuser->save; }, { ldap => Net::LDAP->new( 'ldap.mycompany.org' ) } );
package MyLDAPUser; sub validate { my ( $self, $attr, $value ) = @_; my $method = 'validate_' . $attr; return $self->can($method) ? $self->$method($value) : 1; } sub validate_uid { my ( $self, $value ) = @_; croak "invalid uid: $value" unless $value =~ m/^\w+$/; }
use MyLDAPUser; my $user = MyLDAPUser->new( username => 'larry' ) ->read; printf("user %s primary group is %s\n", $user, $user->group); for my $group ($user->groups) { printf(" secondary group: %s\n", $group); }
use MyLDAPUser; use MyLDAPGroup; my $user = MyLDAPUser->new( username => 'larry' ) ->read_or_create; my $group = MyLDAPGroup->new( name => 'stooges' ) ->read_or_create; $user->add_to_group( $group ) unless $group->has_user( $user ); $user->save;