Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

451 lines
14 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright(C) 1997-1998 Microsoft Corporation all rights reserved.
  4. //
  5. // Module: sdoprofile.cpp
  6. //
  7. // Project: Everest
  8. //
  9. // Description: IAS Server Data Object - Profile Object Implementation
  10. //
  11. // Author: TLP 1/23/98
  12. //
  13. /////////////////////////////////////////////////////////////////////////////
  14. #include "stdafx.h"
  15. // C++ Exception Specification ignored
  16. #pragma warning(disable:4290)
  17. #include <iasattr.h>
  18. #include <attrcvt.h>
  19. #include <varvec.h>
  20. #include "sdoprofile.h"
  21. #include "sdohelperfuncs.h"
  22. #include <attrdef.h>
  23. #include <sdoattribute.h>
  24. #include <sdodictionary.h>
  25. //////////////////////////////////////////////////////////////////////////////
  26. CSdoProfile::CSdoProfile()
  27. : m_pSdoDictionary(NULL)
  28. {
  29. }
  30. //////////////////////////////////////////////////////////////////////////////
  31. CSdoProfile::~CSdoProfile()
  32. {
  33. }
  34. //////////////////////////////////////////////////////////////////////////////
  35. HRESULT CSdoProfile::FinalInitialize(
  36. /*[in]*/ bool fInitNew,
  37. /*[in]*/ ISdoMachine* pAttachedMachine
  38. )
  39. {
  40. CComPtr<IUnknown> pUnknown;
  41. HRESULT hr = pAttachedMachine->GetDictionarySDO(&pUnknown);
  42. if ( SUCCEEDED(hr) )
  43. {
  44. hr = pUnknown->QueryInterface(IID_ISdoDictionaryOld, (void**)&m_pSdoDictionary);
  45. if ( SUCCEEDED(hr) )
  46. {
  47. hr = InitializeCollection(
  48. PROPERTY_PROFILE_ATTRIBUTES_COLLECTION,
  49. SDO_PROG_ID_ATTRIBUTE,
  50. pAttachedMachine,
  51. NULL
  52. );
  53. if ( SUCCEEDED(hr) )
  54. {
  55. if ( ! fInitNew )
  56. hr = Load();
  57. }
  58. }
  59. }
  60. return hr;
  61. }
  62. //////////////////////////////////////////////////////////////////////////////
  63. HRESULT CSdoProfile::Load()
  64. {
  65. HRESULT hr = LoadAttributes();
  66. if ( SUCCEEDED(hr) )
  67. hr = LoadProperties();
  68. return hr;
  69. }
  70. //////////////////////////////////////////////////////////////////////////////
  71. HRESULT CSdoProfile::Save()
  72. {
  73. HRESULT hr = SaveAttributes();
  74. if ( SUCCEEDED(hr) )
  75. hr = SaveProperties();
  76. return hr;
  77. }
  78. //////////////////////////////////////////////////////////////////////////////
  79. // Profile class private member functions
  80. //////////////////////////////////////////////////////////////////////////////
  81. //////////////////////////////////////////////////////////////////////////////
  82. // Load the Profile SDO's attributes from the persistent store
  83. //////////////////////////////////////////////////////////////////////////////
  84. HRESULT CSdoProfile::LoadAttributes()
  85. {
  86. HRESULT hr;
  87. ATTRIBUTEID AttributeID;
  88. SDO_TRACE_VERBOSE_1("Profile SDO at $%p is loading profile attributes from the data store...",this);
  89. _variant_t vtAttributes;
  90. hr = GetPropertyInternal(PROPERTY_PROFILE_ATTRIBUTES_COLLECTION, &vtAttributes);
  91. if ( FAILED(hr) )
  92. {
  93. IASTracePrintf("Error in Profile SDO - LoadAttributes() - Could not get attributes collection...");
  94. return E_FAIL;
  95. }
  96. CComPtr<ISdoCollection> pAttributes;
  97. hr = vtAttributes.pdispVal->QueryInterface(IID_ISdoCollection, (void**)&pAttributes);
  98. if ( FAILED(hr) )
  99. {
  100. IASTracePrintf("Error in Profile SDO - LoadAttributes() - QueryInterface(ISdoCollection) failed...");
  101. return E_FAIL;
  102. }
  103. // Clear the current contents of the profile attributes collection
  104. //
  105. pAttributes->RemoveAll();
  106. // Create a new attribute SDO for each profile attribute
  107. //
  108. CComPtr<IUnknown> pUnknown;
  109. hr = m_pDSObject->get__NewEnum(&pUnknown);
  110. if ( FAILED(hr) )
  111. {
  112. IASTracePrintf("Error in Profile SDO - LoadAttributes() - get__NewEnum() failed...");
  113. return E_FAIL;
  114. }
  115. CComPtr<IEnumVARIANT> pEnumVariant;
  116. hr = pUnknown->QueryInterface(IID_IEnumVARIANT, (void**)&pEnumVariant);
  117. if ( FAILED(hr) )
  118. {
  119. IASTracePrintf("Error in Profile SDO - LoadAttributes() - QueryInterface(IEnumVARIANT) failed...");
  120. return E_FAIL;
  121. }
  122. _variant_t vtDSProperty;
  123. DWORD dwRetrieved = 1;
  124. hr = pEnumVariant->Next(1, &vtDSProperty, &dwRetrieved);
  125. if ( S_FALSE == hr )
  126. {
  127. IASTracePrintf("Error in Profile SDO - LoadAttributes() - No profile object properties...");
  128. return E_FAIL;
  129. }
  130. BSTR bstrName = NULL;
  131. CComPtr<IDataStoreProperty> pDSProperty;
  132. while ( S_OK == hr )
  133. {
  134. hr = vtDSProperty.pdispVal->QueryInterface(IID_IDataStoreProperty, (void**)&pDSProperty);
  135. if ( FAILED(hr) )
  136. {
  137. IASTracePrintf("Error in Profile SDO - LoadAttributes() - QueryInterface(IEnumDataStoreProperty) failed...");
  138. break;
  139. }
  140. hr = pDSProperty->get_Name(&bstrName);
  141. if ( FAILED(hr) )
  142. {
  143. IASTracePrintf("Error in Profile SDO - LoadAttributes() - get_Name() failed for %ls...", bstrName);
  144. break;
  145. }
  146. // If the attribute is not in the dictionary, then it's not an
  147. // attribute and it doesn't belong in the SDO collection.
  148. const AttributeDefinition* def = m_pSdoDictionary->findByLdapName(
  149. bstrName
  150. );
  151. if (def)
  152. {
  153. // Create an attribute SDO.
  154. CComPtr<SdoAttribute> attr;
  155. hr = SdoAttribute::createInstance(def, &attr);
  156. if (FAILED(hr)) { break; }
  157. // Set the value from the datastore.
  158. if (attr->def->restrictions & MULTIVALUED)
  159. {
  160. hr = pDSProperty->get_ValueEx(&attr->value);
  161. }
  162. else
  163. {
  164. hr = pDSProperty->get_Value(&attr->value);
  165. }
  166. if (FAILED(hr))
  167. {
  168. IASTraceFailure("IDataStore2::GetValue", hr);
  169. break;
  170. }
  171. // Add the attribute to the attributes collection.
  172. hr = pAttributes->Add(def->name, (IDispatch**)&attr);
  173. if (FAILED(hr)) { break; }
  174. SDO_TRACE_VERBOSE_2("Profile SDO at $%p loaded attribute '%S'",
  175. this, bstrName);
  176. }
  177. // Next profile object property
  178. //
  179. pDSProperty.Release();
  180. vtDSProperty.Clear();
  181. SysFreeString(bstrName);
  182. bstrName = NULL;
  183. dwRetrieved = 1;
  184. hr = pEnumVariant->Next(1, &vtDSProperty, &dwRetrieved);
  185. }
  186. if ( S_FALSE == hr )
  187. hr = S_OK;
  188. if ( bstrName )
  189. SysFreeString(bstrName);
  190. return hr;
  191. }
  192. //////////////////////////////////////////////////////////////////////////////
  193. // Save the Profile SDO's attributes to the persistent store
  194. //////////////////////////////////////////////////////////////////////////////
  195. HRESULT CSdoProfile::SaveAttributes()
  196. {
  197. HRESULT hr;
  198. SDO_TRACE_VERBOSE_1("Profile SDO at $%p is saving profile attributes...",this);
  199. hr = ClearAttributes();
  200. if ( FAILED(hr) )
  201. return E_FAIL;
  202. _variant_t vtAttributes;
  203. hr = GetPropertyInternal(PROPERTY_PROFILE_ATTRIBUTES_COLLECTION, &vtAttributes);
  204. if ( FAILED(hr) )
  205. {
  206. IASTracePrintf("Error in Profile SDO - LoadAttributes() - Could not get attributes collection...");
  207. return E_FAIL;
  208. }
  209. CComPtr<ISdoCollection> pAttributes;
  210. hr = vtAttributes.pdispVal->QueryInterface(IID_ISdoCollection, (void**)&pAttributes);
  211. if ( FAILED(hr) )
  212. {
  213. IASTracePrintf("Error in Profile SDO - LoadAttributes() - QueryInterface(ISdoCollection) failed...");
  214. return E_FAIL;
  215. }
  216. // Store the "Value" property of each Attribute SDO as a
  217. // profile attribute value.
  218. //
  219. CComPtr<IUnknown> pUnknown;
  220. hr = pAttributes->get__NewEnum(&pUnknown);
  221. if ( FAILED(hr) )
  222. {
  223. IASTracePrintf("Error in Profile SDO - SaveAttributes() - get__NewEnum() failed...");
  224. return E_FAIL;
  225. }
  226. CComPtr<IEnumVARIANT> pEnumVariant;
  227. hr = pUnknown->QueryInterface(IID_IEnumVARIANT, (void**)&pEnumVariant);
  228. if ( FAILED(hr) )
  229. {
  230. IASTracePrintf("Error in Profile SDO - SaveAttributes() - QueryInterface(IEnumVARIANT) failed...");
  231. return E_FAIL;
  232. }
  233. _variant_t vtAttribute;
  234. DWORD dwRetrieved = 1;
  235. hr = pEnumVariant->Next(1, &vtAttribute, &dwRetrieved);
  236. if ( S_FALSE == hr )
  237. {
  238. SDO_TRACE_VERBOSE_1("Profile SDO at $%p has an empty attributes collection...",this);
  239. return S_OK;
  240. }
  241. CComPtr<ISdo> pSdo;
  242. _variant_t vtName;
  243. _variant_t vtValue;
  244. _variant_t vtMultiValued;
  245. while ( S_OK == hr )
  246. {
  247. hr = vtAttribute.pdispVal->QueryInterface(IID_ISdo, (void**)&pSdo);
  248. if ( FAILED(hr) )
  249. {
  250. IASTracePrintf("Error in Profile SDO - SaveAttributes() - QueryInterface(ISdo) failed...");
  251. hr = E_FAIL;
  252. break;
  253. }
  254. hr = pSdo->GetProperty(PROPERTY_ATTRIBUTE_DISPLAY_NAME, &vtName);
  255. if ( FAILED(hr) )
  256. break;
  257. if ( 0 == lstrlen(V_BSTR(&vtName)) )
  258. {
  259. IASTracePrintf("Error in Profile SDO - SaveAttributes() - dictionary corrupt?...");
  260. hr = E_FAIL;
  261. break;
  262. }
  263. hr = pSdo->GetProperty(PROPERTY_ATTRIBUTE_VALUE, &vtValue);
  264. if ( FAILED(hr) )
  265. break;
  266. hr = pSdo->GetProperty(PROPERTY_ATTRIBUTE_ALLOW_MULTIPLE, &vtMultiValued);
  267. if ( FAILED(hr) )
  268. break;
  269. if ( V_BOOL(&vtMultiValued) == VARIANT_TRUE )
  270. {
  271. _variant_t vtType;
  272. hr = pSdo->GetProperty(PROPERTY_ATTRIBUTE_SYNTAX, &vtType);
  273. if ( FAILED(hr) )
  274. break;
  275. hr = m_pDSObject->PutValue(V_BSTR(&vtName), &vtValue);
  276. }
  277. else
  278. {
  279. hr = m_pDSObject->PutValue(V_BSTR(&vtName), &vtValue);
  280. }
  281. if ( FAILED(hr) )
  282. {
  283. IASTracePrintf("Error in Profile SDO - SaveAttributes() - PutValue() failed...");
  284. break;
  285. }
  286. SDO_TRACE_VERBOSE_2("Profile SDO at $%p saved attribute '%ls'...", this, vtName.bstrVal);
  287. pSdo.Release();
  288. vtAttribute.Clear();
  289. vtName.Clear();
  290. vtValue.Clear();
  291. vtMultiValued.Clear();
  292. dwRetrieved = 1;
  293. hr = pEnumVariant->Next(1, &vtAttribute, &dwRetrieved);
  294. }
  295. if ( S_FALSE == hr )
  296. hr = S_OK;
  297. return hr;
  298. }
  299. //////////////////////////////////////////////////////////////////////////////
  300. // Deletes the Profile SDO's attributes from the persistent store
  301. //////////////////////////////////////////////////////////////////////////////
  302. HRESULT CSdoProfile::ClearAttributes(void)
  303. {
  304. HRESULT hr;
  305. ATTRIBUTEID AttributeID;
  306. SDO_TRACE_VERBOSE_1("Profile SDO at $%p is clearing its profile attributes...",this);
  307. _ASSERT( NULL != m_pDSObject);
  308. // Set each profile attribute that has a representation in the IAS dictionary to VT_EMPTY
  309. //
  310. CComPtr<IUnknown> pUnknown;
  311. hr = m_pDSObject->get__NewEnum(&pUnknown);
  312. if ( FAILED(hr) )
  313. {
  314. IASTracePrintf("Error in Profile SDO - ClearAttributes() - get__NewEnum() failed...");
  315. return E_FAIL;
  316. }
  317. CComPtr<IEnumVARIANT> pEnumVariant;
  318. hr = pUnknown->QueryInterface(IID_IEnumVARIANT, (void**)&pEnumVariant);
  319. if ( FAILED(hr) )
  320. {
  321. IASTracePrintf("Error in Profile SDO - ClearAttributes() - QueryInterface(IEnumVARIANT) failed...");
  322. return E_FAIL;
  323. }
  324. DWORD dwRetrieved = 1;
  325. _variant_t vtDSProperty;
  326. hr = pEnumVariant->Next(1, &vtDSProperty, &dwRetrieved);
  327. if ( S_FALSE == hr )
  328. {
  329. SDO_TRACE_VERBOSE_1("Profile SDO at $%p has no profile attributes in the data store...",this);
  330. return S_OK;
  331. }
  332. BSTR bstrName = NULL;
  333. CComPtr<IDataStoreProperty> pDSProperty;
  334. _variant_t vtEmpty;
  335. while ( S_OK == hr )
  336. {
  337. hr = vtDSProperty.pdispVal->QueryInterface(IID_IDataStoreProperty, (void**)&pDSProperty);
  338. if ( FAILED(hr) )
  339. {
  340. IASTracePrintf("Error in Profile SDO - ClearAttributes() - QueryInterface(IDataStoreProperty) failed...");
  341. hr = E_FAIL;
  342. break;
  343. }
  344. hr = pDSProperty->get_Name(&bstrName);
  345. if ( FAILED(hr) )
  346. {
  347. IASTracePrintf("Error in Profile SDO - ClearAttributes() - IDataStoreProperty::Name() failed...");
  348. break;
  349. }
  350. hr = m_pSdoDictionary->GetAttributeID(bstrName, &AttributeID);
  351. if ( SUCCEEDED(hr) )
  352. {
  353. hr = m_pDSObject->PutValue(bstrName, &vtEmpty);
  354. if ( FAILED(hr) )
  355. {
  356. IASTracePrintf("Error in Profile SDO - ClearAttributes() - IDataStoreObject::PutValue() failed...");
  357. break;
  358. }
  359. SDO_TRACE_VERBOSE_2("Profile SDO at $%p cleared attribute '%ls'...", this, bstrName);
  360. }
  361. pDSProperty.Release();
  362. vtDSProperty.Clear();
  363. SysFreeString(bstrName);
  364. bstrName = NULL;
  365. dwRetrieved = 1;
  366. hr = pEnumVariant->Next(1, &vtDSProperty, &dwRetrieved);
  367. }
  368. if ( S_FALSE == hr )
  369. hr = S_OK;
  370. if ( bstrName )
  371. SysFreeString(bstrName);
  372. return hr;
  373. }