PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/BlogEngine/BlogEngine.NET/admin/Users/Profile.aspx.cs

#
C# | 142 lines | 94 code | 24 blank | 24 comment | 13 complexity | bdecdf7b159d77635dd03f640f40b7f4 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace Admin.Users
  2. {
  3. using System;
  4. using System.Linq;
  5. using System.Web.Services;
  6. using System.Web.Security;
  7. using BlogEngine.Core;
  8. /// <summary>
  9. /// The admin pages profile.
  10. /// </summary>
  11. public partial class ProfilePage : System.Web.UI.Page
  12. {
  13. #region Constants and Fields
  14. /// <summary>
  15. /// The id string.
  16. /// </summary>
  17. private string theId = string.Empty;
  18. #endregion
  19. #region Properties
  20. /// <summary>
  21. /// Gets RolesList.
  22. /// </summary>
  23. protected string RolesList
  24. {
  25. get
  26. {
  27. var ret = string.Empty;
  28. const string Ptrn = "<input type=\"checkbox\" id=\"{0}\" class=\"chkRole\" {1} /><span class=\"lbl\">{0}</span>";
  29. var allRoles = System.Web.Security.Roles.GetAllRoles().Where(r => !r.Equals(BlogConfig.AnonymousRole, StringComparison.OrdinalIgnoreCase));
  30. return allRoles.Aggregate(ret, (current, r) => current + (System.Web.Security.Roles.IsUserInRole(theId, r) ? string.Format(Ptrn, r, "checked") : string.Format(Ptrn, r, string.Empty)));
  31. }
  32. }
  33. #endregion
  34. #region Public Methods
  35. /// <summary>
  36. /// The get profile.
  37. /// </summary>
  38. /// <param name="id">
  39. /// The profile id.
  40. /// </param>
  41. /// <returns>
  42. /// An AuthorProfile.
  43. /// </returns>
  44. [WebMethod]
  45. public static AuthorProfile GetProfile(string id)
  46. {
  47. if (!Utils.StringIsNullOrWhitespace(id))
  48. {
  49. bool canEditRoles;
  50. if (!CanUserEditProfile(id, out canEditRoles))
  51. return null;
  52. return AuthorProfile.GetProfile(id) ?? new AuthorProfile()
  53. {
  54. DisplayName = string.Empty,
  55. FirstName = string.Empty,
  56. MiddleName = string.Empty,
  57. LastName = string.Empty,
  58. Birthday = new DateTime(1001, 1, 1),
  59. PhotoUrl = string.Empty,
  60. EmailAddress = string.Empty,
  61. PhoneMobile = string.Empty,
  62. PhoneMain = string.Empty,
  63. PhoneFax = string.Empty,
  64. CityTown = string.Empty,
  65. RegionState = string.Empty,
  66. Country = string.Empty,
  67. AboutMe = string.Empty
  68. };
  69. }
  70. return null;
  71. }
  72. #endregion
  73. #region Methods
  74. private static bool CanUserEditProfile(string id, out bool canEditRoles)
  75. {
  76. canEditRoles = false;
  77. if (Utils.StringIsNullOrWhitespace(id))
  78. return false;
  79. MembershipUser user = Membership.GetUser(id);
  80. if (user == null)
  81. return false;
  82. bool membershipUserIsSelf = user.UserName.Equals(Security.CurrentUser.Identity.Name, StringComparison.OrdinalIgnoreCase);
  83. if (membershipUserIsSelf && Security.IsAuthorizedTo(BlogEngine.Core.Rights.EditOwnRoles))
  84. canEditRoles = true;
  85. else if (!membershipUserIsSelf && Security.IsAuthorizedTo(BlogEngine.Core.Rights.EditOtherUsersRoles))
  86. canEditRoles = true;
  87. if (membershipUserIsSelf)
  88. return Security.IsAuthorizedTo(BlogEngine.Core.Rights.EditOwnUser);
  89. else
  90. return Security.IsAuthorizedTo(BlogEngine.Core.Rights.EditOtherUsers);
  91. }
  92. /// <summary>
  93. /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event to initialize the page.
  94. /// </summary>
  95. /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
  96. protected override void OnInit(EventArgs e)
  97. {
  98. // Rights.AccessAdminPages isn't needed here. If self-registration is turned
  99. // on, we will allow a user who cannot AccessAdminPages to edit their profile.
  100. if (!Security.IsAuthenticated)
  101. {
  102. Security.RedirectForUnauthorizedRequest();
  103. return;
  104. }
  105. bool canEditRoles = false;
  106. if (!CanUserEditProfile(Request.QueryString["id"], out canEditRoles))
  107. {
  108. Response.Redirect("Users.aspx");
  109. return;
  110. }
  111. this.theId = Request.QueryString["id"];
  112. phRoles.Visible = canEditRoles;
  113. phRightContentBox.Visible = Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages);
  114. base.OnInit(e);
  115. }
  116. #endregion
  117. }
  118. }