package LATMOS::Accounts::Bases::Ldap::Groupofnames;

use 5.010000;
use strict;
use warnings;

use base qw(LATMOS::Accounts::Bases::Ldap::objects);
use Net::LDAP;
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 is_supported {
    my ($class, $base) = @_;
    return $base->config('groupofnames_container') ? 1 : 0;
}


sub _class_filter { '(ObjectClass=groupOfNames)' }

sub _key_attr { 'cn' } 

sub _my_ldap_classes { qw(top groupOfNames) }

sub _get_attr_schema {
    my ($class, $base) = @_;
    $class->SUPER::_get_attr_schema($base,
    {
        description => { },
        member => {
            mandatory => 1,
            multiple => 1,
            reference => 'user',
        },
        cn => { ro => 1 },
        dn => { ro => 1 },
        objectClass => { ro => 1 },
    }
    );
}

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 ],);
    my %delayed;
    $data{$class->_key_attribute($base)} = $id;
    foreach (keys %data) {
        /^(member)$/ and do {
            my @DNs;
            foreach my $user ( ref $data{$_} ? @{ $data{$_} } : $data{$_} ) {
                my $OUser = $base->get_object('user', $user) or next;
                push(@DNs, $OUser->{entry}->dn);
            }
            $data{$_} = \@DNs;
        };
        $class->_populate_entry($entry, $_, $data{$_});
    }
    my $msg = $base->ldap->add($entry);
    $base->log(LA_ERR, "Cannot create group: %s", $msg->error) if ($msg->code);
    return if ($msg->code);
    if (! keys %delayed) { return 1 };
    my $res = $base->get_object('group', $id)->set_fields(%delayed);
    return defined($res) ? 1 : 0;
}

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

    $field eq 'member' and do {
        my @res;
        foreach my $dn ($self->{entry}->get_value('member')) {
            my $User = $self->base->_get_object_from_dn($dn) or next;
            push(@res, $User->get_value( LATMOS::Accounts::Bases::Ldap::User::_key_attr() ) );
        }
        return [ sort(@res) ];
    };

    $self->SUPER::get_field($field);
}

sub set_fields {
    my ($self, %data) = @_;
    my %ndata;
    while (my ($f, $val) = each(%data)) {
        $f eq 'member' and do {
            my %users;
            $val = [ $val ] unless(ref $val);
            $users{$_}{e} = 1 foreach (grep { defined($_) } @{ $self->get_field('member') || []});
            $users{$_}{n} = 1 foreach (grep { defined($_) } @{ $val || []});
            foreach (keys %users) {
                $users{$_}{e} && $users{$_}{n} and next;
                my $user = $self->base->get_object('user', $_) or next;
                if ($users{$_}{e}) {
                    $self->{entry}->delete(member => $user->get_field('dn'));
                } elsif ($users{$_}{n}) {
                    $self->{entry}->add(member => $user->get_field('dn'));
                } # else {} # can't happen
            }
            next;
        };
        $ndata{$f} = $val;
    }
    $self->SUPER::set_fields(%ndata);
}

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
