package LATMOS::Accounts::Bases::OCHelper::User;

# $Id: User.pm 2932 2010-08-10 17:19:21Z nanardon $

use strict;
use warnings;
use base qw(LATMOS::Accounts::Bases::OCHelper);
use LATMOS::Accounts::Utils;
use LATMOS::Accounts::I18N;

=head1 NAME

LATMOS::Accounts::Bases::OCHelper::User -
Helper to create user in L<LATMOS::Accounts> system

=cut

sub _step {
    my ($self, $info) = @_;
    if ($info->{step} <= 0) {
        $info->{name}{ask} = 1;
        push(@{$info->{ask}}, qw(givenName sn));
        $info->{step} = 1;
        if (!($info->{name}{content} || 
              ($info->{contents}{sn}) && $info->{contents}{givenName})) {
            return 'NEEDINFO';
        }
    } 
    if ($info->{step} == 1) {
        $info->{step} = 2;
        my $login = $info->{name}{content};
        if (!$login) {
            $info->{name}{ask} = 1;
            # Try to find a login:
            
            if ($info->{contents}{sn}) {

                # On regarde si on objet de même nom existe:
                if ($info->{contents}{givenName}) {
                    if (my @objs = $self->base->search_objects(
                            'user',
                            'givenName~' . $info->{contents}{givenName},
                            'sn~' . $info->{contents}{sn},
                            'oalias=NULL' ) ) {
                        $info->{duplicates} = \@objs;
                    }
                }
                my $login = buildLogin(
                    sub {
                        !$self->base->get_object('user', $_[0]);
                    },
                    $info->{contents}{sn},
                    $info->{contents}{givenName},
                );
                if ($login) {
                    $info->{name}{content} = $login;
                }
            }
        } else {
            $self->base->get_object('user', $login) and do {
                $info->{name}{ask} = 1;
            }
        }
        push(@{$info->{ask}}, 'uidNumber');
        push(@{$info->{ask}}, 'gidNumber');

       my %default = $self->base->compute_default(
            'user', $login || '',
            %{ $info->{contents} || {} }
        );

        if ($default{mail}) {
            push(@{$info->{ask}}, 'mail');
            $info->{contents}{mail} ||= $default{mail};
        }
        if (my $attr = $self->base->attribute('user', 'aliases')) {
            if (!$attr->ro) {
                push(@{$info->{ask}}, 'aliases');
                $info->{contents}{aliases} ||= $default{aliases};
            }
        }
        if (my $attr = $self->base->attribute('user', 'revaliases')) {
            if (!$attr->ro) {
                push(@{$info->{ask}}, 'revaliases');
                $info->{contents}{revaliases} ||= $default{revaliases};
            }
        }
        return 'NEEDINFO';


    } elsif ($info->{step} == 2) {
        if ($self->base->create_c_object($self->otype,
                $info->{name}{content},
                %{$info->{contents} || {}},
            )) {
            return 'CREATED';
        } else {
            return 'ERROR';
        }
    } else {
        return undef;
    }
}

1;

__END__

=head1 SEE ALSO

L<LATMOS::Accounts::Bases::OCHelper>

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2012 by Thauvin Olivier

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
