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.

263 lines
6.1 KiB

  1. // UserPage.cpp : Implementation of CUserPage
  2. #include "stdafx.h"
  3. #include "MainPage.h"
  4. #include "UserPage.h"
  5. EXTERN_C const CLSID CLSID_UserPage = __uuidof(CUserPage);
  6. /////////////////////////////////////////////////////////////////////////////
  7. // CUserPage
  8. LPWSTR CUserPage::c_aHTML[] =
  9. {
  10. L"res://nusrmgr.exe/userpage.htm",
  11. L"res://nusrmgr.exe/userpage_sec.htm"
  12. };
  13. STDMETHODIMP CUserPage::SetFrame(ITaskFrame* pFrame)
  14. {
  15. HRESULT hr;
  16. ATOMICRELEASE(_pUser);
  17. _bSelf = FALSE;
  18. _bRunningAsOwner = FALSE;
  19. _bRunningAsAdmin = FALSE;
  20. hr = CHTMLPageImpl<CUserPage,IUserPageUI>::SetFrame(pFrame);
  21. if (NULL != _pBag)
  22. {
  23. CComVariant var;
  24. hr = _pBag->Read(UA_PROP_USERLIST, &var, NULL);
  25. if (SUCCEEDED(hr))
  26. {
  27. CComQIPtr<ILogonEnumUsers> spUserList(var.punkVal);
  28. if (spUserList)
  29. {
  30. CComPtr<ILogonUser> spLoggedOnUser;
  31. hr = spUserList->get_currentUser(&spLoggedOnUser);
  32. var.Clear();
  33. hr = _pBag->Read(UA_PROP_PAGEINITDATA, &var, NULL);
  34. if (SUCCEEDED(hr))
  35. {
  36. hr = spUserList->item(var, &_pUser);
  37. if (SUCCEEDED(hr))
  38. {
  39. if (spLoggedOnUser)
  40. _bSelf = IsSameAccount(spLoggedOnUser, var.bstrVal);
  41. // Clear the pageinit prop
  42. var.Clear();
  43. _pBag->Write(UA_PROP_PAGEINITDATA, &var);
  44. }
  45. }
  46. else
  47. {
  48. _pUser = spLoggedOnUser;
  49. if (NULL != _pUser)
  50. {
  51. _pUser->AddRef();
  52. _bSelf = TRUE;
  53. }
  54. }
  55. if (spLoggedOnUser)
  56. {
  57. _bRunningAsOwner = IsOwnerAccount(spLoggedOnUser);
  58. _bRunningAsAdmin = IsAdminAccount(spLoggedOnUser);
  59. }
  60. }
  61. }
  62. }
  63. return hr;
  64. }
  65. STDMETHODIMP CUserPage::Reinitialize(ULONG /*reserved*/)
  66. {
  67. if (NULL == _pBag || NULL == _pUser)
  68. return E_UNEXPECTED;
  69. CComVariant var;
  70. if (SUCCEEDED(_pBag->Read(UA_PROP_PAGEINITDATA, &var, NULL)) &&
  71. (VT_BSTR == var.vt) &&
  72. IsSameAccount(_pUser, var.bstrVal))
  73. {
  74. // It's the same user, so this page can be reused as is.
  75. // TODO: actually, should reinit in case something has changed and
  76. // we need to hide/show different tasks. Also, the UserDisplayHTML
  77. // may be out of date.
  78. //
  79. // Leaving this in for now to verify frame optimizations
  80. // Clear the pageinit prop
  81. var.Clear();
  82. _pBag->Write(UA_PROP_PAGEINITDATA, &var);
  83. return S_OK;
  84. }
  85. // We could do some more work to re-init the page here.
  86. // Failing causes the page to be thrown away and recreated.
  87. return E_FAIL;
  88. }
  89. STDMETHODIMP CUserPage::get_passwordRequired(VARIANT_BOOL *pVal)
  90. {
  91. if (NULL == pVal)
  92. return E_POINTER;
  93. *pVal = NULL;
  94. if (NULL == _pUser)
  95. return E_UNEXPECTED;
  96. return _pUser->get_passwordRequired(pVal);
  97. }
  98. STDMETHODIMP CUserPage::get_isAdmin(VARIANT_BOOL *pVal)
  99. {
  100. if (NULL == _pUser)
  101. return E_UNEXPECTED;
  102. return _GetBool((_bSelf ? _bRunningAsAdmin : IsAdminAccount(_pUser)), pVal);
  103. }
  104. STDMETHODIMP CUserPage::get_isGuest(VARIANT_BOOL *pVal)
  105. {
  106. if (NULL == _pUser)
  107. return E_UNEXPECTED;
  108. return _GetBool(IsGuestAccount(_pUser), pVal);
  109. }
  110. STDMETHODIMP CUserPage::get_isOwner(VARIANT_BOOL *pVal)
  111. {
  112. if (NULL == _pUser)
  113. return E_UNEXPECTED;
  114. return _GetBool(IsOwnerAccount(_pUser), pVal);
  115. }
  116. STDMETHODIMP CUserPage::get_userDisplayName(BSTR *pVal)
  117. {
  118. if (NULL == pVal)
  119. return E_POINTER;
  120. *pVal = NULL;
  121. if (NULL == _pUser)
  122. return E_UNEXPECTED;
  123. *pVal = GetUserDisplayName(_pUser);
  124. return S_OK;
  125. }
  126. STDMETHODIMP CUserPage::createUserDisplayHTML(BSTR *pVal)
  127. {
  128. if (NULL == pVal)
  129. return E_POINTER;
  130. *pVal = NULL;
  131. if (NULL == _pUser)
  132. return E_UNEXPECTED;
  133. LPWSTR pszHTML = CreateUserDisplayHTML(_pUser);
  134. if (NULL != pszHTML)
  135. {
  136. *pVal = SysAllocString(pszHTML);
  137. LocalFree(pszHTML);
  138. }
  139. return S_OK;
  140. }
  141. STDMETHODIMP CUserPage::countOwners(UINT *pVal)
  142. {
  143. if (NULL == pVal)
  144. return E_POINTER;
  145. *pVal = 0;
  146. if (NULL == _pBag)
  147. return E_UNEXPECTED;
  148. CComVariant var;
  149. HRESULT hr = _pBag->Read(UA_PROP_USERLIST, &var, NULL);
  150. if (SUCCEEDED(hr))
  151. {
  152. // The user list is saved as VT_UNKNOWN
  153. hr = CountOwners(var.punkVal, pVal);
  154. }
  155. return hr;
  156. }
  157. STDMETHODIMP CUserPage::enableGuest(VARIANT_BOOL bEnable)
  158. {
  159. if (NULL == _pTaskFrame)
  160. return E_UNEXPECTED;
  161. HRESULT hr = EnableGuest(bEnable);
  162. if (SUCCEEDED(hr))
  163. hr = _pTaskFrame->ShowPage(CLSID_MainPage, TRUE);
  164. return hr;
  165. }
  166. HRESULT CountOwners(IUnknown* punkUserList, UINT *pVal)
  167. {
  168. if (NULL == pVal)
  169. return E_POINTER;
  170. *pVal = 0;
  171. if (NULL == punkUserList)
  172. return E_INVALIDARG;
  173. UINT cOwners = 0;
  174. CComQIPtr<ILogonEnumUsers> spUserList(punkUserList);
  175. if (spUserList)
  176. {
  177. // Note that 'Administrator' is not included in the count.
  178. // Note also that we don't really need a true count, we only
  179. // need to know whether there is 0, 1, or many. Therefore, we
  180. // always stop counting at 2.
  181. UINT cUsers = 0;
  182. spUserList->get_length(&cUsers);
  183. VARIANT var;
  184. var.vt = VT_I4;
  185. for (UINT i = 0; i < cUsers && cOwners < 2; i++)
  186. {
  187. CComPtr<ILogonUser> spUser;
  188. var.lVal = i;
  189. spUserList->item(var, &spUser);
  190. if (spUser && IsOwnerAccount(spUser) && !IsAdminAccount(spUser))
  191. ++cOwners;
  192. }
  193. }
  194. *pVal = cOwners;
  195. return S_OK;
  196. }
  197. HRESULT EnableGuest(VARIANT_BOOL bEnable)
  198. {
  199. CComPtr<ILocalMachine> spLocalMachine;
  200. HRESULT hr = spLocalMachine.CoCreateInstance(CLSID_ShellLocalMachine);
  201. if (SUCCEEDED(hr))
  202. hr = spLocalMachine->put_isGuestEnabled(bEnable);
  203. return hr;
  204. }