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.

349 lines
10 KiB

  1. #include "stdafx.h"
  2. #include "grpinfo.h"
  3. #pragma hdrstop
  4. // Names of groups retrieved by SID
  5. WCHAR g_szPowerUsers[MAX_GROUP + 1];
  6. WCHAR g_szUsers[MAX_GROUP + 1];
  7. /**************************************************************
  8. CGroupPageBase Implementation
  9. Functions common to both the group prop page and the group
  10. wizard page.
  11. **************************************************************/
  12. CGroupPageBase::CGroupPageBase(CUserInfo* pUserInfo, CDPA<CGroupInfo>* pGroupList)
  13. {
  14. m_pUserInfo = pUserInfo;
  15. m_pGroupList = pGroupList;
  16. m_hBoldFont = NULL;
  17. // Load names for local groups based on SID
  18. if (FAILED(LookupLocalGroupName(DOMAIN_ALIAS_RID_POWER_USERS, g_szPowerUsers, ARRAYSIZE(g_szPowerUsers))))
  19. {
  20. *g_szPowerUsers = L'\0';
  21. }
  22. if (FAILED(LookupLocalGroupName(DOMAIN_ALIAS_RID_USERS, g_szUsers, ARRAYSIZE(g_szUsers))))
  23. {
  24. *g_szUsers = L'\0';
  25. }
  26. }
  27. INT_PTR CGroupPageBase::HandleGroupMessage(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  28. {
  29. switch (uMsg)
  30. {
  31. HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  32. HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  33. };
  34. return FALSE;
  35. }
  36. void CGroupPageBase::InitializeLocalGroupCombo(HWND hwndCombo)
  37. {
  38. ComboBox_ResetContent(hwndCombo);
  39. // Add all of the groups in the list to the box
  40. for(int i = 0; i < m_pGroupList->GetPtrCount(); i ++)
  41. {
  42. CGroupInfo* pGroupInfo = m_pGroupList->GetPtr(i);
  43. int index = ComboBox_AddString(hwndCombo, pGroupInfo->m_szGroup);
  44. ComboBox_SetItemData(hwndCombo, index, pGroupInfo->m_szComment);
  45. }
  46. TCHAR szSelectGroup[MAX_GROUP + 1];
  47. // Load a local group name from the resources to select by default
  48. // dsheldon: this will fail for MUI...not critical though
  49. LoadString(g_hinst, IDS_USR_DEFAULTGROUP, szSelectGroup, ARRAYSIZE(szSelectGroup));
  50. if (ComboBox_SelectString(hwndCombo, 0, szSelectGroup) == CB_ERR)
  51. ComboBox_SetCurSel(hwndCombo, 0);
  52. }
  53. void CGroupPageBase::SetGroupDescription(HWND hwndCombo, HWND hwndEdit)
  54. {
  55. int iItem = ComboBox_GetCurSel(hwndCombo);
  56. TCHAR* pszDescription = (TCHAR*) ComboBox_GetItemData(hwndCombo, iItem);
  57. SetWindowText(hwndEdit, pszDescription);
  58. }
  59. BOOL CGroupPageBase::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  60. {
  61. // Fill in the local group combo box
  62. HWND hwndCombo = GetDlgItem(hwnd, IDC_GROUPS);
  63. InitializeLocalGroupCombo(hwndCombo);
  64. HWND hwndEdit = GetDlgItem(hwnd, IDC_GROUPDESC);
  65. if ((NULL != m_pUserInfo) && (m_pUserInfo->m_szGroups[0] != TEXT('\0')))
  66. {
  67. // Select the local group corresponding to the first one in the user's groups
  68. // string
  69. TCHAR szSelect[MAX_GROUP + 1];
  70. // Copy the string since we might shorten our copy
  71. StrCpyN(szSelect, m_pUserInfo->m_szGroups, ARRAYSIZE(szSelect));
  72. TCHAR* pchEndOfFirst = StrChr(szSelect, TEXT(';'));
  73. if (pchEndOfFirst)
  74. {
  75. // More than one group; we'll fix that!
  76. *pchEndOfFirst = TEXT('\0');
  77. }
  78. SelectGroup(hwnd, szSelect);
  79. }
  80. else
  81. {
  82. // Select the power user group by default
  83. SendDlgItemMessage(hwnd, IDC_POWERUSERS, BM_SETCHECK,
  84. (WPARAM) BST_CHECKED, 0);
  85. OnRadioChanged(hwnd, IDC_POWERUSERS);
  86. }
  87. SetGroupDescription(hwndCombo, hwndEdit);
  88. // Bold the group names
  89. BoldGroupNames(hwnd);
  90. return TRUE;
  91. }
  92. BOOL CGroupPageBase::GetSelectedGroup(HWND hwnd, LPTSTR pszGroupOut, DWORD cchGroup, CUserInfo::GROUPPSEUDONYM* pgsOut)
  93. {
  94. *pgsOut = CUserInfo::USEGROUPNAME;
  95. UINT idString = 0;
  96. if (BST_CHECKED == Button_GetCheck(GetDlgItem(hwnd, IDC_POWERUSERS)))
  97. {
  98. StrCpyN(pszGroupOut, g_szPowerUsers, cchGroup);
  99. *pgsOut = CUserInfo::STANDARD;
  100. }
  101. else if (BST_CHECKED == Button_GetCheck(GetDlgItem(hwnd, IDC_USERS)))
  102. {
  103. StrCpyN(pszGroupOut, g_szUsers, cchGroup);
  104. *pgsOut = CUserInfo::RESTRICTED;
  105. }
  106. else
  107. {
  108. // 'other' must be selected; get the string from the dropdown
  109. GetWindowText(GetDlgItem(hwnd, IDC_GROUPS), pszGroupOut, cchGroup);
  110. }
  111. return TRUE;
  112. }
  113. // Returns IDC_OTHER if no radio button id corresponds to the group
  114. UINT CGroupPageBase::RadioIdForGroup(LPCTSTR pszGroup)
  115. {
  116. UINT uiRadio = IDC_OTHER; // Assume IDC_OTHER to start
  117. if (0 == StrCmpI(pszGroup, g_szPowerUsers))
  118. {
  119. uiRadio = IDC_POWERUSERS;
  120. }
  121. else if (0 == StrCmpI(pszGroup, g_szUsers))
  122. {
  123. uiRadio = IDC_USERS;
  124. }
  125. return uiRadio;
  126. }
  127. // Disable/update as appropriate when radio selection changes
  128. void CGroupPageBase::OnRadioChanged(HWND hwnd, UINT idRadio)
  129. {
  130. BOOL fEnableGroupDropdown = (IDC_OTHER == idRadio);
  131. EnableWindow(GetDlgItem(hwnd, IDC_GROUPS), fEnableGroupDropdown);
  132. EnableWindow(GetDlgItem(hwnd, IDC_OTHER_STATIC), fEnableGroupDropdown);
  133. ShowWindow(GetDlgItem(hwnd, IDC_GROUPDESC),
  134. fEnableGroupDropdown ? SW_SHOW : SW_HIDE);
  135. }
  136. void CGroupPageBase::SelectGroup(HWND hwnd, LPCTSTR pszSelect)
  137. {
  138. // Always select the group in the 'other' dropdown
  139. ComboBox_SelectString(GetDlgItem(hwnd, IDC_GROUPS),
  140. -1, pszSelect);
  141. // Check the appropriate radio button
  142. UINT idRadio = RadioIdForGroup(pszSelect);
  143. Button_SetCheck(GetDlgItem(hwnd, idRadio), BST_CHECKED);
  144. OnRadioChanged(hwnd, idRadio);
  145. }
  146. void CGroupPageBase::BoldGroupNames(HWND hwnd)
  147. {
  148. HWND hwndPowerUsers = GetDlgItem(hwnd, IDC_POWERUSERS);
  149. HFONT hfont = (HFONT) SendMessage(hwndPowerUsers, WM_GETFONT, 0, 0);
  150. if (hfont)
  151. {
  152. LOGFONT lf;
  153. if (FALSE != GetObject((HGDIOBJ) hfont, sizeof(lf), &lf))
  154. {
  155. lf.lfWeight = FW_BOLD;
  156. m_hBoldFont = CreateFontIndirect(&lf);
  157. if (NULL != m_hBoldFont)
  158. {
  159. // Set the font
  160. SendMessage(hwndPowerUsers, WM_SETFONT,
  161. (WPARAM) m_hBoldFont, 0);
  162. SendDlgItemMessage(hwnd, IDC_USERS,
  163. WM_SETFONT, (WPARAM) m_hBoldFont, 0);
  164. SendDlgItemMessage(hwnd, IDC_OTHER,
  165. WM_SETFONT, (WPARAM) m_hBoldFont, 0);
  166. }
  167. }
  168. }
  169. }
  170. BOOL CGroupPageBase::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  171. {
  172. switch(codeNotify)
  173. {
  174. case CBN_SELCHANGE:
  175. SetGroupDescription(hwndCtl, GetDlgItem(hwnd, IDC_GROUPDESC));
  176. PropSheet_Changed(GetParent(hwnd), hwnd);
  177. break;
  178. case BN_CLICKED:
  179. // Handle radio clicks
  180. switch (id)
  181. {
  182. case IDC_POWERUSERS:
  183. case IDC_USERS:
  184. case IDC_OTHER:
  185. PropSheet_Changed(GetParent(hwnd), hwnd);
  186. OnRadioChanged(hwnd, id);
  187. }
  188. break;
  189. }
  190. return FALSE;
  191. }
  192. /**************************************************************
  193. CGroupWizardPage Implementation
  194. **************************************************************/
  195. INT_PTR CGroupWizardPage::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  196. {
  197. switch (uMsg)
  198. {
  199. HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  200. HANDLE_MSG(hwndDlg, WM_NOTIFY, OnNotify);
  201. HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  202. }
  203. return FALSE;
  204. }
  205. BOOL CGroupWizardPage::OnNotify(HWND hwnd, int idCtrl, LPNMHDR pnmh)
  206. {
  207. switch (pnmh->code)
  208. {
  209. case PSN_SETACTIVE:
  210. {
  211. PropSheet_SetWizButtons(pnmh->hwndFrom, PSWIZB_BACK | PSWIZB_FINISH);
  212. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, 0);
  213. return TRUE;
  214. }
  215. case PSN_WIZFINISH:
  216. {
  217. // Read in the local group name
  218. CUserInfo::GROUPPSEUDONYM gs;
  219. GetSelectedGroup(hwnd, m_pUserInfo->m_szGroups,
  220. ARRAYSIZE(m_pUserInfo->m_szGroups), &gs);
  221. // Don't close wizard by default
  222. LONG_PTR finishResult = (LONG_PTR) hwnd;
  223. CWaitCursor cur;
  224. if (SUCCEEDED(m_pUserInfo->Create(hwnd, gs)))
  225. {
  226. m_pUserInfo->m_fHaveExtraUserInfo = FALSE;
  227. // Close wizard
  228. finishResult = 0;
  229. }
  230. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, finishResult);
  231. return TRUE;
  232. }
  233. }
  234. return FALSE;
  235. }
  236. /**************************************************************
  237. CGroupPropertyPage Implementation
  238. **************************************************************/
  239. INT_PTR CGroupPropertyPage::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  240. {
  241. switch (uMsg)
  242. {
  243. HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  244. HANDLE_MSG(hwndDlg, WM_NOTIFY, OnNotify);
  245. HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  246. }
  247. return FALSE;
  248. }
  249. BOOL CGroupPropertyPage::OnNotify(HWND hwnd, int idCtrl, LPNMHDR pnmh)
  250. {
  251. switch(pnmh->code)
  252. {
  253. case PSN_APPLY:
  254. {
  255. // Check to see if the group needs updating on Apply
  256. TCHAR szTemp[MAX_GROUP + 1];
  257. // Read in the local group name
  258. CUserInfo::GROUPPSEUDONYM gs;
  259. GetSelectedGroup(hwnd, szTemp,
  260. ARRAYSIZE(szTemp), &gs);
  261. if (StrCmp(szTemp, m_pUserInfo->m_szGroups) != 0)
  262. {
  263. HRESULT hr = m_pUserInfo->UpdateGroup(hwnd, szTemp, gs);
  264. if (SUCCEEDED(hr))
  265. {
  266. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, PSNRET_NOERROR);
  267. }
  268. else
  269. {
  270. TCHAR szDomainUser[MAX_DOMAIN + MAX_USER + 2];
  271. MakeDomainUserString(m_pUserInfo->m_szDomain, m_pUserInfo->m_szUsername,
  272. szDomainUser, ARRAYSIZE(szDomainUser));
  273. ::DisplayFormatMessage(hwnd, IDS_USR_APPLET_CAPTION,
  274. IDS_USR_UPDATE_GROUP_ERROR, MB_ICONERROR | MB_OK,
  275. szDomainUser);
  276. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
  277. }
  278. }
  279. }
  280. break;
  281. default:
  282. return FALSE;
  283. }
  284. return TRUE;
  285. }