package LATMOS::Accounts::Web::Controller::PwRst;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

LATMOS::Accounts::Web::Controller::PwRst - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

sub begin : Private {
    my ( $self, $c ) = @_;

    $c->model('Accounts')->db->SetConnectedUser('$ROOT');
    $c->stash->{user} = $c->model('Accounts')->db->get_object('user', 'thauvin');
    $c->log->debug('Allow ' . $c->model('Accounts')->db->check_acl($c->stash->{user}, 'userPassword', 'w'));
    
    return 1;
}


=head2 index

=cut

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

    $c->stash->{Nosync} = 1;

    if ( my $user = $c->req->param('user') ) {
        $c->forward('/remote/askpwreset/index');
        if ($c->stash->{data}{err}) {
            $c->log->error(
                sprintf(
                    'Request from %s to reset password for user %s failed: %s',
                    $c->req->address,
                    $user,
                    $c->stash->{data}{err}
                )
            );
        } elsif ( $c->stash->{data}{ok} eq 'Done' ) {
            $c->log->info(
                sprintf(
                    'Request from %s to reset password for user %s Sent',
                    $c->req->address,
                    $user,
                )
            );
        }

        $c->go('reqdone');
    }
}

sub changepw :Path :Args(1) {
    my ( $self, $c, $reqid ) = @_;

    $c->stash->{ReqId} = $reqid;

    my $password  = $c->req->param('password')  || '';
    my $cpassword = $c->req->param('cpassword') || '';

    if ($password) {
        if ($password ne $cpassword) {
            $c->stash->{data}{err} = $c->localize('Password are differents');
            return;
        }

        $c->forward('/remote/pwreset/index');

        if ( $c->stash->{data}{ok} eq 'Done' ) {
            $c->go('changedone');
        }
    }
}

sub reqdone : Private {
    my ( $self, $c ) = @_;
}

sub changedone : Private {
    my ( $self, $c ) = @_;
}

=encoding utf8

=head1 AUTHOR

Olivier Thauvin,Guyancourt-B1428,+33 1 80285052,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
