package LATMOS::Accounts::Bases::Unix::Group;

use 5.010000;
use strict;
use warnings;

use base qw(LATMOS::Accounts::Bases::Objects);
use LATMOS::Accounts::Log;

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

=head1 NAME

LATMOS::Accounts::Bases::Unix::Group - Group management in Unix Base

=head1 SYNOPSIS

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

=head1 DESCRIPTION

Access and modify group informations in Unix base.

=head1 FUNCTIONS

=cut

sub _get_attr_schema {
    my ($class, $base) = @_;
    {
        sAMAccountName => {
            iname => 'group_name',
            ro => 1,
        },
        gidNumber => {
            iname => 'gid',
        },
        memberUID => {
            iname => 'user_list',
            multiple => 1,
            delayed => 1,
        },
    };
}

=head2 new(%config)

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 new {
    my ($class, $base, $id, @args) = @_;

    if (exists($base->{groups}{$id})) {
        if ($base->{groups}{$id}{gid} < ($base->{min_gid} || 0)) {
            $base->log(LA_DEBUG, "gid %d less than minimal %d", $base->{groups}{$id}{gid}, ($base->{min_gid} || 0));
            return;
        }
        return bless($base->{groups}{$id}, $class);
    } else {
        $base->log(LA_ERR, "Cannot find group $id");
        return
    }
}

sub get_field {
    my ($self, $field) = @_;
    if ($field eq 'user_list') {
        return [ keys %{ $self->{'users'} || {} } ];
    } elsif ($field eq 'group_name') {
        return $self->id;
    }

    $self->{$field}
}

sub set_fields {
    my ($self, %gdata) = @_;

    my %data;

    foreach my $attr (keys %gdata) {
        if (my $oattr = $self->attribute($attr)) {
            $data{ $oattr->iname } = $gdata{$attr};
        } else {
            $data{$attr} = $gdata{$attr};
        }   
    }

    foreach my $field (keys %data) {
        $field eq 'gid' and do {
            if (!(defined($data{$field}) && $data{$field} =~ /^\d+$/)) {
                $self->base->log(LA_ERR,
                    "Invalid Group data for $field: " . (defined($data{$field}) ? $data{$field} : ''));
                return;
            }
        };
    }
    foreach my $field (keys %data) {
        # TODO check fields exists !
        if ($field eq 'user_list') {
            $self->{'users'} = {};
            foreach (ref $data{$field} ? (@{ $data{$field} || [] }) : ($data{$field})) {
                defined($_) or next;
                $self->{'users'}{$_} = 1;
            }
        } else {
            $self->{$field} = $data{$field};
        }
    }
    1;
}

1;

__END__

=head1 SEE ALSO

L<LATMOS::Accounts::Bases::Objects>
L<LATMOS::Accounts::Bases::Unix>

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENSE

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