From 8663144f1f5a3d163119f17a7f9c06655e32727a Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Tue, 17 Apr 2012 01:25:28 +0200 Subject: OO Perl library for our LDAP schema. --- lib/Fripost/Schema/Search.pm | 111 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 lib/Fripost/Schema/Search.pm (limited to 'lib/Fripost/Schema/Search.pm') diff --git a/lib/Fripost/Schema/Search.pm b/lib/Fripost/Schema/Search.pm new file mode 100644 index 0000000..30d92d6 --- /dev/null +++ b/lib/Fripost/Schema/Search.pm @@ -0,0 +1,111 @@ +package Fripost::Schema::Search; + +use 5.010_000; +use warnings; +use strict; + +use Fripost::Schema::Type; + +use base qw/Net::LDAP::Search/; +our $VERSION = '0.01'; + +# Count the entries got out from the query. +sub count { $_[0]->{_res}->count } + +# Create a hash out of the LDAP entry. Keys depend on the context +# of the object. +# The value can be an array reference (multi-valued attributes) or +# a scalar (otherwise). +sub entries { + my $self = shift; + + my $dumpEntry; + if ( $self->{_type} == MAILBOX ) { + $dumpEntry = "_userEntry"; + } + elsif ( $self->{_type} == DOMAIN ) { + $dumpEntry = "_domainEntry"; + } + elsif ( $self->{_type} == ALIAS ) { + $dumpEntry = "_aliasEntry"; + } + else { + die "Something weird happened. Please report." + } + + no strict "refs"; + return (map {&$dumpEntry($_)} $self->{_res}->entries); +} + + +sub _userEntry { + my $entry = shift; + my %user; + &_get_values( $entry, \%user, 'username', 'uid'); + map { &_get_values($entry, \%user, $_) } + qw /maildir isActive userPassword/; + return \%user; +} + +sub _domainEntry { + my $entry = shift; + my %domain; + &_get_values( $entry, \%domain, 'domain', 'dc'); + &_get_values( $entry, \%domain, 'isActive'); + my $parent = &_get_dn($entry)->[1]; + unless ($parent eq 'ou=domains') { + $domain{owner} = (split /=/, $parent, 2)[1]; + } + return \%domain; +} + +sub _aliasEntry { + my $entry = shift; + my %alias; + &_get_values( $entry, \%alias, 'address', 'mailLocalAddress'); + &_get_values( $entry, \%alias, 'goto', 'mailTarget'); + &_get_values( $entry, \%alias, 'isActive'); + return \%alias; +} + +sub _get_values { + my ($entry, $h, $attr, $attr2) = @_; + $attr2 //= $attr; + my $values = $entry->get_value ( $attr2, asref => 1 ); + + return unless defined $values; + if ($#$values == 0) { + $h->{$attr} = $values->[0]; + } + else { + $h->{$attr} = $values; + } +} + +sub _get_dn { + return [split ',', $_[0]->dn()]; +} + + +=head1 NAME + +Fripost::Schema::Search - Class for the result of LDAP queries. + +=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 Search.pm + +__END__ -- cgit v1.2.3