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.

626 lines
18 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. #define TAB_TITLE_LENGTH 40
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CConfigGroup dialog
  27. CConfigGroup::CConfigGroup(UINT nTemplateID)
  28. : CAttribute (nTemplateID ? nTemplateID : IDD),
  29. m_fDirty(false),
  30. m_bNoMembers(FALSE),
  31. m_bNoMemberOf(FALSE)
  32. {
  33. //{{AFX_DATA_INIT(CConfigGroup)
  34. // NOTE: the ClassWizard will add member initialization here
  35. //}}AFX_DATA_INIT
  36. m_pHelpIDs = (DWORD_PTR)a107HelpIDs;
  37. m_uTemplateResID = IDD;
  38. }
  39. void CConfigGroup::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CAttribute::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CConfigGroup)
  43. DDX_Control(pDX, IDC_MEMBERS, m_lbMembers);
  44. DDX_Control(pDX, IDC_MEMBEROF, m_lbMemberOf);
  45. DDX_Control(pDX, IDC_NO_MEMBERS, m_eNoMembers);
  46. DDX_Control(pDX, IDC_NO_MEMBER_OF, m_eNoMemberOf);
  47. //}}AFX_DATA_MAP
  48. }
  49. BEGIN_MESSAGE_MAP(CConfigGroup, CAttribute)
  50. //{{AFX_MSG_MAP(CConfigGroup)
  51. ON_BN_CLICKED(IDC_ADD_MEMBER, OnAddMember)
  52. ON_BN_CLICKED(IDC_ADD_MEMBEROF, OnAddMemberof)
  53. ON_BN_CLICKED(IDC_REMOVE_MEMBER, OnRemoveMember)
  54. ON_BN_CLICKED(IDC_REMOVE_MEMBEROF, OnRemoveMemberof)
  55. ON_LBN_SELCHANGE(IDC_MEMBERS, OnSelChangeMem)
  56. ON_LBN_SELCHANGE(IDC_MEMBEROF, OnSelChangeMemof)
  57. //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CConfigGroup message handlers
  61. void CConfigGroup::OnAddMember()
  62. {
  63. CSCEAddGroup gu(this);
  64. gu.m_sTitle.LoadString( IDS_ADDMEMBER );
  65. gu.m_sDescription.LoadString(IDS_GROUP_MEMBERS_HEADER);
  66. gu.m_dwFlags = SCE_SHOW_USERS | SCE_SHOW_GLOBAL | SCE_SHOW_WELLKNOWN;
  67. gu.SetModeBits(m_pSnapin->GetModeBits());
  68. PSCE_NAME_LIST pName = 0;
  69. if( IDD_CONFIG_MEMBERSHIP == m_uTemplateResID ) //Raid #475075, Yang Gao
  70. {
  71. gu.m_fCheckName = FALSE;
  72. }
  73. CThemeContextActivator activator;
  74. if (gu.DoModal() == IDOK )
  75. {
  76. pName = gu.GetUsers();
  77. PWSTR pLongestName = NULL; //Raid #496574, yanggao
  78. int maxlength = 0;
  79. int curlength = 0;
  80. while(pName)
  81. {
  82. if (LB_ERR == m_lbMembers.FindStringExact(-1,pName->Name))
  83. {
  84. if (LB_ERR == m_lbMemberOf.FindStringExact(-1,pName->Name))
  85. {
  86. if (m_bNoMembers)
  87. {
  88. m_bNoMembers = FALSE;
  89. m_lbMembers.ShowWindow(SW_SHOW);
  90. m_eNoMembers.ShowWindow(SW_HIDE);
  91. }
  92. m_lbMembers.AddString(pName->Name);
  93. m_fDirty = true;
  94. curlength = lstrlen(pName->Name);
  95. if( curlength > maxlength )
  96. {
  97. maxlength = curlength;
  98. pLongestName = pName->Name;
  99. }
  100. } else {
  101. // already in the Members Of list, shouldn't add to members
  102. }
  103. }
  104. pName = pName->Next;
  105. }
  106. if( pLongestName )
  107. {
  108. CClientDC hDC(&m_lbMembers);
  109. HFONT hFontOld = (HFONT)hDC.SelectObject((HFONT)GetStockObject(DEFAULT_GUI_FONT));
  110. CSize cSize;
  111. GetTextExtentPoint32(hDC, pLongestName, lstrlen(pLongestName), &cSize);
  112. hDC.SelectObject(hFontOld);
  113. if( cSize.cx > m_lbMembers.GetHorizontalExtent() )
  114. {
  115. m_lbMembers.SetHorizontalExtent(cSize.cx+10);
  116. }
  117. }
  118. if( m_fDirty ) //Raid #497350, yanggao, 11/20/2001
  119. {
  120. SetModified(TRUE);
  121. }
  122. m_lbMembers.EnableWindow(TRUE); //Raid 472244, Yang Gao, 10/22/2001
  123. }
  124. }
  125. void CConfigGroup::OnAddMemberof()
  126. {
  127. CSCEAddGroup gu;
  128. PSCE_NAME_LIST pName = 0;
  129. DWORD nFlag = 0;
  130. if ( IsDomainController() )
  131. {
  132. nFlag = SCE_SHOW_GROUPS | SCE_SHOW_ALIASES;
  133. } else {
  134. nFlag = SCE_SHOW_ALIASES | SCE_SHOW_BUILTIN | SCE_SHOW_LOCALGROUPS; //Raid #464603
  135. }
  136. gu.m_dwFlags = nFlag;
  137. gu.SetModeBits(m_pSnapin->GetModeBits());
  138. gu.m_sTitle.LoadString( IDS_GROUP_MEMBER_OF_HEADER );
  139. if( IDD_CONFIG_MEMBERSHIP == m_uTemplateResID ) //Raid #475075, Yang Gao
  140. {
  141. gu.m_fCheckName = FALSE;
  142. }
  143. CThemeContextActivator activator;
  144. if (gu.DoModal() == IDOK )
  145. {
  146. pName = gu.GetUsers();
  147. PWSTR pLongestName = NULL; //Raid #496574, yanggao
  148. int maxlength = 0;
  149. int curlength = 0;
  150. while(pName)
  151. {
  152. if (LB_ERR == m_lbMemberOf.FindStringExact(-1,pName->Name))
  153. {
  154. if (LB_ERR == m_lbMembers.FindStringExact(-1,pName->Name))
  155. {
  156. if (m_bNoMemberOf)
  157. {
  158. m_bNoMemberOf = FALSE;
  159. m_lbMemberOf.ShowWindow(SW_SHOW);
  160. m_eNoMemberOf.ShowWindow(SW_HIDE);
  161. }
  162. m_lbMemberOf.AddString(pName->Name);
  163. m_fDirty = true;
  164. curlength = lstrlen(pName->Name);
  165. if( curlength > maxlength )
  166. {
  167. maxlength = curlength;
  168. pLongestName = pName->Name;
  169. }
  170. }
  171. else
  172. {
  173. // already find in the members list, shouldn't add it
  174. // otherwise, it will be in a circle
  175. }
  176. }
  177. pName = pName->Next;
  178. }
  179. if( pLongestName )
  180. {
  181. CClientDC hDC(&m_lbMemberOf);
  182. HFONT hFontOld = (HFONT)hDC.SelectObject((HFONT)GetStockObject(DEFAULT_GUI_FONT));
  183. CSize cSize;
  184. GetTextExtentPoint32(hDC, pLongestName, lstrlen(pLongestName), &cSize);
  185. hDC.SelectObject(hFontOld);
  186. if( cSize.cx > m_lbMemberOf.GetHorizontalExtent() )
  187. {
  188. m_lbMemberOf.SetHorizontalExtent(cSize.cx+10);
  189. }
  190. }
  191. if( m_fDirty ) //Raid #497350, yanggao, 11/20/2001
  192. {
  193. SetModified(TRUE);
  194. }
  195. m_lbMemberOf.EnableWindow(TRUE); //Raid 472244, Yang Gao, 10/22/2001
  196. }
  197. }
  198. void CConfigGroup::OnRemoveMember()
  199. {
  200. int cbItems = m_lbMembers.GetSelCount();
  201. if (cbItems > 0)
  202. {
  203. int* pnItems = new int [cbItems];
  204. if ( pnItems )
  205. {
  206. m_lbMembers.GetSelItems(cbItems,pnItems);
  207. m_fDirty = true;
  208. SetModified(TRUE);
  209. while(cbItems--)
  210. {
  211. m_lbMembers.DeleteString(pnItems[cbItems]);
  212. }
  213. delete[] pnItems;
  214. }
  215. }
  216. if (0 == m_lbMembers.GetCount())
  217. {
  218. m_bNoMembers = TRUE;
  219. m_lbMembers.ShowWindow(SW_HIDE);
  220. m_eNoMembers.ShowWindow(SW_SHOW);
  221. m_eNoMembers.EnableWindow(FALSE);
  222. }
  223. CWnd* pCur = GetDlgItem(IDC_REMOVE_MEMBER); //Raid 472244, Yanggao, 10/23/2001
  224. if( pCur )
  225. {
  226. CWnd* pPrev = pCur->GetWindow(GW_HWNDPREV);
  227. if( pPrev )
  228. {
  229. this->GotoDlgCtrl(pPrev);
  230. pCur->EnableWindow(FALSE); //Raid 467886, Yang Gao, 9/18/2001
  231. }
  232. }
  233. }
  234. void CConfigGroup::OnRemoveMemberof()
  235. {
  236. int cbItems = m_lbMemberOf.GetSelCount();
  237. if (cbItems > 0)
  238. {
  239. int* pnItems = new int [cbItems];
  240. if ( pnItems )
  241. {
  242. m_lbMemberOf.GetSelItems(cbItems,pnItems);
  243. m_fDirty = true;
  244. SetModified(TRUE);
  245. while(cbItems--)
  246. {
  247. m_lbMemberOf.DeleteString(pnItems[cbItems]);
  248. }
  249. delete[] pnItems;
  250. }
  251. }
  252. if (0 == m_lbMemberOf.GetCount())
  253. {
  254. m_bNoMemberOf = TRUE;
  255. m_lbMemberOf.ShowWindow(SW_HIDE);
  256. m_eNoMemberOf.ShowWindow(SW_SHOW);
  257. m_eNoMemberOf.EnableWindow(FALSE);
  258. }
  259. CWnd* pCur = GetDlgItem(IDC_REMOVE_MEMBEROF); //Raid 472244, Yanggao, 10/23/2001
  260. if( pCur )
  261. {
  262. CWnd* pPrev = pCur->GetWindow(GW_HWNDPREV);
  263. if( pPrev )
  264. {
  265. this->GotoDlgCtrl(pPrev);
  266. pCur->EnableWindow(FALSE); //Raid 467886, Yang Gao, 9/18/2001
  267. }
  268. }
  269. }
  270. /*//////////////////////////////////////////////////////////////////////////////
  271. Method: OnApply
  272. Synopsis: Save all changes to for the group
  273. Arguments: None
  274. Returns: None
  275. ////////////////////////////////////////////////////////////////////////////////*/
  276. BOOL CConfigGroup::OnApply()
  277. {
  278. if ( !m_bReadOnly )
  279. {
  280. PSCE_GROUP_MEMBERSHIP pgm = 0;
  281. PSCE_NAME_LIST pNames = 0;
  282. CString strItem;
  283. int cItems = 0;
  284. int i = 0;
  285. if (m_fDirty)
  286. {
  287. //
  288. // the group pointer is saved in the ID field
  289. //
  290. pgm = (PSCE_GROUP_MEMBERSHIP) (m_pData->GetID());
  291. //
  292. // should not free the old buffer first because
  293. // creation of the new buffer may fail
  294. //
  295. PSCE_NAME_LIST pTemp1=NULL, pTemp2=NULL;
  296. DWORD err=ERROR_SUCCESS;
  297. if (!m_bNoMembers)
  298. {
  299. cItems = m_lbMembers.GetCount();
  300. for(i=0;i<cItems;i++)
  301. {
  302. pNames = (PSCE_NAME_LIST) LocalAlloc(LPTR,sizeof(SCE_NAME_LIST));
  303. if ( pNames )
  304. {
  305. m_lbMembers.GetText(i,strItem);
  306. pNames->Name = (PWSTR) LocalAlloc(LPTR,(strItem.GetLength()+1)*sizeof(TCHAR));
  307. if ( pNames->Name )
  308. {
  309. // This may not be a safe usage, using WCHAR instead of TCHAR. Consider fix
  310. lstrcpy(pNames->Name,(LPCTSTR)strItem);
  311. pNames->Next = pTemp1;
  312. pTemp1 = pNames;
  313. }
  314. else
  315. {
  316. //
  317. // no memory
  318. //
  319. LocalFree(pNames);
  320. err = ERROR_NOT_ENOUGH_MEMORY;
  321. break;
  322. }
  323. }
  324. else
  325. {
  326. err = ERROR_NOT_ENOUGH_MEMORY;
  327. break;
  328. }
  329. }
  330. }
  331. if ( err == ERROR_SUCCESS )
  332. {
  333. if (!m_bNoMemberOf)
  334. {
  335. cItems = m_lbMemberOf.GetCount();
  336. for (i=0;i<cItems;i++)
  337. {
  338. pNames = (PSCE_NAME_LIST) LocalAlloc(LPTR,sizeof(SCE_NAME_LIST));
  339. if ( pNames )
  340. {
  341. m_lbMemberOf.GetText(i,strItem);
  342. pNames->Name = (PWSTR) LocalAlloc(LPTR,(strItem.GetLength()+1)*sizeof(TCHAR));
  343. if ( pNames->Name )
  344. {
  345. // This may not be a safe usage, using WCHAR instead of TCHAR. Consider fix.
  346. lstrcpy(pNames->Name,(LPCTSTR)strItem);
  347. pNames->Next = pTemp2;
  348. pTemp2 = pNames;
  349. }
  350. else
  351. {
  352. //
  353. // no memory
  354. //
  355. LocalFree(pNames);
  356. err = ERROR_NOT_ENOUGH_MEMORY;
  357. break;
  358. }
  359. }
  360. else
  361. {
  362. err = ERROR_NOT_ENOUGH_MEMORY;
  363. break;
  364. }
  365. }
  366. }
  367. }
  368. if ( err == ERROR_SUCCESS )
  369. {
  370. SceFreeMemory(pgm->pMembers,SCE_STRUCT_NAME_LIST);
  371. pgm->pMembers = pTemp1;
  372. SceFreeMemory(pgm->pMemberOf,SCE_STRUCT_NAME_LIST);
  373. pgm->pMemberOf = pTemp2;
  374. m_pData->Update(m_pSnapin);
  375. m_fDirty = false;
  376. }
  377. else
  378. {
  379. //
  380. // error occured, can't save data
  381. // free the temp buffer and return to the dialog
  382. //
  383. SceFreeMemory(pTemp1,SCE_STRUCT_NAME_LIST);
  384. SceFreeMemory(pTemp2,SCE_STRUCT_NAME_LIST);
  385. return FALSE;
  386. }
  387. }
  388. }
  389. return CAttribute::OnApply();
  390. }
  391. BOOL CConfigGroup::OnInitDialog()
  392. {
  393. CAttribute::OnInitDialog();
  394. CString str;
  395. UpdateData(TRUE);
  396. str.LoadString(IDS_NO_MEMBERS);
  397. m_eNoMembers.SetWindowText(str);
  398. str.LoadString(IDS_NO_MEMBER_OF);
  399. m_eNoMemberOf.SetWindowText(str);
  400. PSCE_GROUP_MEMBERSHIP pgm = (PSCE_GROUP_MEMBERSHIP) (m_pData->GetID());
  401. if ( pgm )
  402. {
  403. SCESTATUS rc;
  404. if( pgm->GroupName )
  405. {
  406. CString strCap; //Raid 467420, Yang Gao
  407. strCap.Format(IDS_GROUP_TITLE, pgm->GroupName);
  408. if( strCap.GetLength() > TAB_TITLE_LENGTH ) //Raid #495610, yanggao
  409. {
  410. strCap.Format(IDS_GROUP_TITLE_WRAP, pgm->GroupName);
  411. }
  412. CPropertySheet* pSheet = (CPropertySheet*)GetParent(); //Raid 469026
  413. if( pSheet )
  414. {
  415. CTabCtrl* pTab = pSheet->GetTabControl();
  416. if( pTab )
  417. {
  418. TC_ITEM ti;
  419. ti.mask = TCIF_TEXT;
  420. ti.pszText = strCap.GetBuffer(strCap.GetLength());
  421. int nPage = pSheet->GetActiveIndex();
  422. pTab->SetItem(nPage, &ti);
  423. }
  424. }
  425. }
  426. PSCE_NAME_LIST pNames = pgm->pMembers;
  427. if (!pNames)
  428. {
  429. m_bNoMembers = TRUE;
  430. m_lbMembers.ShowWindow(SW_HIDE);
  431. m_eNoMembers.ShowWindow(SW_SHOW);
  432. m_eNoMembers.EnableWindow(FALSE);
  433. }
  434. else
  435. {
  436. PWSTR pLongestName = NULL; //Raid #496574, yanggao
  437. int maxlength = 0;
  438. int curlength = 0;
  439. while(pNames)
  440. {
  441. if( pNames->Name )
  442. {
  443. curlength = lstrlen(pNames->Name);
  444. if( curlength )
  445. {
  446. m_lbMembers.AddString(pNames->Name);
  447. if( curlength > maxlength )
  448. {
  449. maxlength = curlength;
  450. pLongestName = pNames->Name;
  451. }
  452. }
  453. }
  454. pNames = pNames->Next;
  455. }
  456. if( pLongestName )
  457. {
  458. CClientDC hDC(&m_lbMembers);
  459. HFONT hFontOld = (HFONT)hDC.SelectObject((HFONT)GetStockObject(DEFAULT_GUI_FONT));
  460. CSize cSize;
  461. GetTextExtentPoint32(hDC, pLongestName, lstrlen(pLongestName), &cSize);
  462. hDC.SelectObject(hFontOld);
  463. m_lbMembers.SetHorizontalExtent(cSize.cx+10);
  464. }
  465. }
  466. if( m_lbMembers.GetCount() <= 0 ) //Raid 472244, Yang Gao, 9/18/2001
  467. {
  468. m_lbMembers.EnableWindow(false);
  469. }
  470. GetDlgItem (IDC_REMOVE_MEMBER)->EnableWindow(FALSE);
  471. pNames = pgm->pMemberOf;
  472. if (!pNames)
  473. {
  474. m_bNoMemberOf = TRUE;
  475. m_lbMemberOf.ShowWindow(SW_HIDE);
  476. m_eNoMemberOf.ShowWindow(SW_SHOW);
  477. m_eNoMemberOf.EnableWindow(FALSE);
  478. }
  479. else
  480. {
  481. PWSTR pLongestName = NULL; //Raid #496574, yanggao
  482. int maxlength = 0;
  483. int curlength = 0;
  484. while(pNames)
  485. {
  486. if( pNames->Name )
  487. {
  488. curlength = lstrlen(pNames->Name);
  489. if( curlength )
  490. {
  491. m_lbMemberOf.AddString(pNames->Name);
  492. if( curlength > maxlength )
  493. {
  494. maxlength = curlength;
  495. pLongestName = pNames->Name;
  496. }
  497. }
  498. }
  499. pNames = pNames->Next;
  500. }
  501. if( pLongestName )
  502. {
  503. CClientDC hDC(&m_lbMemberOf);
  504. HFONT hFontOld = (HFONT)hDC.SelectObject((HFONT)GetStockObject(DEFAULT_GUI_FONT));
  505. CSize cSize;
  506. GetTextExtentPoint32(hDC, pLongestName, lstrlen(pLongestName), &cSize);
  507. hDC.SelectObject(hFontOld);
  508. m_lbMemberOf.SetHorizontalExtent(cSize.cx+10);
  509. }
  510. }
  511. if( m_lbMemberOf.GetCount() <= 0 ) //Raid 472244, Yang Gao, 10/22/2001
  512. {
  513. m_lbMemberOf.EnableWindow(false);
  514. }
  515. GetDlgItem (IDC_REMOVE_MEMBEROF)->EnableWindow(FALSE);
  516. }
  517. // m_btnTitle.SetWindowText(m_pData->GetAttrPretty());
  518. if ( m_bReadOnly )
  519. {
  520. GetDlgItem (IDC_ADD_MEMBER)->EnableWindow (FALSE);
  521. GetDlgItem (IDC_REMOVE_MEMBER)->EnableWindow (FALSE);
  522. GetDlgItem (IDC_ADD_MEMBEROF)->EnableWindow (FALSE);
  523. GetDlgItem (IDC_REMOVE_MEMBEROF)->EnableWindow (FALSE);
  524. }
  525. return TRUE; // return TRUE unless you set the focus to a control
  526. // EXCEPTION: OCX Property Pages should return FALSE
  527. }
  528. void CConfigGroup::OnSelChangeMem() //Raid 467886, Yang Gao, 9/18/2001
  529. {
  530. CWnd* pwnd = this->GetDlgItem(IDC_REMOVE_MEMBER);
  531. if( pwnd )
  532. {
  533. if( m_lbMembers.GetSelCount() == 0 ) //Raid #490559, Yanggao. For multi-select listbox
  534. {
  535. pwnd->EnableWindow(FALSE);
  536. }
  537. else
  538. {
  539. pwnd->EnableWindow(TRUE);
  540. }
  541. }
  542. }
  543. void CConfigGroup::OnSelChangeMemof()
  544. {
  545. CWnd* pwnd = this->GetDlgItem(IDC_REMOVE_MEMBEROF);
  546. if( pwnd )
  547. {
  548. if( m_lbMemberOf.GetSelCount() == 0 ) //Raid #450559, Yanggao, For multi-select listbox
  549. {
  550. pwnd->EnableWindow(FALSE);
  551. }
  552. else
  553. {
  554. pwnd->EnableWindow(TRUE);
  555. }
  556. }
  557. }