package LATMOS::Accounts::Bases::Ldap::Onlyaddress;

use 5.010000;
use strict;
use warnings;

use base qw(LATMOS::Accounts::Bases::Ldap::objects);
use Net::LDAP;
use Net::LDAPS;
use Net::LDAP::Entry;
use Net::LDAP::Control::Paged;
use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED ); 
use Net::LDAP::Util     qw( escape_filter_value );
use LATMOS::Accounts::Log;

our $VERSION = (q$Rev: 649 $ =~ /^Rev: (\d+) /)[0];

=head1 NAME

LATMOS::Ldap - Perl extension for blah blah blah

=head1 SYNOPSIS

  use LATMOS::Ldap;
  blah blah blah

=head1 DESCRIPTION

Stub documentation for LATMOS::Ldap, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.

Blah blah blah.

=head1 FUNCTIONS

=cut

sub _class_filter { '(&(ObjectClass=inetOrgPerson))' }

sub _key_attr { 'cn' } 

sub _my_ldap_classes { qw(
    top
    inetOrgPerson
    organizationalPerson
) }

sub _get_attr_schema {
    my ($class, $base) = @_;
    my $info = {
        # inetOrgPerson
        displayName => {},
        givenName => {},
        initials => {},
        # mail => {},
        sn => {},
        mobile => {},
        o => {},
        uid => {},
        facsimileTelephoneNumber => {},
        # organizationalPerson
        street => {},
        postOfficeBox => {},
        postalCode => {},
        postalAddress => {},
        streetAddress => {},
        physicalDeliveryOfficeName => {},
        ou => {},
        st => {},
        l => {},
        telephoneNumber => {},
        cn => { ro => 1 },
        dn => { ro => 1 },
        objectClass => { ro => 1 },
    };

    $info->{$class->_key_attribute($base)}{ro} = 1;
    $info->{$class->_dn_attribute($base)}{ro} = 1;

    $info
}

sub is_supported {
    my ($class, $base) = @_;
    return $base->config('onlyaddress_container') ? 1 : 0;
}

sub _create {
    my ($class, $base, $id, %data) = @_;

    my $entry = Net::LDAP::Entry->new();

    $entry->dn(join(',',
        sprintf('%s=%s',
            $class->_dn_attribute($base),
            escape_filter_value($id)),
            $base->object_base_dn($class->type),
    ));
    $entry->replace(objectClass =>
        [ $class->_my_ldap_classes ],);
    $data{sn} ||= $id; # sn is mandatory
    $data{$class->_key_attribute($base)} = $id;
    foreach (keys %data) {
        $class->_populate_entry($entry,
            $_, $data{$_}, $base);
    }

    my $msg = $base->ldap->add($entry);
    if ($msg->code) {
    	$base->log(LA_ERR, "Cannot create onlyaddress: %s", $msg->error);
    	return 0;
    }
    return 1;
}

sub get_field {
    my ($self, $field) = @_;

    $field eq 'streetAddress' and $field = 'street';
    $self->SUPER::get_field($field);
}

sub _populate_entry {
    my ($self, $entry, $f, $val, $base) = @_;
    $base ||= $self->base;
    for ($f) {
        /^sn$/ and $val ||= $entry->get_value($self->_key_attribute($base));
    }
    $self->SUPER::_populate_entry($entry, $f, $val, $base);
}

1;

__END__

=head1 SEE ALSO

=head1 AUTHOR

Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008 CNRS SA/CETP/LATMOS

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.

=cut
