package Fripost::Schema; use 5.010_000; use warnings; use strict; use Fripost::Schema::Search; use Fripost::Schema::Type; use Fripost::Schema::Type::User; use Fripost::Schema::Type::Domain; use Fripost::Schema::Type::Alias; use base qw/Net::LDAP/; our $VERSION = '0.01'; ####################################################################### # Public methods # Change the context of the object. sub user { $_[0]->Fripost::Schema::Type::_set_type(MAILBOX); } sub domain { $_[0]->Fripost::Schema::Type::_set_type(DOMAIN); } sub alias { $_[0]->Fripost::Schema::Type::_set_type(ALIAS); } # Create the oject, and initialize a connection to the LDAP # host. sub new { my $class = shift; my $h = shift; $h->{server_host} //= 'ldap://127.0.0.1:389'; $h->{base_dn} //= ''; my $self = {_options => $h}; bless $self, $class; my $ldap = Net::LDAP->new ( $h->{server_host} ) or die "Error: Cannot initialize connection to LDAP server at `" .$h->{server_host}. "'.\n"; my $mesg; if ( (defined $h->{bind_dn}) and $h->{bind_dn} ne '' ) { my %bind; my $debug = "Binding to DN `" .$h->{bind_dn}. "'"; if (defined $h->{bind_pw}) { $debug .= " (authenticated)."; $bind{password} = $h->{bind_pw}; } else { $debug .= " (unauthenticated)."; $bind{noauth} = 1; } $self->_dsay( $debug ); $mesg = $ldap->bind( $h->{bind_dn}, %bind ); } else { # Anonymous bind $self->_dsay( "Anonymous bind." ); $mesg = $ldap->bind(); } die "Error: " .$mesg->error. "\n" if $mesg->code; $self->{_ldap} = $ldap; return $self; } # Search. Returns a `Fripost::Schema::Search'. sub search { my $self = shift; my $res; if ( $self->{_type} == MAILBOX ) { $res = $self->Fripost::Schema::Type::User::search(@_) } elsif ( $self->{_type} == DOMAIN ) { $res = $self->Fripost::Schema::Type::Domain::search(@_) } elsif ( $self->{_type} == ALIAS ) { $res = $self->Fripost::Schema::Type::Alias::search(@_) } else { die "Something weird happened. Please report." } my $result = {_res => $res, _type => $self->{_type}}; bless $result, 'Fripost::Schema::Search'; return $result; } sub add { my $self = shift; if ( $self->{_type} == MAILBOX ) { $self->Fripost::Schema::Type::User::add(@_) } elsif ( $self->{_type} == DOMAIN ) { $self->Fripost::Schema::Type::Domain::add(@_) } elsif ( $self->{_type} == ALIAS ) { $self->Fripost::Schema::Type::Alias::add(@_) } else { die "Something weird happened. Please report."; } } sub passwd { my $self = shift; if ( $self->{_type} == MAILBOX ) { $self->Fripost::Schema::Type::User::passwd(@_); } elsif ( $self->{_type} == DOMAIN ) { die "Cannot change the password of a domain."; } elsif ( $self->{_type} == ALIAS ) { die "Cannot change the password of an alias."; } else { die "Something weird happened. Please report."; } } # Disconnect to the LDAP server. sub unbind { $_[0]->_dsay( "Unbinding from the LDAP server." ); $_[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__