package LATMOS::Accounts::Bases::Unix::User;

use 5.010000;
use strict;
use warnings;

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

our $VERSION = (q$Rev: 205 $ =~ /^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('unix');
  ...

=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 new {
    my ($class, $base, $id, @args) = @_;
    # we profit of ref, quite easy
    if (exists($base->{users}{$id}) && $base->{users}{$id}) {
        if ($base->{users}{$id}{uid} < ($base->{min_uid} || 0)) {
            return;
        }
        return bless($base->{users}{$id}, $class);
    } else { return }
}

sub id {
    $_[0]->{account}
}

sub get_field {
    my ($self, $field) = @_;
    for ($field) {
        /^(sn|givenName)$/ and do {
            my $gecos = $self->{gecos} || '';
            my ($given, $sn) = $gecos =~ /^([^, ]+) +([^, ]+)/;
            return $field eq 'sn' ? $sn : $given;
        };
    }
    $self->{$field}
}

sub set_fields {
    my ($self, %data) = @_;
    foreach my $field (keys %data) {
        # TODO check fields exists !
        $field =~ /^(sn|givenName)$/ and next;
        $self->{$field} = $data{$field};
    }
    1;
}

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
