PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Web/ProfileView.aspx.cs

#
C# | 385 lines | 299 code | 66 blank | 20 comment | 63 complexity | 1de372c7076214a3059583546ca11d7d MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. /// Author: Joe Audette
  2. /// Created: 2004-10-03
  3. /// Last Modified: 2010-05-19
  4. ///
  5. /// The use and distribution terms for this software are covered by the
  6. /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. /// which can be found in the file CPL.TXT at the root of this distribution.
  8. /// By using this software in any fashion, you are agreeing to be bound by
  9. /// the terms of this license.
  10. ///
  11. /// You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.Globalization;
  14. using mojoPortal.Business;
  15. using mojoPortal.Business.WebHelpers;
  16. using mojoPortal.Web.Configuration;
  17. using mojoPortal.Web.Controls;
  18. using mojoPortal.Web.Framework;
  19. using Resources;
  20. using ConsentToken = mojoPortal.Web.WindowsLiveLogin.ConsentToken;
  21. namespace mojoPortal.Web.UI.Pages
  22. {
  23. public partial class ProfileView : NonCmsBasePage
  24. {
  25. private Guid userGuid = Guid.Empty;
  26. private int userID = -1;
  27. private Double timeOffset = 0;
  28. private TimeZoneInfo timeZone = null;
  29. //Gravatar public enum RatingType { G, PG, R, X }
  30. private Gravatar.RatingType MaxAllowedGravatarRating = SiteUtils.GetMaxAllowedGravatarRating();
  31. private bool allowGravatars = false;
  32. private bool disableAvatars = true;
  33. private string avatarPath = string.Empty;
  34. private SiteUser siteUser = null;
  35. private bool allowView = false;
  36. protected string allowedImageUrlRegexPattern = SecurityHelper.RegexRelativeImageUrlPatern;
  37. #region OnInit
  38. override protected void OnInit(EventArgs e)
  39. {
  40. base.OnInit(e);
  41. this.Load += new EventHandler(this.Page_Load);
  42. SuppressMenuSelection();
  43. SuppressPageMenu();
  44. }
  45. #endregion
  46. private void Page_Load(object sender, EventArgs e)
  47. {
  48. LoadSettings();
  49. if (!allowView)
  50. {
  51. if (!Request.IsAuthenticated)
  52. {
  53. SiteUtils.RedirectToLoginPage(this);
  54. return;
  55. }
  56. else
  57. {
  58. WebUtils.SetupRedirect(this, SiteRoot);
  59. return;
  60. }
  61. }
  62. if (SiteUtils.SslIsAvailable() && WebConfigSettings.ForceSslOnProfileView) { SiteUtils.ForceSsl(); }
  63. PopulateControls();
  64. }
  65. private void PopulateControls()
  66. {
  67. if (siteUser != null)
  68. {
  69. this.lblCreatedDate.Text = siteUser.DateCreated.AddHours(timeOffset).ToString();
  70. this.lblTotalPosts.Text = siteUser.TotalPosts.ToString(CultureInfo.InvariantCulture);
  71. this.lblUserName.Text = Server.HtmlEncode(siteUser.Name);
  72. Title = SiteUtils.FormatPageTitle(siteSettings, string.Format(CultureInfo.InvariantCulture,
  73. Resource.PageTitleFormatProfilePage, Server.HtmlEncode(siteUser.Name)));
  74. MetaDescription = string.Format(CultureInfo.InvariantCulture,
  75. Resource.ProfileViewMetaFormat, Server.HtmlEncode(siteUser.Name));
  76. if (allowGravatars)
  77. {
  78. imgAvatar.Visible = false;
  79. gravatar1.Visible = true;
  80. gravatar1.Email = siteUser.Email;
  81. gravatar1.MaxAllowedRating = MaxAllowedGravatarRating;
  82. }
  83. else
  84. {
  85. gravatar1.Visible = false;
  86. if (disableAvatars)
  87. {
  88. divAvatar.Visible = false;
  89. }
  90. else
  91. {
  92. if (siteUser.AvatarUrl.Length > 0)
  93. {
  94. this.imgAvatar.Src = avatarPath + siteUser.AvatarUrl;
  95. }
  96. }
  97. }
  98. lnkUserPosts.UserId = siteUser.UserId;
  99. lnkUserPosts.TotalPosts = siteUser.TotalPosts;
  100. if (siteUser.TimeZoneId.Length > 0)
  101. {
  102. TimeZoneInfo userTz = SiteUtils.GetTimeZone(siteUser.TimeZoneId);
  103. if (userTz != null)
  104. {
  105. pnlTimeZone.Visible = true;
  106. if (userTz.IsDaylightSavingTime(DateTime.UtcNow))
  107. lblTimeZone.Text = userTz.DaylightNameWithOffset();
  108. else
  109. lblTimeZone.Text = userTz.DisplayName;
  110. }
  111. }
  112. if (WebConfigSettings.UseRelatedSiteMode)
  113. {
  114. // this can't be used in related site mode
  115. // because we can't assume forum posts were in this site.
  116. divForumPosts.Visible = false;
  117. }
  118. if (Request.IsAuthenticated)
  119. {
  120. ShowAuthenticatedProperties(siteUser);
  121. }
  122. else
  123. {
  124. ShowAnonymousProperties(siteUser);
  125. }
  126. PopulateMessenger();
  127. }
  128. else
  129. {
  130. this.lblUserName.Text = "User not found";
  131. imgAvatar.Visible = false;
  132. gravatar1.Visible = false;
  133. }
  134. }
  135. private void LoadSettings()
  136. {
  137. avatarPath = Page.ResolveUrl("~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/useravatars/");
  138. allowView = WebUser.IsInRoles(siteSettings.RolesThatCanViewMemberList);
  139. userID = WebUtils.ParseInt32FromQueryString("userid", true, userID);
  140. timeOffset = SiteUtils.GetUserTimeOffset();
  141. timeZone = SiteUtils.GetUserTimeZone();
  142. userGuid = WebUtils.ParseGuidFromQueryString("u", Guid.Empty);
  143. if (userID > -1)
  144. {
  145. siteUser = new SiteUser(siteSettings, userID);
  146. if (siteUser.UserGuid == Guid.Empty) { siteUser = null; }
  147. }
  148. else if(userGuid != Guid.Empty)
  149. {
  150. siteUser = new SiteUser(siteSettings, userGuid);
  151. if (siteUser.UserGuid == Guid.Empty) { siteUser = null; }
  152. }
  153. switch (siteSettings.AvatarSystem)
  154. {
  155. case "gravatar":
  156. allowGravatars = true;
  157. disableAvatars = true;
  158. break;
  159. case "internal":
  160. allowGravatars = false;
  161. disableAvatars = false;
  162. break;
  163. case "none":
  164. default:
  165. allowGravatars = false;
  166. disableAvatars = true;
  167. break;
  168. }
  169. AddClassToBody("profileview");
  170. }
  171. private void PopulateMessenger()
  172. {
  173. if (WebConfigSettings.GloballyDisableMemberUseOfWindowsLiveMessenger) { return; }
  174. if (!siteSettings.AllowWindowsLiveMessengerForMembers) { return; }
  175. if (siteUser == null) { return; }
  176. if (!siteUser.EnableLiveMessengerOnProfile) { return; }
  177. if (siteUser.LiveMessengerId.Length == 0) { return; }
  178. divLiveMessenger.Visible = true;
  179. chat1.Invitee = siteUser.LiveMessengerId;
  180. //chat1.InviteeDisplayName = siteUser.Name;
  181. if (WebConfigSettings.TestLiveMessengerDelegation)
  182. {
  183. WindowsLiveLogin wl = WindowsLiveHelper.GetWindowsLiveLogin();
  184. WindowsLiveMessenger m = new WindowsLiveMessenger(wl);
  185. ConsentToken token = m.DecodeToken(siteUser.LiveMessengerDelegationToken);
  186. ConsentToken refreshedToken = m.RefreshConsent(token);
  187. if (refreshedToken != null)
  188. {
  189. chat1.DelegationToken = refreshedToken.DelegationToken;
  190. string signedParams = WindowsLiveMessenger.SignParameters(
  191. refreshedToken.SessionKey,
  192. siteUser.Name,
  193. string.Empty,
  194. string.Empty);
  195. chat1.SignedParams = signedParams;
  196. }
  197. else
  198. {
  199. //chat1.DelegationToken = siteUser.LiveMessengerDelegationToken;
  200. chat1.DelegationToken = token.DelegationToken;
  201. string signedParams = WindowsLiveMessenger.SignParameters(
  202. token.SessionKey,
  203. siteUser.Name,
  204. string.Empty,
  205. string.Empty);
  206. chat1.SignedParams = signedParams;
  207. }
  208. }
  209. }
  210. private void ShowAuthenticatedProperties(SiteUser siteUser)
  211. {
  212. mojoProfileConfiguration profileConfig = mojoProfileConfiguration.GetConfig();
  213. if (profileConfig != null)
  214. {
  215. foreach (mojoProfilePropertyDefinition propertyDefinition in profileConfig.PropertyDefinitions)
  216. {
  217. #if!MONO
  218. // we are using the new TimeZoneInfo list but it doesn't work under Mono
  219. // this makes us skip the TimeOffsetHours setting from mojoProfile.config which is not used under windows
  220. if (propertyDefinition.Name == mojoProfilePropertyDefinition.TimeOffsetHoursKey) { continue; }
  221. #endif
  222. // we allow this to be configured as a profile property so it can be required for registration
  223. // but we don't need to load it here because we have a dedicated control for the property already
  224. if (propertyDefinition.Name == mojoProfilePropertyDefinition.TimeZoneIdKey) { continue; }
  225. if (
  226. (propertyDefinition.VisibleToAuthenticated)
  227. && (
  228. (propertyDefinition.OnlyAvailableForRoles.Length == 0)
  229. || (siteUser.IsInRoles(propertyDefinition.OnlyAvailableForRoles))
  230. )
  231. &&(
  232. (propertyDefinition.OnlyVisibleForRoles.Length == 0)
  233. || (WebUser.IsInRoles(propertyDefinition.OnlyVisibleForRoles))
  234. )
  235. )
  236. {
  237. object propValue = siteUser.GetProperty(propertyDefinition.Name, propertyDefinition.SerializeAs, propertyDefinition.LazyLoad);
  238. if (propValue != null)
  239. {
  240. mojoProfilePropertyDefinition.SetupReadOnlyPropertyControl(
  241. pnlProfileProperties,
  242. propertyDefinition,
  243. propValue.ToString(),
  244. timeOffset,
  245. timeZone);
  246. }
  247. else
  248. {
  249. mojoProfilePropertyDefinition.SetupReadOnlyPropertyControl(
  250. pnlProfileProperties,
  251. propertyDefinition,
  252. propertyDefinition.DefaultValue,
  253. timeOffset,
  254. timeZone);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. private void ShowAnonymousProperties(SiteUser siteUser)
  261. {
  262. bool wouldSeeMoreIfAuthenticated = false;
  263. mojoProfileConfiguration profileConfig = mojoProfileConfiguration.GetConfig();
  264. if (profileConfig != null)
  265. {
  266. foreach (mojoProfilePropertyDefinition propertyDefinition in profileConfig.PropertyDefinitions)
  267. {
  268. if (
  269. (propertyDefinition.VisibleToAnonymous)
  270. && (propertyDefinition.OnlyVisibleForRoles.Length == 0)
  271. &&(
  272. (propertyDefinition.OnlyAvailableForRoles.Length == 0)
  273. || (siteUser.IsInRoles(propertyDefinition.OnlyAvailableForRoles))
  274. )
  275. )
  276. {
  277. object propValue = siteUser.GetProperty(propertyDefinition.Name, propertyDefinition.SerializeAs, propertyDefinition.LazyLoad);
  278. if (propValue != null)
  279. {
  280. mojoProfilePropertyDefinition.SetupReadOnlyPropertyControl(
  281. pnlProfileProperties,
  282. propertyDefinition,
  283. propValue.ToString(),
  284. timeOffset,
  285. timeZone);
  286. }
  287. else
  288. {
  289. mojoProfilePropertyDefinition.SetupReadOnlyPropertyControl(
  290. pnlProfileProperties,
  291. propertyDefinition,
  292. propertyDefinition.DefaultValue,
  293. timeOffset,
  294. timeZone);
  295. }
  296. }
  297. else
  298. {
  299. if (
  300. (propertyDefinition.VisibleToAuthenticated)
  301. && (propertyDefinition.OnlyVisibleForRoles.Length == 0)
  302. &&(
  303. (propertyDefinition.OnlyAvailableForRoles.Length == 0)
  304. || (siteUser.IsInRoles(propertyDefinition.OnlyAvailableForRoles))
  305. )
  306. )
  307. {
  308. wouldSeeMoreIfAuthenticated = true;
  309. }
  310. }
  311. }
  312. }
  313. if (wouldSeeMoreIfAuthenticated)
  314. {
  315. lblMessage.Text = ProfileResource.WouldSeeMoreIfAuthenticatedMessage;
  316. }
  317. }
  318. }
  319. }