Entry

"In priority AX2012. Spread useful projects and materials for the development of business applications. Good luck!"

пятница, 27 декабря 2013 г.

Electronic signature and AX. Part 2: Create signature relying COM [AX2012]

Make simple electronic signature relying on COM object Cryptopro:
(Download object: http://www.cryptopro.ru/downloads)
str makeSignature(str   _dataForSign)
{
    COM             SignedData;
    COM             Signer;
    COM             SignerAuthenticatedAttributes;
    COM             oStore;
    COM             oStoreCertificates;
    COM             TimeAttribute;
    COM             oCertificates;
    COM             SelectedCertificate;
    COMVariant      oCertificate;
    COM             certificate;  
    str             sSignedMessage;
    str             ret;
    date            todayDate        = systemDateGet();
    utcDateTime     todayUTCDateTime = DateTimeUtil::getSystemDateTime();
    
    COM createObject(str _className)
    {
        COM COM;
        COM = new COM(_className);
        if (com != null)
        {
            return COM;
        }
        else
        {
            error("COM object is not initialized!");
            return null;
        }
    }

    //Init objects
    SignedData    = createObject("CAPICOM.SignedData");
    Signer        = createObject("CAPICOM.Signer");
    oStore        = createObject("CAPICOM.Store");
    TimeAttribute = createObject("CAPICOM.Attribute");

    //Find certificates
    try
    {
        //Open library
        oStore.Open(#CAPICOM_CURRENT_USER_STORE, #CAPICOM_MY_STORE, #CAPICOM_STORE_OPEN_READ_ONLY);
        oStoreCertificates = oStore.Certificates();
        //Filters
        oStoreCertificates = oStoreCertificates.Find(#CAPICOM_CERTIFICATE_FIND_KEY_USAGE, #CAPICOM_DIGITAL_SIGNATURE_KEY_USAGE);
        oStoreCertificates = oStoreCertificates.Find(#CAPICOM_CERTIFICATE_FIND_TIME_VALID);
        oStoreCertificates = oStoreCertificates.Find(#CAPICOM_CERTIFICATE_FIND_EXTENDED_PROPERTY, #CERT_KEY_SPEC_PROP_ID);
        oCertificates = oStoreCertificates;
    }
    catch(Exception::Error)
    {
        throw error("Find error!");
    }

    //If certificate selected
    if (oCertificates.Count() >= 1)
    {
        try
        {
            //Set signed data
            SignedData.Content(_dataForSign);
            //Open window to select certificate
            SelectedCertificate = oCertificates.Select();
            oCertificate = SelectedCertificate.Item(1);
            //Custom check to valid user serial num
            certificate = COM::createFromVariant(oCertificate);
            if(MyUserInfoTable::find(curUserId()).SerialNum != certificate.SerialNumber())
            {
                throw error("Check error");
            }
            //Configurate object
            Signer.Certificate(oCertificate);
            Signer.Options(1);
            //Create signature on BASE64 cod.
            sSignedMessage = SignedData.Sign(Signer, true, #CAPICOM_ENCODE_BASE64);
            ret = sSignedMessage;
        }
        catch(Exception::Error)
        {
            this.getErrorMessage("Create signature error!");
        }
    }
    else
    {
        this.getErrorMessage("Certificate not selected!");
    }

    //Cleare cashe
    TimeAttribute = null;
    Signer = null;
    oCertificate = null;
    SignedData = null;
    oCertificates = null;
    oStore.Close();
    oStore = null;

    return ret;
}

Electronic signature and AX. Part 1: Check Office file on exists signatures [AX2012]

How check to exists signatures in Word or Excel file:
void veryfyDocuFile(Common  _common)
{
    DocuRef                 docuRef;
    ComExcelDocument_RU     excelDocument;
    ComWordDocument_RU      wordDocument;
    COM                     document;
    COM                     signatures;
    COM                     sign;
    FilePath                filePath;
    Filename                fileName;
    str endSlash(str _str)
    {
        return (strscan(_str, '\\',strlen(_str),-1)) ? _str : _str + '\\';
    }
    
    while select docuRef
        order by TypeId desc
        where docuRef.RefCompanyId == _common.dataAreaId
           && docuRef.RefTableId   == _common.TableId
           && docuRef.RefRecId     == _common.RecId
    {
        if (docuRef.isValueAttached())
        {
            if(docuRef.docuType().FilePlace != DocuFilePlace::Database && docuRef.docuValue().Type != DocuValueType::URL)
            {
                fileName = docuRef.completeFilename();
            }
            else if(docuRef.docuType().FilePlace == DocuFilePlace::Database)
            {
                fileName = DocuActionFile::saveTempFile(docuRef);
            }
            try
            {
                switch(docuRef.docuValue().FileType)
                {
                    case 'xls', 'xlsm', 'xlsx', 'xltm':
                        excelDocument = new ComExcelDocument_RU();
                        #StartSafeCall_RU
                        excelDocument.open(fileName, false);
                        #EndSafeCall_RU
                        if(excelDocument.getComDocument() != null)
                            document = excelDocument.getComDocument();
                        if(document)
                        {
                            #StartSafeCall_RU
                            signatures = document.Signatures();
                            #EndSafeCall_RU
                        }
                        if(signatures && signatures.Count() >= 1)
                            info(strFmt("Docu %1 have a sign!", docuRef.docuValue().OriginalFileName));
                        else
                            info(strFmt("Docu %1 don't have a sign!", docuRef.docuValue().OriginalFileName));
                        excelDocument.saved(false);
                        excelDocument.closeDocument();
                        excelDocument.Quit();
                        break;
                    case 'doc', 'docx':
                        wordDocument = new ComWordDocument_RU();
                        wordDocument.open(fileName, true);
                        document = wordDocument.getComDocument();
                        signatures = document.Signatures();
                        if(signatures.Count() >= 1)
                            info(strFmt("Docu %1 have a sign!", docuRef.docuValue().OriginalFileName));
                        else
                            info(strFmt("Docu %1 don't have a sign!", docuRef.docuValue().OriginalFileName));
                        wordDocument.saved(false);
                        wordDocument.closeDocument();
                        wordDocument.quitApplication();
                        break;
                    default : error("Check error");
                }
            }
            catch(Exception::Error)
            {
                error('ExceptionError');
            }
        }
        else
            info("@SYS26665");
    }
}

понедельник, 10 июня 2013 г.