diff options
author | Guilhem Moulin <guilhem.moulin@fripost.org> | 2012-05-28 14:27:08 +0200 |
---|---|---|
committer | Guilhem Moulin <guilhem.moulin@fripost.org> | 2012-05-28 14:29:53 +0200 |
commit | ea06aa8c41d103ff161630fd85d083b9ed6f0b41 (patch) | |
tree | 34c017a57cacbf55da29e8b69889a5d8929ee532 /lib/Fripost/Schema | |
parent | 1da2826f3246bd9ecc4a819ac3bc1ef8173893e2 (diff) |
More flexible use of wildcards on usernames.
Diffstat (limited to 'lib/Fripost/Schema')
-rw-r--r-- | lib/Fripost/Schema/Search.pm | 28 | ||||
-rw-r--r-- | lib/Fripost/Schema/Type/User.pm | 45 |
2 files changed, 55 insertions, 18 deletions
diff --git a/lib/Fripost/Schema/Search.pm b/lib/Fripost/Schema/Search.pm index a9eb2ea..1051a21 100644 --- a/lib/Fripost/Schema/Search.pm +++ b/lib/Fripost/Schema/Search.pm @@ -11,7 +11,20 @@ use base qw/Net::LDAP::Search/; our $VERSION = '0.01'; # Count the entries got out from the query. -sub count { $_[0]->{_res}->count } +sub count { + my $self = shift; + + if (ref $self->{_res} eq 'ARRAY') { + my $count; + foreach (@{$self->{_res}}) { + $count += $_->count; + } + return $count; + } + else { + return $self->{_res}->count; + } +} # Create a hash out of the LDAP entry. Keys depend on the context # of the object. @@ -31,11 +44,20 @@ sub entries { $dumpEntry = "_aliasEntry"; } else { - die "Something weird happened. Please report." + die "Something weird happened. Please report."; } + my @entries; + if (ref $self->{_res} eq 'ARRAY') { + foreach (@{$self->{_res}}) { + push @entries, $_->entries; + } + } + else { + @entries = ($self->{_res}->entries); + } no strict "refs"; - return (map {&$dumpEntry($_)} $self->{_res}->entries); + return (map {&$dumpEntry($_)} @entries); } diff --git a/lib/Fripost/Schema/Type/User.pm b/lib/Fripost/Schema/Type/User.pm index 794f5e5..a9eb293 100644 --- a/lib/Fripost/Schema/Type/User.pm +++ b/lib/Fripost/Schema/Type/User.pm @@ -6,6 +6,7 @@ use strict; use base qw/Net::LDAP/; use Fripost::Schema::Utils; +use Fripost::Schema::Type::Domain; our $VERSION = '0.01'; @@ -15,7 +16,9 @@ our $VERSION = '0.01'; # Search a user, and return the corresponding entries if found. If no # user is given, returns all users. # If the user has no domain part, returns matching users for any -# domains. +# domains. Otherwise, we first search for matching domains (we may +# have multiple matches as wildcards are allowed), and then for each of +# them, search for the matching users. sub search { my $self = shift; my $user = shift; @@ -25,26 +28,38 @@ sub search { if defined $user->{username}; my $base = $self->{_options}->{base_dn}; - $base = join ',', ( 'dc='.$domain, $base ) - if defined $domain; + my @bases; + if (defined $domain) { + my $dres = Fripost::Schema::Type::Domain::search ( $self, {domain => $domain} ); + foreach ($dres->entries) { + push @bases, join ',', ( 'dc='.$_->get_value('dc'), $base ); + } + } + else { + push @bases, $base; + } my $filter = "(ObjectClass=virtualMailbox)"; $filter = "(&" .$filter. "(uid=" .$username. ")" .")" if defined $username; - if ($self->{_options}->{debug}) { - say STDERR "DEBUG: Search base: " .$base; - say STDERR "DEBUG: Search filter: " .$filter; + my @res; + foreach my $b (@bases) { + if ($self->{_options}->{debug}) { + say STDERR "DEBUG: Search base: " .$b; + say STDERR "DEBUG: Search filter: " .$filter; + } + + my $res = $self->{_ldap}->search( + base => $b, + scope => 'sub', + attrs => [ 'uid', 'gn' , 'sn', 'isActive' ], + filter => $filter + ); + die "Error: " .$res->error. "\n" if $res->code; + push @res, $res; } - - my $res = $self->{_ldap}->search( - base => $base, - scope => 'sub', - attrs => [ 'uid', 'gn' , 'sn', 'isActive' ], - filter => $filter - ); - die "Error: " .$res->error. "\n" if $res->code; - return $res; + return \@res; } |