## OpenCA - Command
## (c) 1998-2001 by Massimiliano Pala and OpenCA Group
## (c) Copyright 2002-2004 The OpenCA Project
##
##   File Name: deleleCRR
##       Brief: Delete a CRR
##     Version: $Revision: 1.6.4.2 $
## Description: Move a request to the DELETED_CRR dB
##  Parameters: key, dataType

use strict;

sub cmdDeleteCRR {

our ($query, $tools, $db);

## Get the parameters
my $key		= $query->param('key');
my $dataType	= $query->param('dataType');

my $req = $db->getItem( DATATYPE=>$dataType, KEY=>$key);
if ( not $req ) {
	configError (i18nGettext ("No __KEY__ found in __DATATYPE__!",
                              "__KEY__", $key,
                              "__DATATYPE__", $dataType));
}

$req->getParsed()->{HEADER}->{DELETED} = $tools->getDate();
$req->getParsed()->{HEADER}->{OPERATOR} = 
				( $ENV{'SSL_CLIENT_M_SERIAL'} or undef );

my $cert_serial = $req->getParsed()->{REVOKE_CERTIFICATE_SERIAL};

if ( not $db->updateStatus( DATATYPE=>$dataType, 
			NEWTYPE=>"DELETED_CRR", OBJECT=>$req)) {
	configError (i18nGettext ("Error changing CRR's status to DELETED (__KEY__)!", "__KEY__", $key));
}

my @crrList = $db->searchItems (DATATYPE => "PENDING_CRR",
                                REVOKE_CERTIFICATE_SERIAL => $cert_serial);
if (!scalar @crrList) {
    @crrList = $db->searchItems (DATATYPE => "APPROVED_CRR",
                                 REVOKE_CERTIFICATE_SERIAL => $cert_serial);
}
if (!scalar @crrList) {
    @crrList = $db->searchItems (DATATYPE => "ARCHIVED_CRR",
                                 REVOKE_CERTIFICATE_SERIAL => $cert_serial);
}

if (!scalar @crrList) {
    my $cert = $db->getItem (DATATYPE => "CERTIFICATE", KEY => $cert_serial);

    if ($cert) {
        $db->updateStatus (DATATYPE => "SUSPENDED_CERTIFICATE",
                           NEWTYPE => "VALID_CERTIFICATE",
                           OBJECT => $cert);
    }
}

## Send Success Page
success (gettext ("Certificate Revocation Request Successfully deleted."));

}

1;
