package LATMOS::Accounts::Bases::Sql::Group;

use 5.010000;
use strict;
use warnings;

use base qw(LATMOS::Accounts::Bases::Sql::objects);

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

=head1 NAME

LATMOS::Ad - Perl extension for blah blah blah

=head1 SYNOPSIS

  use LATMOS::Accounts::Bases;
  my $base = LATMOS::Accounts::Bases->new('sql');
  ...

=head1 DESCRIPTION

Account base access over standard unix file format.

=head1 FUNCTIONS

=cut

=head2 new(%options)

Create a new LATMOS::Ad object for windows AD $domain.

domain / server: either the Ad domain or directly the server

ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>.

=cut

sub object_table { 'group' }

sub key_field { 'name' }

sub has_extended_attributes { 1 }

sub _inline_fields {
    my ($self, $for, $base) = @_;
    return (
        $self->SUPER::_inline_fields($for, $base),
        gidNumber       => 'gidnumber',
        ($for !~ /w/) ? (cn => 'name') : (),
    );
}

sub _managed_fields {
    my ($self, $for, $base) = @_;

    my %fields = (
        memberUID       => 'memberUID',
        member          => 'member',
        $for !~ /w/ ? (
            sAMAccountName  => 'name',
            groupname => 'name',
        ) : (),
    );
    %fields;
}

sub _delayed_fields {
    my ($self)= @_;
    return qw(memberUID member managedBy);
}

sub get_field {
    my ($self, $field) = @_;
    for ($field) {
        /^(member|memberUID)$/ and do {
            my $sth = $self->db->prepare_cached(
                q{
                select value from group_attributes_users
                join "group" on "group".ikey = group_attributes_users.okey
                where name = ? and attr = ?
                }
            );
            $sth->execute($self->id, 'memberUID');
            my @res;
            while (my $res = $sth->fetchrow_hashref) {
                push(@res, $res->{value});
            }
            return \@res;
        };
    }
    $self->SUPER::get_field($field);
}

sub set_fields {
    my ($self, %data) = @_;
    my %fdata;
    my $res = 0;
    foreach my $attr (keys %data) {
        $attr =~ /^memberUID|member$/ and do {
            my %member;
            foreach (@{ $self->get_field('memberUID') }) {
                $member{$_}{c} = 1;
            }
            foreach (ref $data{$attr} ? @{ $data{$attr} || []} : $data{$attr}) {
                $member{$_}{n} = 1;
            }

            foreach (keys %member) {
                $member{$_}{c} && $member{$_}{n} and next; # no change !
                my $user = $self->base->get_object('user', $_) or next;
                if ($member{$_}{n}) {
                    my $sth = $self->db->prepare_cached(
                        q{insert into group_attributes_users (value, attr, okey) values (?,?,?)}
                    );
                    $sth->execute($_, 'memberUID', $self->_get_ikey);
                    $res++;
                } elsif ($member{$_}{c}) {
                    my $sth = $self->db->prepare_cached(
                        q{delete from group_attributes_users where value = ? and attr = ? and okey = ?}
                    );
                    $sth->execute($_, 'memberUID', $self->_get_ikey);
                    $res++;
                } # else {} # can't happend
            }
            next;
        };
        $fdata{$attr} = $data{$attr};
    }
    if (keys %fdata) {
        return $res + $self->SUPER::set_fields(%fdata);
    } else {
        $res
    }
}

1;

__END__

=head1 SEE ALSO

=head1 AUTHOR

Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008, 2009 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
