#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package AppUser::User::Command::ResetMFA;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use RSApache::Response::Redirect;

use lib '/app/tools/appuser/lib';
use AppUser::User::User;
use AppUser::User::Command::Profile;
use AppUser::User::Form;
use AppUser::Login::Command::MFAAuth;

use lib '/app/tools/rps/lib';
use RPS::Message;
use RPS::Command;
use base 'RPS::Command';

use POSIX;

sub cmd  { return "resetmfa"; }
sub area { return "user"; }

use constant kTemplate => '/app/tools/appuser/templates/user/index.xsl';

sub execute {
    my ($self) = @_;

    # Give any inherited methods the first crack at this.
    # (This checks for login and basic access permissions).
    #
    my $response = $self->SUPER::execute();
    return $response if $response;

    if ( $self->getParam('formSubmit') ) {

        my $userID = $self->getParam('userID');

        my $cdbo = Common::RSApp::GetCommonDB();
        my $sql  = "UPDATE user SET mfa_type=0, mfa_verified=0, mfa_phone=NULL, mfa_secret=NULL WHERE user_id = ?";
        my $sth  = $cdbo->DoCmdWithPlaceholders( $sql, [ $userID ] );

        # Clear the 'remember me' cookie if it exists
        AppUser::Login::Command::MFAAuth->DeleteMFACookie( $userID );

        my $successMessage   = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
        my $command          = AppUser::User::Command::ResetMFA->new( msg => $successMessage->toString, UserID => $userID );
        my $redirectResponse = RSApache::Response::Redirect->new( $command->getRedirect() );

        return $redirectResponse;
    }

    my $id = $self->getParam("UserID");
    my $form = AppUser::User::Form->new( userID => $id, accessLevel => $self->_getUser()->AccessLevel() );

    $self->{xml}{Form} = $form;

    $response = RSApache::Response::XSLT->new( $self->{xml}, kTemplate );
    return $response;

}

1;

