os/persistentdata/persistentstorage/sql/SRC/Security/SqlSecurityImpl.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#ifndef __SQLSECURITYIMPL_H__
sl@0
    17
#define __SQLSECURITYIMPL_H__
sl@0
    18
sl@0
    19
#include <sqldb.h>
sl@0
    20
#include "SqlBufFlat.h"		//RSqlBufFlat
sl@0
    21
sl@0
    22
//Forward declarations
sl@0
    23
class TSqlSecurityPolicyIterator;
sl@0
    24
sl@0
    25
/**
sl@0
    26
CSqlSecurityPolicy class implements RSqlSecurityPolicy.
sl@0
    27
sl@0
    28
Current CSqlSecurityPolicy implementation uses a flat buffer (see RSqlBufFlat class) as a storage for the database 
sl@0
    29
objects security policies.
sl@0
    30
By default the flat buffer has threee entries:
sl@0
    31
 - entry 0 is a system entry and keeps the count of the database security entries;
sl@0
    32
 - entry 1 is reserved for the default database security policy. This is a nameless entry;
sl@0
    33
 - entry 2 is reserved for the database security policies: read, write and schema. This is a nameless entry;
sl@0
    34
The flat buffer may have more entries if database object security policies are added. In this case each flat buffer entry
sl@0
    35
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
    36
database object name length.
sl@0
    37
Note: Current CSqlSecurityPolicy implementation can have no more than 13 database object security policy entries.
sl@0
    38
An access to the flat buffer can be obtained via CSqlSecurityPolicy::BufFlat() methods (const and non-const).
sl@0
    39
There are two ways to externalize (resp. internalize) CSqlSecurityPolicy data:
sl@0
    40
 - (1) using CSqlSecurityPolicy::ExternalizeL()/CSqlSecurityPolicy::InternalizeL();
sl@0
    41
 - (2) using CSqlSecurityPolicy::BufFlat() to get an access to the flat buffer and then externalize/internalize the buffer.
sl@0
    42
       CSqlSecurityPolicy class has only one data member which is the flat buffer;
sl@0
    43
sl@0
    44
TSqlSecurityPolicyIterator class gives a convinient way to access the security policies kept inside the flat buffer.
sl@0
    45
The given access is a read-only.
sl@0
    46
sl@0
    47
@see RSqlSecurityPolicy
sl@0
    48
@see RSqlBufFlat
sl@0
    49
@see TSqlSecurityPolicyIterator
sl@0
    50
@see RSqlSecurityPolicy::TPolicyType
sl@0
    51
@see RSqlSecurityPolicy::TObjectType
sl@0
    52
@see CSqlSecurityPolicy::NewL()
sl@0
    53
@see CSqlSecurityPolicy::NewLC()
sl@0
    54
@see CSqlSecurityPolicy::SetDefaultPolicy()
sl@0
    55
@see CSqlSecurityPolicy::SetDbPolicy()
sl@0
    56
@see CSqlSecurityPolicy::SetPolicy()
sl@0
    57
@see CSqlSecurityPolicy::DefaultPolicy()
sl@0
    58
@see CSqlSecurityPolicy::DbPolicy()
sl@0
    59
@see CSqlSecurityPolicy::Policy()
sl@0
    60
@see CSqlSecurityPolicy::ExternalizeL()
sl@0
    61
@see CSqlSecurityPolicy::InternalizeL()
sl@0
    62
@see CSqlSecurityPolicy::BufFlat()
sl@0
    63
sl@0
    64
@internalComponent
sl@0
    65
*/
sl@0
    66
