package Fripost::Schema; use 5.010_000; use strict; use base qw/Net::LDAP/; our $VERSION = '0.01'; # Initialize a connection to the LDAP host. sub new { my $class = shift; my $h = shift; my $self = {options => $h}; bless $self, $class; my $ldap = Net::LDAP->new ( $h->{server_host} ) or die "Error: Cannot initialize connection to LDAP server.\n"; my $mesg; if ( (defined $h->{bind_dn}) and $h->{bind_dn} ne '' ) { $self->_dsay( "Binding to DN `" .$h->{bind_dn}. "'." ); $mesg = $ldap->bind( $h->{bind_dn}, password => $h->{bind_pw} ); } else { # Anonymous bind $self->_dsay( "Anonymous bind." ); $mesg = $ldap->bind(); } die "Error: " .$mesg->error. "\n" if $mesg->code; $self->{ldap} = $ldap; return $self; } # Search a user, and return the corresponding entries if found. If no # user is given, returns all users. sub searchUser { my $self = shift; my $base = join ',', ('ou=mailboxes',$self->{options}->{base_dn}); my $filter = "(ObjectClass=virtualMailbox)"; $filter = "(&" .$filter. "(uid=" .$_[0]. ")" .")" if defined $_[0]; my $res = $self->{ldap}->search( base => $base, scope => 'one', attrs => [ 'uid', 'gn' , 'sn', 'maildir', 'isActive' ], filter => $filter ); die "Error: " .$res->error. "\n" if $res->code; return $res; } # Add a user sub addUser { my $self = shift; my $user = shift; my $base = join ',', ('ou=mailboxes',$self->{options}->{base_dn}); my $res = $self->{ldap}->add( 'uid=' .$user->{username}. ',' .$base, attrs => [ uid => $user->{username}, objectClass => [ 'top', 'virtualMailbox' ], userPassword => $user->{userPassword}, maildir => $user->{maildir}, isActive => $user->{isActive} ] ); die "Error: " .$res->error. "\n" if $res->code; return $res; } # Disconnect to the LDAP server. sub unbind { $_[0]->{ldap}->unbind(); } # Debug print. sub _dsay { my $self = shift; return unless (exists $self->{options}->{debug}) and $self->{options}->{debug}; print STDERR "Debug: "; say STDERR @_; } 1; =head1 NAME Fripost::Schema - =head1 AUTHOR Guilhem Moulin C<< >> =head1 COPYRIGHT Copyright 2012 Guilhem Moulin, all rights reserved. =head1 LICENSE This program is free software; you can redistribute it and/or modify it under the same terms as perl itself. =cut 1; # End of Schema.pm __END__