{ secd.pas } program SecD; {$IfDef VER80} uses SnipTool, SnipData, SysUtils, WinTypes, WinProcs, DbiProcs, DbiTypes, DbiErrs; {$Else} uses WinTypes, WinCrt, Strings, DbiProcs, DbiTypes, DbiErrs, SnipTool, SnipData; {$EndIf} const szTblName = 'SecDesc'; { Name of table to be created. } szTblType = szPARADOX; { Table type to use. } { Field Descriptor used in creating a table } fldDes: array[0..3] of FLDDesc = ( ( { Field 1 - AUTOINC } iFldNum: 1; { Field Number } szName: 'Status ID'; { Field Name } iFldType: fldFLOAT; { Field Type } iSubType: fldUNKNOWN; { Field Subtype } iUnits1: 0; { Field Size } iUnits2: 0; { Decimal places ( 0 ) } iOffset: 0; { Offset in record ( 0 ) } iLen: 0; { Length in Bytes ( 0 ) } iNullOffset: 0; { For Null Bits ( 0 ) } efldvVchk: fldvNOCHECKS; { Validiy checks ( 0 ) } efldrRights: fldrREADWRITE { Rights } ), ( { Field 2 - ALPHA } iFldNum: 2; szName: 'Order No'; iFldType: fldFLOAT; iSubType: fldUNKNOWN; iUnits1: 0; iUnits2: 0; iOffset: 0; iLen: 0; iNullOffset: 0; efldvVchk: fldvNOCHECKS; efldrRights: fldrREADWRITE ), ( { Field 3 - DATE SENT} iFldNum: 3; szName: 'Date Sent'; iFldType: fldDATE; iSubType: fldUNKNOWN; iUnits1: 0; iUnits2: 0; iOffset: 0; iLen: 0; iNullOffset: 0; efldvVchk: fldvNOCHECKS; efldrRights: fldrREADWRITE ), ( { Field 4 - DATE } iFldNum: 4; szName: 'Date Received'; iFldType: fldDATE; iSubType: fldUNKNOWN; iUnits1: 0; iUnits2: 0; iOffset: 0; iLen: 0; iNullOffset: 0; efldvVchk: fldvNOCHECKS; efldrRights: fldrREADWRITE ) ); { Array of field descriptors } secDes: array[0..0] of SECDesc = ( ( iSecNum: 1; { # to Identify Descriptor } eprvTable: prvREADONLY; { Table Priviledges } iFamRights: NOFAMRIGHTS; { Family Rights } szPassword: 'test' { Password } { Field Level Priviledges } ) ); { Define the operation on the table: add the security descriptor to the table. } crOpTyp: CROpType = ( crADD ); {===================================================================== Function: AddRecord (hDBICur hCur, FLOAT fStatus, FLOAT fOrder) Input: hCur - Cursor to which the record will be added fStatus - Value to write to the Status ID field fOrder - Value to write to the Order ID field Return: Success of the function. Description: This function is used to add a record to the table. Generates random data for two date fields. Those fields are: DateSent - Value to write to the Date Sent field and DateDelivered - Value to write to the Date Received field ===================================================================== } Function AddRecord (hCur: hDBICur; fStatus: FLOAT; fOrder: FLOAT): DBIResult; var rslt: DBIResult; { Return value from IDAPI functions } pRecBuf: pBYTE; { Record Buffer } TblProps: CURProps; { The properties of the table } uMonth: UINT16; { Contains the month portion of the date } uDay: UINT16; { Contains the day portion of the date } iYear: INT16; { Contains the year portion of the date } szTemp: pCHAR; { Temporary string used in parsing the date } sDate: DATE; { Date structure - used in DbiPutField. } begin { Get the Record Size for the table } ChkRslt(DbiGetCursorProps(hCur, TblProps), DBIERR_NONE, ' Error - GetCursorProps.'); { Allocate a record buffer } GetMem(pRecBuf, TblProps.iRecBufSize * sizeof(pBYTE)); if not Assigned (pRecBuf) then begin Screen(' Error - Out of Memory.'); AddRecord := DBIERR_NOMEMORY; exit; end; { Make sure we're starting with a clean record buffer } ChkRslt(DbiInitRecord(hCur, pRecBuf), DBIERR_NONE, ' Error - InitRecord.'); { Add the Status ID to the Record Buffer } ChkRslt(DbiPutField(hCur, 1, pRecBuf, @(fStatus)), DBIERR_NONE, ' Error - PutField.'); { Add the Order ID to the Record Buffer } ChkRslt(DbiPutField(hCur, 2, pRecBuf, @(fOrder)), DBIERR_NONE, ' Error - PutField.'); { Randomly generate & encode a date } uMonth := random(11) + 1; uDay := random(20) + 1; iYear := random(98) + 1900; ChkRslt(DbiDateEncode(uMonth, uDay, iYear, sDate), DBIERR_NONE, ' Error - DateEncode.'); { Put the data into the record buffer } rslt := DbiPutField(hCur, 3, pRecBuf, @(sDate)); { Randomly generate & encode a date } uMonth := random(11) + 1; uDay := random(20) + 1; iYear := random(98) + 1900; { Encode the date } ChkRslt(DbiDateEncode(uMonth, uDay, iYear, sDate), DBIERR_NONE, ' Error - DateEncode.'); { Put the data into the record buffer } rslt := DbiPutField(hCur, 4, pRecBuf, @(sDate)); { Insert the record into the table } rslt := ChkRslt(DbiInsertRecord(hCur, dbiNOLOCK, pRecBuf), DBIERR_NONE, ' Error - InsertRecord.'); FreeMem(pRecBuf, TblProps.iRecBufSize * sizeof(pBYTE)); AddRecord := rslt; end; {===================================================================== Code: SecDesc(); Desccription: This function shows how to use Auxilary Passwords with a Paradox Table. Note that Auxillary Passwords are only supported by Paradox Tables. ===================================================================== } var hDb: hDBIDb; { Handle to the database } hCur: hDBICur; { Handle to the table } TblDesc: CRTblDesc; { Create table descriptor } uNumFields, uNumDescs: UINT16; Count: Integer; const uDispNumRecs: UINT16 = 10; { Number of records to add and display } szPassword = 'MyPass'; { Password for the table } begin Screen('*** Security Descriptor Example ***'); Screen(' Initializing IDAPI...'); if (InitAndConnect(hDb) <> DBIERR_NONE) then begin Screen(''); Screen('*** End of Example ***'); exit; end; { The number of fields in the table - note that fldDesc is defined globally } uNumFields := trunc(sizeof(fldDes) / sizeof(fldDes[0])); { The number of Secutiry Descriptors in the table } uNumDescs := trunc(sizeof(secDes) / sizeof(secDes[0])); { Set the directory for the database handle } Screen(' Setting the default Database directory...'); ChkRslt(DbiSetDirectory(hDb, pCHAR(szTblDirectory)), DBIERR_NONE, ' Error - SetDirectory.'); Screen(' Initializing the table descriptor...'); FillChar(TblDesc, SizeOf(CRTblDesc), #0); StrCopy(TblDesc.szTblName, szTblName); StrCopy(TblDesc.szTblType, szTblType); TblDesc.iFldCount := uNumFields; TblDesc.pfldDesc := @fldDes; TblDesc.bProtected := TRUE; StrCopy(TblDesc.szPassword, szPassword); TblDesc.iSecRecCount := uNumDescs; TblDesc.pecrSecOp := @crOpTyp; { Add Field Level Priviledges } for Count := 0 to (uNumFields-1) do begin if (Count = 0) then secDes[0].aprvFld[Count] := prvNONE else secDes[0].aprvFld[Count] := prvREADONLY; end; TblDesc.psecDesc := @secDes; { Add the master password to the session. This is needed before the create in case the table needs to be overwritten. The master password is also required in order to open the table. } ChkRslt(DbiAddPassword(szPassword), DBIERR_NONE, ' Error - AddPassword.'); Screen(' Creating the "'+szTblName+'" table...'); if (ChkRslt(DbiCreateTable(hDb, TRUE, TblDesc), DBIERR_NONE, ' Error - CreateTable.') <> DBIERR_NONE) then begin Screen(' Close the Database and exit IDAPI...'); CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; Screen(' Open the "'+szTblName+'" table with the master password...'); if (ChkRslt(DbiOpenTable(hDb, pCHAR(szTblName), pCHAR(szTblType), nil, nil, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, nil, hCur), DBIERR_NONE, ' Error - OpenTable.') <> DBIERR_NONE) then begin Screen(' Close the Database and exit IDAPI...'); ChkRslt(DbiDeleteTable(hDb, pCHAR(szTblName), pCHAR(szTblType)), DBIERR_NONE, ' Error - DeleteTable.'); CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; Screen(''); Screen(' Add five records to the table...'); AddRecord(hCur, 1, 1004); AddRecord(hCur, 2, 1005); AddRecord(hCur, 3, 1006); AddRecord(hCur, 4, 1007); AddRecord(hCur, 5, 1008); Screen(''); Screen(' Close the Table...'); ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); { Remove the Master Password from the session } ChkRslt(DbiDropPassword(szPassword), DBIERR_NONE, ' Error - DropPassword.'); { Add the Auxiliary Password to the session } ChkRslt(DbiAddPassword('test'), DBIERR_NONE, ' Error - AddPassword.'); { Open the table. Note that the table has to be opened read only as the security descriptor only supports read operations. } Screen(''); Screen(' Open the "'+szTblName+'" table with the auxiliary password...'); if (ChkRslt(DbiOpenTable(hDb, pCHAR(szTblName), pCHAR(szTblType), nil, nil, 0, dbiREADONLY, dbiOPENSHARED, xltFIELD, FALSE, nil, hCur), DBIERR_NONE, ' Error - OpenTable.') <> DBIERR_NONE) then begin Screen(' Close the Database and exit IDAPI...'); ChkRslt(DbiDeleteTable(hDb, pCHAR(szTblName), pCHAR(szTblType)), DBIERR_NONE, ' Error - DeleteTable.'); CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; ChkRslt(DbiSetToBegin(hCur), DBIERR_NONE, ' Error - SetToBegin.'); Screen(' Display the "'+szTblName+'" table...'); Screen(' Notice that the "Status ID" field is blank:'+ ' the auxiliary password does'); Screen(' not have access to the "Status ID" field'); DisplayTable(hCur, uDispNumRecs); Screen(''); Screen(' Close the Table...'); ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); Screen(' Need to add the master password in order to delete the table.'); Screen(' The auxiliary password does not have sufficient rights'); ChkRslt(DbiAddPassword(szPassword), DBIERR_NONE, ' Error - AddPassword.'); Screen(' Deleting the table...'); ChkRslt(DbiDeleteTable(hDb, pCHAR(szTblName), pCHAR(szTblType)), DBIERR_NONE, ' Error - DeleteTable.'); Screen(' Close the Database and exit IDAPI...'); CloseDbAndExit(hDb); Screen('*** End of Example ***'); end.