PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Profile.cs

#
C# | 157 lines | 111 code | 28 blank | 18 comment | 18 complexity | 3a80c6594c1722670bfa498397e9d31c MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace App_Code
  2. {
  3. using System;
  4. using System.Web.Script.Services;
  5. using System.Web.Security;
  6. using System.Web.Services;
  7. using BlogEngine.Core;
  8. using BlogEngine.Core.Json;
  9. /// <summary>
  10. /// The profile.
  11. /// </summary>
  12. [WebService(Namespace = "http://tempuri.org/")]
  13. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  14. [ScriptService]
  15. public class Profile : WebService
  16. {
  17. #region Constants and Fields
  18. /// <summary>
  19. /// JSON object that will be return back to client
  20. /// </summary>
  21. private readonly JsonResponse response;
  22. #endregion
  23. #region Constructors and Destructors
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="Profile"/> class.
  26. /// </summary>
  27. public Profile()
  28. {
  29. this.response = new JsonResponse();
  30. }
  31. #endregion
  32. #region Public Methods
  33. /// <summary>
  34. /// Saves the specified id.
  35. /// </summary>
  36. /// <param name="id">The profile id.</param>
  37. /// <param name="vals">The values.</param>
  38. /// <param name="roles">The roles.</param>
  39. /// <returns>JSON response.</returns>
  40. [WebMethod]
  41. public JsonResponse Save(string id, string[] vals, string[] roles)
  42. {
  43. this.response.Success = false;
  44. if (string.IsNullOrEmpty(vals[0]))
  45. {
  46. this.response.Message = Resources.labels.displayNameIsRequired;
  47. return this.response;
  48. }
  49. bool isSelf = id.Equals(Security.CurrentUser.Identity.Name, StringComparison.OrdinalIgnoreCase);
  50. if (isSelf && !Security.IsAuthorizedTo(Rights.EditOwnUser))
  51. {
  52. this.response.Message = Resources.labels.notAuthorized;
  53. return this.response;
  54. }
  55. else if (!isSelf && !Security.IsAuthorizedTo(Rights.EditOtherUsers))
  56. {
  57. this.response.Message = Resources.labels.notAuthorized;
  58. return this.response;
  59. }
  60. var pf = AuthorProfile.GetProfile(id) ?? new AuthorProfile(id);
  61. try
  62. {
  63. pf.DisplayName = vals[0];
  64. pf.FirstName = vals[1];
  65. pf.MiddleName = vals[2];
  66. pf.LastName = vals[3];
  67. pf.EmailAddress = vals[4];
  68. DateTime date;
  69. if (vals[5].Length == 0)
  70. {
  71. vals[5] = "1/1/1001";
  72. }
  73. if (DateTime.TryParse(vals[5], out date))
  74. {
  75. pf.Birthday = date;
  76. }
  77. else
  78. {
  79. this.response.Message = "Date must be in format mm/dd/yyyy";
  80. return this.response;
  81. }
  82. pf.PhotoUrl = vals[6];
  83. pf.Private = false;
  84. bool prv;
  85. if (bool.TryParse(vals[7], out prv))
  86. {
  87. pf.Private = prv;
  88. }
  89. pf.PhoneMobile = vals[8];
  90. pf.PhoneMain = vals[9];
  91. pf.PhoneFax = vals[10];
  92. pf.CityTown = vals[11];
  93. pf.RegionState = vals[12];
  94. pf.Country = vals[13]; // ddlCountry.SelectedValue;
  95. // pf.Company = tbCompany.Text;
  96. pf.AboutMe = vals[14];
  97. pf.Save();
  98. bool saveRoles = false;
  99. if (isSelf && Security.IsAuthorizedTo(Rights.EditOwnRoles))
  100. saveRoles = true;
  101. else if (!isSelf && Security.IsAuthorizedTo(Rights.EditOtherUsersRoles))
  102. saveRoles = true;
  103. if (saveRoles)
  104. {
  105. // remove all user roles and add only checked
  106. string[] currentRoles = Roles.GetRolesForUser(id);
  107. if (currentRoles.Length > 0)
  108. {
  109. Roles.RemoveUserFromRoles(id, currentRoles);
  110. }
  111. if (roles.GetLength(0) > 0)
  112. {
  113. Roles.AddUsersToRoles(new string[] { id }, roles);
  114. }
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. Utils.Log(string.Format("Profile.Edit: {0}", ex.Message));
  120. this.response.Message = string.Format(Resources.labels.couldNotUpdateProfile, vals[0]);
  121. return this.response;
  122. }
  123. this.response.Success = true;
  124. this.response.Message = string.Format(Resources.labels.profileUpdated, vals[0]);
  125. return this.response;
  126. }
  127. #endregion
  128. }
  129. }