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.

372 lines
10 KiB

  1. // This node class ...
  2. #include "stdafx.h"
  3. #include "MyNodes.h"
  4. #include "DomSel.h"
  5. #include "..\Common\UString.hpp"
  6. #include "..\Common\Common.hpp"
  7. #import "\bin\NetEnum.tlb" no_namespace, named_guids
  8. // {162A41A3-405C-11d3-8AED-00A0C9AFE114}
  9. static const GUID CPruneGraftGUID_NODETYPE =
  10. { 0x162a41a3, 0x405c, 0x11d3, { 0x8a, 0xed, 0x0, 0xa0, 0xc9, 0xaf, 0xe1, 0x14 } };
  11. const GUID* CPruneGraftNode::m_NODETYPE = &CPruneGraftGUID_NODETYPE;
  12. const OLECHAR* CPruneGraftNode::m_SZNODETYPE = OLESTR("C8C24622-3FA1-11d3-8AED-00A0C9AFE114");
  13. const OLECHAR* CPruneGraftNode::m_SZDISPLAY_NAME = OLESTR("Domain Migrator");
  14. const CLSID* CPruneGraftNode::m_SNAPIN_CLASSID = &CLSID_DomMigrator;
  15. // 0 1 2 3 4
  16. WCHAR * gLDAPColumns[] = { L"", L"", L"", L"", L"" };
  17. WCHAR * gColumnHeaders[] = { L"", L"",L"",L"" };
  18. // these define the index in gLDAPColumns to use for each column
  19. int gDomainMapping[] = { 0,1,2,4 };
  20. int gOuMapping[] = { 3,1,2,4 };
  21. int gContainerMapping[] = { 0,1,2,4 };
  22. int gGroupMapping[] = { 0, 1,2,4 };
  23. int gUserMapping[] = { 0, 1,2,4 };
  24. CPruneGraftNode::CPruneGraftNode()
  25. {
  26. // Initialize the array of children
  27. // TODO: load the domain hierarchy for the current forest
  28. m_bLoaded = FALSE;
  29. m_bstrDisplayName = SysAllocString(L"Prune & Graft");
  30. m_scopeDataItem.nImage = IMAGE_INDEX_AD; // May need modification
  31. m_scopeDataItem.nOpenImage = IMAGE_INDEX_AD_OPEN; // May need modification
  32. m_resultDataItem.nImage = IMAGE_INDEX_AD; // May need modification
  33. m_Data.SetSize(MAX_COLUMNS);
  34. }
  35. void
  36. CPruneGraftNode::Init(
  37. WCHAR const * domain,
  38. WCHAR const * path,
  39. WCHAR const * objClass,
  40. WCHAR const * displayName
  41. )
  42. {
  43. m_Domain = domain;
  44. m_LDAPPath = path;
  45. m_objectClass = objClass;
  46. m_bstrDisplayName = displayName;
  47. // set the icons
  48. if ( ! UStrICmp(objClass,L"user") )
  49. {
  50. m_scopeDataItem.nImage = IMAGE_INDEX_USER;
  51. m_scopeDataItem.nOpenImage = IMAGE_INDEX_USER_OPEN;
  52. m_resultDataItem.nImage = IMAGE_INDEX_USER;
  53. }
  54. else if ( ! UStrICmp(objClass,L"group") )
  55. {
  56. m_scopeDataItem.nImage = IMAGE_INDEX_GROUP;
  57. m_scopeDataItem.nOpenImage = IMAGE_INDEX_GROUP_OPEN;
  58. m_resultDataItem.nImage = IMAGE_INDEX_GROUP;
  59. }
  60. else if ( ! UStrICmp(objClass,L"organizationalUnit") )
  61. {
  62. m_scopeDataItem.nImage = IMAGE_INDEX_OU;
  63. m_scopeDataItem.nOpenImage = IMAGE_INDEX_OU_OPEN;
  64. m_resultDataItem.nImage = IMAGE_INDEX_OU;
  65. }
  66. else if ( ! UStrICmp(objClass,L"domain") )
  67. {
  68. m_scopeDataItem.nImage = IMAGE_INDEX_DOMAIN;
  69. m_scopeDataItem.nOpenImage = IMAGE_INDEX_DOMAIN_OPEN;
  70. m_resultDataItem.nImage = IMAGE_INDEX_DOMAIN;
  71. }
  72. else if ( ! UStrICmp(objClass,L"container") )
  73. {
  74. m_scopeDataItem.nImage = IMAGE_INDEX_VIEW;
  75. m_scopeDataItem.nOpenImage = IMAGE_INDEX_VIEW_OPEN;
  76. m_resultDataItem.nImage = IMAGE_INDEX_VIEW;
  77. }
  78. }
  79. BOOL
  80. CPruneGraftNode::ShowInScopePane()
  81. {
  82. return ( UStrICmp(m_objectClass,L"user") );
  83. }
  84. HRESULT CPruneGraftNode::OnAddDomain(bool &bHandled, CSnapInObjectRootBase * pObj)
  85. {
  86. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  87. HRESULT hr = S_OK;
  88. CDomainSelDlg dlg;
  89. CComPtr<IConsole> pConsole;
  90. hr = GetConsoleFromCSnapInObjectRootBase(pObj, &pConsole );
  91. if (FAILED(hr))
  92. return hr;
  93. if ( IDOK == dlg.DoModal() )
  94. {
  95. // insert the domain in the scope pane
  96. CPruneGraftNode * pNode = new CPruneGraftNode();
  97. pNode->Init(dlg.m_Domain.AllocSysString(),L"",L"domain",dlg.m_Domain.AllocSysString());
  98. hr = InsertNodeToScopepane2((IConsole*)pConsole, pNode, m_scopeDataItem.ID );
  99. m_ChildArray.Add(pNode);
  100. }
  101. return hr;
  102. }
  103. HRESULT CPruneGraftNode::OnExpand( IConsole *spConsole )
  104. {
  105. // TODO: if we haven't already, enumerate our contents
  106. if ( ! m_bLoaded )
  107. {
  108. EnumerateChildren(spConsole);
  109. m_bLoaded = TRUE;
  110. }
  111. return CNetNode<CPruneGraftNode>::OnExpand(spConsole);
  112. }
  113. SAFEARRAY * CPruneGraftNode::GetAvailableColumns(WCHAR const * objectClass)
  114. {
  115. long nItems = 0;
  116. WCHAR ** columns = NULL;
  117. columns = gLDAPColumns;
  118. nItems = DIM(gLDAPColumns);
  119. // Build a safearray containing the data
  120. SAFEARRAYBOUND bound[1] = { { 0, 0 } };
  121. long ndx[1];
  122. bound[0].cElements = nItems;
  123. SAFEARRAY * pArray = SafeArrayCreate(VT_BSTR,1,bound);
  124. for ( long i = 0 ; i < nItems ; i++ )
  125. {
  126. ndx[0] = i;
  127. SafeArrayPutElement(pArray,ndx,SysAllocString(columns[i]));
  128. }
  129. return pArray;
  130. }
  131. HRESULT CPruneGraftNode::EnumerateChildren(IConsole * spConsole)
  132. {
  133. HRESULT hr = S_OK;
  134. WCHAR path[MAX_PATH];
  135. INetObjEnumeratorPtr pEnum;
  136. IEnumVARIANT * pValues = NULL;
  137. hr = pEnum.CreateInstance(CLSID_NetObjEnumerator);
  138. if ( SUCCEEDED(hr) )
  139. {
  140. if ( m_LDAPPath.length() )
  141. {
  142. swprintf(path,L"LDAP://%ls/%ls",(WCHAR*)m_Domain,(WCHAR*)m_LDAPPath);
  143. }
  144. else
  145. {
  146. safecopy(path,(WCHAR*)m_LDAPPath);
  147. }
  148. hr = pEnum->raw_SetQuery(path,m_Domain,L"(objectClass=*)",1,FALSE);
  149. }
  150. if ( SUCCEEDED(hr) )
  151. {
  152. hr = pEnum->raw_SetColumns(GetAvailableColumns(m_objectClass));
  153. }
  154. if ( SUCCEEDED(hr) )
  155. {
  156. hr = pEnum->raw_Execute(&pValues);
  157. }
  158. if ( SUCCEEDED(hr) )
  159. {
  160. hr = LoadChildren(pValues);
  161. pValues->Release();
  162. }
  163. return hr;
  164. }
  165. HRESULT CPruneGraftNode::LoadChildren(IEnumVARIANT * pEnumerator)
  166. {
  167. HRESULT hr = 0;
  168. VARIANT var;
  169. long count = 0;
  170. ULONG nReturned = 0;
  171. CPruneGraftNode * pNode = NULL;
  172. VariantInit(&var);
  173. while ( hr != S_FALSE )
  174. {
  175. hr = pEnumerator->Next(1,&var,&nReturned);
  176. // break if there was an error, or Next returned S_FALSE
  177. if ( hr != S_OK )
  178. break;
  179. // see if this is an array ( it should be!)
  180. if ( var.vt == ( VT_ARRAY | VT_VARIANT ) )
  181. {
  182. VARIANT * pData;
  183. SAFEARRAY * pArray;
  184. pArray = var.parray;
  185. pNode = new CPruneGraftNode;
  186. SafeArrayGetUBound(pArray,1,&count);
  187. SafeArrayAccessData(pArray,(void**)&pData);
  188. // make sure we at least have an LDAP path and an objectClass
  189. if ( count )
  190. {
  191. // get the object class and distinguishedName
  192. pNode->Init(m_Domain,pData[1].bstrVal,pData[2].bstrVal,pData[0].bstrVal);
  193. m_ChildArray.Add(pNode);
  194. for ( long i = 0 ; i <= count ; i++ )
  195. {
  196. // convert each value to a string, and store it in the node
  197. if ( SUCCEEDED(VariantChangeType(&pData[i],&pData[i],0,VT_BSTR)) )
  198. {
  199. pNode->AddColumnValue(i,pData[i].bstrVal);
  200. }
  201. }
  202. }
  203. else
  204. {
  205. delete pNode;
  206. }
  207. }
  208. }
  209. return hr;
  210. }
  211. HRESULT CPruneGraftNode::OnShow( bool bShow, IHeaderCtrl *spHeader, IResultData *spResultData)
  212. {
  213. HRESULT hr=S_OK;
  214. if (bShow)
  215. { // show
  216. for ( int i = 0 ; i < DIM(gColumnHeaders) ; i++ )
  217. {
  218. spHeader->InsertColumn(i, gColumnHeaders[i], LVCFMT_LEFT, m_iColumnWidth[i]);
  219. }
  220. {
  221. CString cstr;
  222. CComBSTR text;
  223. cstr.Format(_T("%d subitem(s)"), m_ChildArray.GetSize() );
  224. text = (LPCTSTR)cstr;
  225. spResultData->SetDescBarText( BSTR(text) );
  226. }
  227. }
  228. else
  229. { // hide
  230. // save the column widths
  231. for ( int i = 0 ; i < DIM(gColumnHeaders) ; i++ )
  232. {
  233. spHeader->GetColumnWidth(i, m_iColumnWidth + i);
  234. }
  235. }
  236. hr = S_OK;
  237. return hr;
  238. }
  239. LPOLESTR CPruneGraftNode::GetResultPaneColInfo(int nCol)
  240. {
  241. CString value;
  242. int ndx = nCol;
  243. int * mapping = NULL;
  244. if ( m_objectClass.length() && UStrICmp(m_objectClass,L"domain") )
  245. {
  246. if ( ! UStrICmp(m_objectClass,L"user") )
  247. {
  248. mapping = gUserMapping;
  249. }
  250. else if ( ! UStrICmp(m_objectClass,L"group") )
  251. {
  252. mapping = gGroupMapping;
  253. }
  254. else if ( ! UStrICmp(m_objectClass,L"organizationalUnit") )
  255. {
  256. mapping = gOuMapping;
  257. }
  258. else if ( ! UStrICmp(m_objectClass,L"domain") )
  259. {
  260. mapping = gDomainMapping;
  261. }
  262. else if ( ! UStrICmp(m_objectClass,L"container") )
  263. {
  264. mapping = gContainerMapping;
  265. }
  266. else
  267. {
  268. mapping = gContainerMapping;
  269. }
  270. if ( mapping )
  271. ndx = mapping[nCol];
  272. if ( ndx <= m_Data.GetUpperBound() )
  273. {
  274. value = m_Data.GetAt(ndx);
  275. return value.AllocSysString();
  276. }
  277. else
  278. return OLESTR("Override GetResultPaneColInfo");
  279. }
  280. else
  281. {
  282. return CNetNode<CPruneGraftNode>::GetResultPaneColInfo(nCol);
  283. }
  284. return NULL;
  285. }
  286. void CPruneGraftNode::AddColumnValue(int col,WCHAR const * value)
  287. {
  288. m_Data.SetAtGrow(col,value);
  289. // see if we need to update the display name
  290. // get the pointer for the columns
  291. int * mapping = NULL;
  292. if ( ! UStrICmp(m_objectClass,L"user") )
  293. {
  294. mapping = gUserMapping;
  295. }
  296. else if ( ! UStrICmp(m_objectClass,L"group") )
  297. {
  298. mapping = gGroupMapping;
  299. }
  300. else if ( ! UStrICmp(m_objectClass,L"organizationalUnit") )
  301. {
  302. mapping = gOuMapping;
  303. }
  304. else if ( ! UStrICmp(m_objectClass,L"domain") )
  305. {
  306. mapping = gDomainMapping;
  307. }
  308. else if ( ! UStrICmp(m_objectClass,L"container") )
  309. {
  310. mapping = gContainerMapping;
  311. }
  312. else
  313. {
  314. mapping = gContainerMapping;
  315. }
  316. if ( mapping && col == mapping[0] ) // display name
  317. {
  318. m_bstrDisplayName = value;
  319. }
  320. }