sl@0: // Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #ifndef __SQLSECURITYIMPL_H__ sl@0: #define __SQLSECURITYIMPL_H__ sl@0: sl@0: #include sl@0: #include "SqlBufFlat.h" //RSqlBufFlat sl@0: sl@0: //Forward declarations sl@0: class TSqlSecurityPolicyIterator; sl@0: sl@0: /** sl@0: CSqlSecurityPolicy class implements RSqlSecurityPolicy. sl@0: sl@0: Current CSqlSecurityPolicy implementation uses a flat buffer (see RSqlBufFlat class) as a storage for the database sl@0: objects security policies. sl@0: By default the flat buffer has threee entries: sl@0: - entry 0 is a system entry and keeps the count of the database security entries; sl@0: - entry 1 is reserved for the default database security policy. This is a nameless entry; sl@0: - entry 2 is reserved for the database security policies: read, write and schema. This is a nameless entry; sl@0: The flat buffer may have more entries if database object security policies are added. In this case each flat buffer entry sl@0: has a 16-bit encoded database object name, kept at the end of the entry, after the database object security policy data and sl@0: database object name length. sl@0: Note: Current CSqlSecurityPolicy implementation can have no more than 13 database object security policy entries. sl@0: An access to the flat buffer can be obtained via CSqlSecurityPolicy::BufFlat() methods (const and non-const). sl@0: There are two ways to externalize (resp. internalize) CSqlSecurityPolicy data: sl@0: - (1) using CSqlSecurityPolicy::ExternalizeL()/CSqlSecurityPolicy::InternalizeL(); sl@0: - (2) using CSqlSecurityPolicy::BufFlat() to get an access to the flat buffer and then externalize/internalize the buffer. sl@0: CSqlSecurityPolicy class has only one data member which is the flat buffer; sl@0: sl@0: TSqlSecurityPolicyIterator class gives a convinient way to access the security policies kept inside the flat buffer. sl@0: The given access is a read-only. sl@0: sl@0: @see RSqlSecurityPolicy sl@0: @see RSqlBufFlat sl@0: @see TSqlSecurityPolicyIterator sl@0: @see RSqlSecurityPolicy::TPolicyType sl@0: @see RSqlSecurityPolicy::TObjectType sl@0: @see CSqlSecurityPolicy::NewL() sl@0: @see CSqlSecurityPolicy::NewLC() sl@0: @see CSqlSecurityPolicy::SetDefaultPolicy() sl@0: @see CSqlSecurityPolicy::SetDbPolicy() sl@0: @see CSqlSecurityPolicy::SetPolicy() sl@0: @see CSqlSecurityPolicy::DefaultPolicy() sl@0: @see CSqlSecurityPolicy::DbPolicy() sl@0: @see CSqlSecurityPolicy::Policy() sl@0: @see CSqlSecurityPolicy::ExternalizeL() sl@0: @see CSqlSecurityPolicy::InternalizeL() sl@0: @see CSqlSecurityPolicy::BufFlat() sl@0: sl@0: @internalComponent sl@0: */ sl@0: NONSHARABLE_CLASS(CSqlSecurityPolicy) : public CBase sl@0: { sl@0: friend class TSqlSecurityPolicyIterator; sl@0: struct TPolicyItem; sl@0: sl@0: public: sl@0: static CSqlSecurityPolicy* NewL(const TSecurityPolicy& aDefaultPolicy); sl@0: static CSqlSecurityPolicy* NewLC(const TSecurityPolicy& aDefaultPolicy); sl@0: virtual ~CSqlSecurityPolicy(); sl@0: void SetDefaultPolicy(const TSecurityPolicy& aPolicy); sl@0: void SetDbPolicy(RSqlSecurityPolicy::TPolicyType aPolicyType, const TSecurityPolicy& aPolicy); sl@0: TInt SetPolicy(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName, RSqlSecurityPolicy::TPolicyType aPolicyType, const TSecurityPolicy& aPolicy); sl@0: TSecurityPolicy DefaultPolicy() const; sl@0: TSecurityPolicy DbPolicy(RSqlSecurityPolicy::TPolicyType aPolicyType) const; sl@0: TSecurityPolicy Policy(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName, RSqlSecurityPolicy::TPolicyType aPolicyType); sl@0: TInt Count() const; sl@0: const RSqlBufFlat& BufFlat() const; sl@0: RSqlBufFlat& BufFlat(); sl@0: sl@0: private: sl@0: CSqlSecurityPolicy(); sl@0: void ConstructL(const TSecurityPolicy& aDefaultPolicy); sl@0: inline static TInt PolicyType2Index(RSqlSecurityPolicy::TPolicyType aPolicyType); sl@0: inline static TPolicyItem* PolicyItemPtr(const RSqlBufFlat::TCell* aBegin, const RSqlBufFlat::TCell* aCurrent); sl@0: const TPolicyItem* FindPolicyItemPtr(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName) const; sl@0: void SetCount(TInt aCount); sl@0: sl@0: private: sl@0: enum { sl@0: EPolicyTypeCount = 3, //Schema, Read, Write policy types sl@0: EMaxCount = 16, //iBufFlat max element count. The database cannot have more than EMaxCount security policies. sl@0: ECountIdx = 0, //The index of the policy counter sl@0: EDefaultPolicyIdx = 1, //The index of the default policy type in iBufFlat sl@0: EDbPolicyIdx = 2 //The index of the database policy type in iBufFlat sl@0: }; sl@0: sl@0: //The database security policies are kept in objects of TPolicyItem type. sl@0: //If particular TPolicyItem object represents the database security policies, then the memory layout is: sl@0: // [SchemaPolicy] sl@0: // [ReadPolicy] sl@0: // [WritePolicy] sl@0: //If particular TPolicyItem object represents table security policies, then the memory layout is: sl@0: // [SchemaPolicy] sl@0: // [ReadPolicy] sl@0: // [WritePolicy] sl@0: // [The length of the table name - 32-bit integer] sl@0: // [The table name] sl@0: //As you can see, TPolicyItem does not have "name length" and "name" data members. If that is needed, an appropriate block of memory will be allocated for the TPolicyItem object. sl@0: //There SQL platform security interface currently supports only database security policies, but the implementation is capable of handling also table security policies. sl@0: struct TPolicyItem sl@0: { sl@0: enum {EPolicyTypeCount = 3}; //Schema, Read, Write policy types. The same as the declaration above. sl@0: TSecurityPolicy iPolicy[EPolicyTypeCount]; //Database object policies sl@0: //Database object name size will be after the end of TPolicyItem object sl@0: inline TInt* NameSizePtr() sl@0: { sl@0: //See the comments of the TPolicyItem structure above. sl@0: //coverity[overrun-local] sl@0: return reinterpret_cast (reinterpret_cast (iPolicy) + sizeof(iPolicy)); sl@0: } sl@0: inline TInt NameSize() const sl@0: { sl@0: //See the comments of the TPolicyItem structure above. sl@0: //coverity[overrun-local] sl@0: return *reinterpret_cast (reinterpret_cast (iPolicy) + sizeof(iPolicy)); sl@0: } sl@0: //Database object name will be after the end of TPolicyItem object + sizeof(TInt) sl@0: inline const TUint16* NamePtr() const sl@0: { sl@0: //See the comments of the TPolicyItem structure above. sl@0: //coverity[overrun-local] sl@0: return reinterpret_cast sl@0: (reinterpret_cast (iPolicy) + sizeof(iPolicy) + sizeof(TInt)); sl@0: } sl@0: inline TUint16* NamePtr() sl@0: { sl@0: //See the comments of the TPolicyItem structure above. sl@0: //coverity[overrun-local] sl@0: return reinterpret_cast (reinterpret_cast (iPolicy) + sizeof(iPolicy) + sizeof(TInt)); sl@0: } sl@0: inline static TInt CalcSize(TInt aNameLen) sl@0: { sl@0: return sizeof(TPolicyItem) + sizeof(TInt) + aNameLen * sizeof(TUint16); sl@0: } sl@0: }; sl@0: RSqlBufFlat iBufFlat; sl@0: }; sl@0: sl@0: /** sl@0: CSqlSecurityPolicy container read-only iterator. sl@0: sl@0: Usage: sl@0: sl@0: @code sl@0: //Valid CSqlSecurityPolicy instance. sl@0: CSqlSecurityPolicy* securityPolicy; sl@0: ... sl@0: TSqlSecurityPolicyIterator it(*securityPolicy); sl@0: sl@0: //The following variables will be initialized on each iteration sl@0: RSqlSecurityPolicy::TObjectType objectType; sl@0: TPtrC objectName; sl@0: RSqlSecurityPolicy::TPolicyType policyType; sl@0: TSecurityPolicy policy; sl@0: sl@0: while(it.Next(objectType, objectName, policyType, policy)) sl@0: { sl@0: //"objectType" is initialized with the object type (see RSqlSecurityPolicy::TObjectType) sl@0: //"objectName" is initialized with the object name (database table name) sl@0: //"policyType" is initialized with the policy type (see RSqlSecurityPolicy::TPolicyType) sl@0: //"policy" is initialized with the security policy data (see TSecurityPolicy) sl@0: ; sl@0: } sl@0: sl@0: @endcode sl@0: sl@0: @see CSqlSecurityPolicy sl@0: @see RSqlSecurityPolicy::TObjectType sl@0: @see RSqlSecurityPolicy::TPolicyType sl@0: @see TSecurityPolicy sl@0: sl@0: @internalComponent sl@0: */ sl@0: NONSHARABLE_CLASS(TSqlSecurityPolicyIterator) sl@0: { sl@0: public: sl@0: TSqlSecurityPolicyIterator(const CSqlSecurityPolicy& aSqlSecurityPolicy); sl@0: TBool Next(RSqlSecurityPolicy::TObjectType& aObjectType, TPtrC& aObjectName, RSqlSecurityPolicy::TPolicyType& aPolicyType, TSecurityPolicy& aPolicy); sl@0: sl@0: private: sl@0: const RSqlBufFlat::TCell* iBegin; sl@0: const RSqlBufFlat::TCell* iCurrent; sl@0: const RSqlBufFlat::TCell* iEnd; sl@0: TInt iCurPolicyIdx; sl@0: sl@0: }; sl@0: sl@0: #endif//__SQLSECURITYIMPL_H__