Source code of Windows XP (NT5)
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.

434 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation 1996-2001.
  5. //
  6. // File: cgroup.cpp
  7. //
  8. // Contents: implementation of CConfigGroup
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include "wsecmgr.h"
  13. #include "CGroup.h"
  14. #include "Addgrp.h"
  15. #include "GetUser.h"
  16. #include "wrapper.h"
  17. #include "util.h"
  18. #include "snapmgr.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CConfigGroup dialog
  26. CConfigGroup::CConfigGroup(UINT nTemplateID)
  27. : CAttribute (nTemplateID ? nTemplateID : IDD),
  28. m_fDirty(false),
  29. m_bNoMembers(FALSE),
  30. m_bNoMemberOf(FALSE)
  31. {
  32. //{{AFX_DATA_INIT(CConfigGroup)
  33. // NOTE: the ClassWizard will add member initialization here
  34. //}}AFX_DATA_INIT
  35. m_pHelpIDs = (DWORD_PTR)a107HelpIDs;
  36. m_uTemplateResID = IDD;
  37. }
  38. void CConfigGroup::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CAttribute::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CConfigGroup)
  42. DDX_Control(pDX, IDC_MEMBERS, m_lbMembers);
  43. DDX_Control(pDX, IDC_MEMBEROF, m_lbMemberOf);
  44. DDX_Control(pDX, IDC_NO_MEMBERS, m_eNoMembers);
  45. DDX_Control(pDX, IDC_NO_MEMBER_OF, m_eNoMemberOf);
  46. //}}AFX_DATA_MAP
  47. }
  48. BEGIN_MESSAGE_MAP(CConfigGroup, CAttribute)
  49. //{{AFX_MSG_MAP(CConfigGroup)
  50. ON_BN_CLICKED(IDC_ADD_MEMBER, OnAddMember)
  51. ON_BN_CLICKED(IDC_ADD_MEMBEROF, OnAddMemberof)
  52. ON_BN_CLICKED(IDC_REMOVE_MEMBER, OnRemoveMember)
  53. ON_BN_CLICKED(IDC_REMOVE_MEMBEROF, OnRemoveMemberof)
  54. //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CConfigGroup message handlers
  58. void CConfigGroup::OnAddMember()
  59. {
  60. CSCEAddGroup gu(this);
  61. gu.m_sTitle.LoadString( IDS_ADDMEMBER );
  62. gu.m_sDescription.LoadString(IDS_GROUP_MEMBERS_HEADER);
  63. gu.m_dwFlags = SCE_SHOW_USERS | SCE_SHOW_GLOBAL | SCE_SHOW_WELLKNOWN;
  64. gu.SetModeBits(m_pSnapin->GetModeBits());
  65. PSCE_NAME_LIST pName = 0;
  66. CThemeContextActivator activator;
  67. if (gu.DoModal() == IDOK )
  68. {
  69. pName = gu.GetUsers();
  70. while(pName)
  71. {
  72. if (LB_ERR == m_lbMembers.FindStringExact(-1,pName->Name))
  73. {
  74. if (LB_ERR == m_lbMemberOf.FindStringExact(-1,pName->Name))
  75. {
  76. if (m_bNoMembers)
  77. {
  78. m_bNoMembers = FALSE;
  79. m_lbMembers.ShowWindow(SW_SHOW);
  80. m_eNoMembers.ShowWindow(SW_HIDE);
  81. }
  82. m_lbMembers.AddString(pName->Name);
  83. m_fDirty = true;
  84. } else {
  85. // already in the Members Of list, shouldn't add to members
  86. }
  87. }
  88. pName = pName->Next;
  89. }
  90. SetModified(TRUE);
  91. }
  92. }
  93. void CConfigGroup::OnAddMemberof()
  94. {
  95. CSCEAddGroup gu;
  96. PSCE_NAME_LIST pName = 0;
  97. DWORD nFlag = 0;
  98. if ( IsDomainController() )
  99. {
  100. nFlag = SCE_SHOW_GROUPS | SCE_SHOW_ALIASES;
  101. } else {
  102. nFlag = SCE_SHOW_ALIASES;
  103. }
  104. gu.m_dwFlags = nFlag;
  105. gu.SetModeBits(m_pSnapin->GetModeBits());
  106. gu.m_sTitle.LoadString( IDS_GROUP_MEMBER_OF_HEADER );
  107. CThemeContextActivator activator;
  108. if (gu.DoModal() == IDOK )
  109. {
  110. pName = gu.GetUsers();
  111. while(pName)
  112. {
  113. if (LB_ERR == m_lbMemberOf.FindStringExact(-1,pName->Name))
  114. {
  115. if (LB_ERR == m_lbMembers.FindStringExact(-1,pName->Name))
  116. {
  117. if (m_bNoMemberOf)
  118. {
  119. m_bNoMemberOf = FALSE;
  120. m_lbMemberOf.ShowWindow(SW_SHOW);
  121. m_eNoMemberOf.ShowWindow(SW_HIDE);
  122. }
  123. m_lbMemberOf.AddString(pName->Name);
  124. m_fDirty = true;
  125. }
  126. else
  127. {
  128. // already find in the members list, shouldn't add it
  129. // otherwise, it will be in a circle
  130. }
  131. }
  132. pName = pName->Next;
  133. }
  134. SetModified(TRUE);
  135. }
  136. }
  137. void CConfigGroup::OnRemoveMember()
  138. {
  139. int cbItems = m_lbMembers.GetSelCount();
  140. if (cbItems > 0)
  141. {
  142. int* pnItems = new int [cbItems];
  143. if ( pnItems )
  144. {
  145. m_lbMembers.GetSelItems(cbItems,pnItems);
  146. m_fDirty = true;
  147. SetModified(TRUE);
  148. while(cbItems--)
  149. {
  150. m_lbMembers.DeleteString(pnItems[cbItems]);
  151. }
  152. delete[] pnItems;
  153. }
  154. }
  155. if (0 == m_lbMembers.GetCount())
  156. {
  157. m_bNoMembers = TRUE;
  158. m_lbMembers.ShowWindow(SW_HIDE);
  159. m_eNoMembers.ShowWindow(SW_SHOW);
  160. }
  161. }
  162. void CConfigGroup::OnRemoveMemberof()
  163. {
  164. int cbItems = m_lbMemberOf.GetSelCount();
  165. if (cbItems > 0)
  166. {
  167. int* pnItems = new int [cbItems];
  168. if ( pnItems )
  169. {
  170. m_lbMemberOf.GetSelItems(cbItems,pnItems);
  171. m_fDirty = true;
  172. SetModified(TRUE);
  173. while(cbItems--)
  174. {
  175. m_lbMemberOf.DeleteString(pnItems[cbItems]);
  176. }
  177. delete[] pnItems;
  178. }
  179. }
  180. if (0 == m_lbMemberOf.GetCount())
  181. {
  182. m_bNoMemberOf = TRUE;
  183. m_lbMemberOf.ShowWindow(SW_HIDE);
  184. m_eNoMemberOf.ShowWindow(SW_SHOW);
  185. }
  186. }
  187. /*//////////////////////////////////////////////////////////////////////////////
  188. Method: OnApply
  189. Synopsis: Save all changes to for the group
  190. Arguments: None
  191. Returns: None
  192. ////////////////////////////////////////////////////////////////////////////////*/
  193. BOOL CConfigGroup::OnApply()
  194. {
  195. if ( !m_bReadOnly )
  196. {
  197. PSCE_GROUP_MEMBERSHIP pgm = 0;
  198. PSCE_NAME_LIST pNames = 0;
  199. CString strItem;
  200. int cItems = 0;
  201. int i = 0;
  202. if (m_fDirty)
  203. {
  204. //
  205. // the group pointer is saved in the ID field
  206. //
  207. pgm = (PSCE_GROUP_MEMBERSHIP) (m_pData->GetID());
  208. //
  209. // should not free the old buffer first because
  210. // creation of the new buffer may fail
  211. //
  212. PSCE_NAME_LIST pTemp1=NULL, pTemp2=NULL;
  213. DWORD err=ERROR_SUCCESS;
  214. if (!m_bNoMembers)
  215. {
  216. cItems = m_lbMembers.GetCount();
  217. for(i=0;i<cItems;i++)
  218. {
  219. pNames = (PSCE_NAME_LIST) LocalAlloc(LPTR,sizeof(SCE_NAME_LIST));
  220. if ( pNames )
  221. {
  222. m_lbMembers.GetText(i,strItem);
  223. pNames->Name = (PWSTR) LocalAlloc(LPTR,(strItem.GetLength()+1)*sizeof(TCHAR));
  224. if ( pNames->Name )
  225. {
  226. lstrcpy(pNames->Name,(LPCTSTR)strItem);
  227. pNames->Next = pTemp1;
  228. pTemp1 = pNames;
  229. }
  230. else
  231. {
  232. //
  233. // no memory
  234. //
  235. LocalFree(pNames);
  236. err = ERROR_NOT_ENOUGH_MEMORY;
  237. break;
  238. }
  239. }
  240. else
  241. {
  242. err = ERROR_NOT_ENOUGH_MEMORY;
  243. break;
  244. }
  245. }
  246. }
  247. if ( err == ERROR_SUCCESS )
  248. {
  249. if (!m_bNoMemberOf)
  250. {
  251. cItems = m_lbMemberOf.GetCount();
  252. for (i=0;i<cItems;i++)
  253. {
  254. pNames = (PSCE_NAME_LIST) LocalAlloc(LPTR,sizeof(SCE_NAME_LIST));
  255. if ( pNames )
  256. {
  257. m_lbMemberOf.GetText(i,strItem);
  258. pNames->Name = (PWSTR) LocalAlloc(LPTR,(strItem.GetLength()+1)*sizeof(TCHAR));
  259. if ( pNames->Name )
  260. {
  261. lstrcpy(pNames->Name,(LPCTSTR)strItem);
  262. pNames->Next = pTemp2;
  263. pTemp2 = pNames;
  264. }
  265. else
  266. {
  267. //
  268. // no memory
  269. //
  270. LocalFree(pNames);
  271. err = ERROR_NOT_ENOUGH_MEMORY;
  272. break;
  273. }
  274. }
  275. else
  276. {
  277. err = ERROR_NOT_ENOUGH_MEMORY;
  278. break;
  279. }
  280. }
  281. }
  282. }
  283. if ( err == ERROR_SUCCESS )
  284. {
  285. SceFreeMemory(pgm->pMembers,SCE_STRUCT_NAME_LIST);
  286. pgm->pMembers = pTemp1;
  287. SceFreeMemory(pgm->pMemberOf,SCE_STRUCT_NAME_LIST);
  288. pgm->pMemberOf = pTemp2;
  289. m_pData->Update(m_pSnapin);
  290. m_fDirty = false;
  291. }
  292. else
  293. {
  294. //
  295. // error occured, can't save data
  296. // free the temp buffer and return to the dialog
  297. //
  298. SceFreeMemory(pTemp1,SCE_STRUCT_NAME_LIST);
  299. SceFreeMemory(pTemp2,SCE_STRUCT_NAME_LIST);
  300. return FALSE;
  301. }
  302. }
  303. }
  304. return CAttribute::OnApply();
  305. }
  306. BOOL CConfigGroup::OnInitDialog()
  307. {
  308. CAttribute::OnInitDialog();
  309. CString str;
  310. UpdateData(TRUE);
  311. str.LoadString(IDS_NO_MEMBERS);
  312. m_eNoMembers.SetWindowText(str);
  313. str.LoadString(IDS_NO_MEMBER_OF);
  314. m_eNoMemberOf.SetWindowText(str);
  315. PSCE_GROUP_MEMBERSHIP pgm = (PSCE_GROUP_MEMBERSHIP) (m_pData->GetID());
  316. if ( pgm )
  317. {
  318. SCESTATUS rc;
  319. m_bAlias = FALSE;
  320. PSCE_NAME_LIST pNames = pgm->pMembers;
  321. if (!pNames)
  322. {
  323. m_bNoMembers = TRUE;
  324. m_lbMembers.ShowWindow(SW_HIDE);
  325. m_eNoMembers.ShowWindow(SW_SHOW);
  326. }
  327. else while(pNames)
  328. {
  329. if (pNames->Name && lstrlen(pNames->Name))
  330. {
  331. m_lbMembers.AddString(pNames->Name);
  332. }
  333. pNames = pNames->Next;
  334. }
  335. pNames = pgm->pMemberOf;
  336. if (!pNames)
  337. {
  338. m_bNoMemberOf = TRUE;
  339. m_lbMemberOf.ShowWindow(SW_HIDE);
  340. m_eNoMemberOf.ShowWindow(SW_SHOW);
  341. }
  342. else while(pNames)
  343. {
  344. if (pNames->Name && lstrlen(pNames->Name))
  345. {
  346. m_lbMemberOf.AddString(pNames->Name);
  347. }
  348. pNames = pNames->Next;
  349. }
  350. // alias can't be members of any other groups, so disable memberof buttons
  351. CWnd *cwnd = GetDlgItem(IDC_ADD_MEMBEROF);
  352. if ( cwnd )
  353. cwnd->EnableWindow(!m_bAlias);
  354. cwnd = GetDlgItem(IDC_REMOVE_MEMBEROF);
  355. if ( cwnd )
  356. cwnd->EnableWindow(!m_bAlias);
  357. }
  358. // m_btnTitle.SetWindowText(m_pData->GetAttrPretty());
  359. if ( m_bReadOnly )
  360. {
  361. GetDlgItem (IDC_ADD_MEMBER)->EnableWindow (FALSE);
  362. GetDlgItem (IDC_REMOVE_MEMBER)->EnableWindow (FALSE);
  363. GetDlgItem (IDC_ADD_MEMBEROF)->EnableWindow (FALSE);
  364. GetDlgItem (IDC_REMOVE_MEMBEROF)->EnableWindow (FALSE);
  365. }
  366. return TRUE; // return TRUE unless you set the focus to a control
  367. // EXCEPTION: OCX Property Pages should return FALSE
  368. }