NONSHARABLE_CLASS(CSqlSecurityPolicy) : public CBase
sl@0
    67
	{
sl@0
    68
	friend class TSqlSecurityPolicyIterator;
sl@0
    69
	struct TPolicyItem;
sl@0
    70
sl@0
    71
public:
sl@0
    72
	static CSqlSecurityPolicy* NewL(const TSecurityPolicy& aDefaultPolicy);
sl@0
    73
	static CSqlSecurityPolicy* NewLC(const TSecurityPolicy& aDefaultPolicy);
sl@0
    74
	virtual ~CSqlSecurityPolicy();
sl@0
    75
	void SetDefaultPolicy(const TSecurityPolicy& aPolicy);
sl@0
    76
	void SetDbPolicy(RSqlSecurityPolicy::TPolicyType aPolicyType, const TSecurityPolicy& aPolicy);
sl@0
    77
	TInt SetPolicy(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName, RSqlSecurityPolicy::TPolicyType aPolicyType, const TSecurityPolicy& aPolicy);
sl@0
    78
	TSecurityPolicy DefaultPolicy() const;
sl@0
    79
	TSecurityPolicy DbPolicy(RSqlSecurityPolicy::TPolicyType aPolicyType) const;
sl@0
    80
	TSecurityPolicy Policy(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName, RSqlSecurityPolicy::TPolicyType aPolicyType);
sl@0
    81
	TInt Count() const;
sl@0
    82
	const RSqlBufFlat&	BufFlat() const;
sl@0
    83
	RSqlBufFlat& BufFlat();
sl@0
    84
sl@0
    85
private:
sl@0
    86
	CSqlSecurityPolicy();
sl@0
    87
	void ConstructL(const TSecurityPolicy& aDefaultPolicy);
sl@0
    88
	inline static TInt PolicyType2Index(RSqlSecurityPolicy::TPolicyType aPolicyType);
sl@0
    89
	inline static TPolicyItem* PolicyItemPtr(const RSqlBufFlat::TCell* aBegin, const RSqlBufFlat::TCell* aCurrent);
sl@0
    90
	const TPolicyItem* FindPolicyItemPtr(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName) const;
sl@0
    91
	void SetCount(TInt aCount);
sl@0
    92
	
sl@0
    93
private:
sl@0
    94
	enum {
sl@0
    95
		 EPolicyTypeCount	= 3,					//Schema, Read, Write policy types
sl@0
    96
		 EMaxCount 			= 16,					//iBufFlat max element count. The database cannot have more than EMaxCount security policies.
sl@0
    97
		 ECountIdx			= 0,					//The index of the policy counter
sl@0
    98
		 EDefaultPolicyIdx	= 1,					//The index of the default policy type in iBufFlat
sl@0
    99
		 EDbPolicyIdx		= 2						//The index of the database policy type in iBufFlat
sl@0
   100
		 };					
sl@0
   101
	
sl@0
   102
	//The database security policies are kept in objects of TPolicyItem type.
sl@0
   103
	//If particular TPolicyItem object represents the database security policies, then the memory layout is:
sl@0
   104
	//    [SchemaPolicy]
sl@0
   105
	//    [ReadPolicy]
sl@0
   106
	//    [WritePolicy]
sl@0
   107
	//If particular TPolicyItem object represents table security policies, then the memory layout is:
sl@0
   108
	//    [SchemaPolicy]
sl@0
   109
	//    [ReadPolicy]
sl@0
   110
	//    [WritePolicy]
sl@0
   111
	//    [The length of the table name - 32-bit integer]
sl@0
   112
	//    [The table name]
sl@0
   113
	//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
   114
	//There SQL platform security interface currently supports only database security policies, but the implementation is capable of handling also table security policies.
sl@0
   115
	struct TPolicyItem
sl@0
   116
		{
sl@0
   117
		enum {EPolicyTypeCount	= 3};				//Schema, Read, Write policy types. The same as the declaration above.
sl@0
   118
		TSecurityPolicy iPolicy[EPolicyTypeCount];	//Database object policies
sl@0
   119
		//Database object name size will be after the end of TPolicyItem object
sl@0
   120
		inline TInt* NameSizePtr() 
sl@0
   121
			{
sl@0
   122
			//See the comments of the TPolicyItem structure above.
sl@0
   123
			//coverity[overrun-local]
sl@0
   124
			return reinterpret_cast <TInt*> (reinterpret_cast <TUint8*> (iPolicy) + sizeof(iPolicy));
sl@0
   125
			}
sl@0
   126
		inline TInt NameSize() const 
sl@0
   127
			{
sl@0
   128
			//See the comments of the TPolicyItem structure above.
sl@0
   129
			//coverity[overrun-local]
sl@0
   130
			return *reinterpret_cast <const TInt*> (reinterpret_cast <const TUint8*> (iPolicy) + sizeof(iPolicy));
sl@0
   131
			}
sl@0
   132
		//Database object name will be after the end of TPolicyItem object + sizeof(TInt)
sl@0
   133
		inline const TUint16* NamePtr() const 
sl@0
   134
			{
sl@0
   135
			//See the comments of the TPolicyItem structure above.
sl@0
   136
			//coverity[overrun-local]
sl@0
   137
			return reinterpret_cast <const TUint16*> 
sl@0
   138
				(reinterpret_cast <const TUint8*> (iPolicy) + sizeof(iPolicy) + sizeof(TInt));
sl@0
   139
			}
sl@0
   140
		inline TUint16* NamePtr() 
sl@0
   141
			{
sl@0
   142
			//See the comments of the TPolicyItem structure above.
sl@0
   143
			//coverity[overrun-local]
sl@0
   144
			return reinterpret_cast <TUint16*> (reinterpret_cast <TUint8*> (iPolicy) + sizeof(iPolicy) + sizeof(TInt));
sl@0
   145
			}
sl@0
   146
		inline static TInt CalcSize(TInt aNameLen) 
sl@0
   147
			{
sl@0
   148
			return sizeof(TPolicyItem) + sizeof(TInt) + aNameLen * sizeof(TUint16);
sl@0
   149
			}
sl@0
   150
		};	
sl@0
   151
	RSqlBufFlat			iBufFlat;
sl@0
   152
	};
