package Fripost::Schema::Type::User; use 5.010_000; use warnings; use strict; use base qw/Net::LDAP/; use Fripost::Schema::Utils; use Fripost::Schema::Type::Domain; 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. 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; my ($username, $domain); ($username, $domain) = split /\@/, $user->{username}, 2 if defined $user->{username}; my $base = $self->{_options}->{base_dn}; 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; 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; } return \@res; } # Add the given user sub add { my $self = shift; my $user = shift; my $base = Fripost::Schema::Utils::mkDN ( $self->{_options} , $user->{username} ); if ($self->{_options}->{debug}) { say STDERR "DEBUG: Add base: " .$base; } my $res = $self->{_ldap}->add( $base, attrs => [ objectClass => 'virtualMailbox', userPassword => $user->{userPassword}, isActive => $user->{isActive} ] ); die "Error: " .$res->error. "\n" if $res->code; return $res; } # Change password sub passwd { my $self = shift; my $user = shift; my $base = Fripost::Schema::Utils::mkDN ( $self->{_options} , $user->{username} ); if ($self->{_options}->{debug}) { say STDERR "DEBUG: Modify base: " .$base; } my $res = $self->{_ldap}->modify( $base, replace => [ userPassword => $user->{userPassword} ] ); die "Error: " .$res->error. "\n" if $res->code; return $res; } ####################################################################### 1; =head1 NAME Fripost::Schema::Type::User - =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 User.pm __END__