package Fripost::Schema::Type::Domain; use 5.010_000; use warnings; use strict; use base qw/Net::LDAP/; use Fripost::Schema::Utils; our $VERSION = '0.01'; ####################################################################### # Search a domain, and return the corresponding entries if found. If no # domain is given, returns all domains. # Filters on values of both keys `domain' and `owner' (unless they are # undefined). # If `owner' is the empty string, search for non self-managed domains # only. sub search { my $self = shift; my $domain = shift; my $owner; $owner = Fripost::Schema::Utils::mkDN ( $self->{_options}, $domain->{owner} ) if defined $domain->{owner}; my @filters = ('(ObjectClass=virtualDomain)'); push @filters, "(dc=" .$domain->{domain}. ")" if defined $domain->{domain}; if (defined $domain->{owner}) { if ($domain->{owner} eq '') { push @filters, "(!(owner=*))"; } else { push @filters, "(owner=" .$owner. ")"; } } my $res = $self->{_ldap}->search( base => $self->{_options}->{base_dn}, scope => 'one', attrs => [ 'dc', 'owner', 'isActive' ], filter => Fripost::Schema::Utils::mkAndFilter( @filters ) ); die "Error: " .$res->error. "\n" if $res->code; return $res; } # Add the given domain. If it already exists, adds the new owner; Or # fails if the new domains is not self-manageable. sub add { my $self = shift; my $domain = shift; my ($base, $owner); $base = Fripost::Schema::Utils::mkDN ( $self->{_options}, $domain->{domain} ); $owner = Fripost::Schema::Utils::mkDN ( $self->{_options}, $domain->{owner} ) if defined $domain->{owner}; my $res; if ($self->search({ domain => $domain->{domain} })->count) { die "Error: Cannot create self-managed domain `" .$domain->{domain}. "' since it already exists.\n" unless defined $domain->{owner}; $res = $self->{_ldap}->modify( $base, add => [ owner => $owner ] ); } else { my @attrs = ( objectClass => 'virtualDomain', , isActive => $domain->{isActive} ); push @attrs, (owner => $owner) if defined $domain->{owner}; $res = $self->{_ldap}->add( $base, attrs => [ @attrs ] ); } die "Error: " .$res->error. "\n" if $res->code; return $res; } ####################################################################### 1; =head1 NAME Fripost::Schema::Type::Domain - =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 Domain.pm __END__