sl@0
   153
sl@0
   154
/**
sl@0
   155
CSqlSecurityPolicy container read-only iterator.
sl@0
   156
sl@0
   157
Usage:
sl@0
   158
sl@0
   159
@code
sl@0
   160
//Valid CSqlSecurityPolicy instance.
sl@0
   161
CSqlSecurityPolicy* securityPolicy;
sl@0
   162
...
sl@0
   163
TSqlSecurityPolicyIterator it(*securityPolicy);
sl@0
   164
sl@0
   165
//The following variables will be initialized on each iteration
sl@0
   166
RSqlSecurityPolicy::TObjectType objectType;
sl@0
   167
TPtrC objectName;
sl@0
   168
RSqlSecurityPolicy::TPolicyType policyType;
sl@0
   169
TSecurityPolicy policy;
sl@0
   170
sl@0
   171
while(it.Next(objectType, objectName, policyType, policy))
sl@0
   172
	{
sl@0
   173
	//"objectType" is initialized with the object type (see RSqlSecurityPolicy::TObjectType)
sl@0
   174
	//"objectName" is initialized with the object name (database table name)
sl@0
   175
	//"policyType" is initialized with the policy type (see RSqlSecurityPolicy::TPolicyType)
sl@0
   176
	//"policy" is initialized with the security policy data (see TSecurityPolicy)
sl@0
   177
	<do something with the policy>;
sl@0
   178
	}
sl@0
   179
	
sl@0
   180
@endcode
sl@0
   181
sl@0
   182
@see CSqlSecurityPolicy
sl@0
   183
@see RSqlSecurityPolicy::TObjectType
sl@0
   184
@see RSqlSecurityPolicy::TPolicyType
sl@0
   185
@see TSecurityPolicy
sl@0
   186
sl@0
   187
@internalComponent
sl@0
   188
*/
sl@0
   189
NONSHARABLE_CLASS(TSqlSecurityPolicyIterator)
sl@0
   190
	{
sl@0
   191
public:
sl@0
   192
	TSqlSecurityPolicyIterator(const CSqlSecurityPolicy& aSqlSecurityPolicy);
sl@0
   193
	TBool Next(RSqlSecurityPolicy::TObjectType& aObjectType, TPtrC& aObjectName, RSqlSecurityPolicy::TPolicyType& aPolicyType, TSecurityPolicy& aPolicy);
sl@0
   194
sl@0
   195
private:
sl@0
   196
	const RSqlBufFlat::TCell* 	iBegin;
sl@0
   197
	const RSqlBufFlat::TCell* 	iCurrent;
sl@0
   198
	const RSqlBufFlat::TCell* 	iEnd;
sl@0
   199
	TInt 						iCurPolicyIdx;
sl@0
   200
	
sl@0
   201
	};
sl@0
   202
sl@0
   203
#endif//__SQLSECURITYIMPL_H__