PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/UserService.cs

#
C# | 236 lines | 170 code | 33 blank | 33 comment | 33 complexity | 4068a397dcac9f5beaa9bec2e51f3413 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.Linq;
  5. using System.Web.Script.Services;
  6. using System.Web.Security;
  7. using System.Web.Services;
  8. using BlogEngine.Core;
  9. using BlogEngine.Core.Json;
  10. /// <summary>
  11. /// The user service.
  12. /// </summary>
  13. [WebService(Namespace = "http://tempuri.org/")]
  14. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  15. [ScriptService]
  16. public class UserService : WebService
  17. {
  18. #region Constants and Fields
  19. /// <summary>
  20. /// The response.
  21. /// </summary>
  22. private readonly JsonResponse response;
  23. #endregion
  24. #region Constructors and Destructors
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="UserService"/> class.
  27. /// </summary>
  28. public UserService()
  29. {
  30. this.response = new JsonResponse();
  31. }
  32. #endregion
  33. #region Public Methods
  34. /// <summary>
  35. /// Adds the specified user.
  36. /// </summary>
  37. /// <param name="user">The user to add.</param>
  38. /// <param name="pwd">The password to add.</param>
  39. /// <param name="email">The email to add.</param>
  40. /// <param name="roles">Roles for new user</param>
  41. /// <returns>JSON Response.</returns>
  42. [WebMethod]
  43. public JsonResponse Add(string user, string pwd, string email, string[] roles)
  44. {
  45. if (!Security.IsAuthorizedTo(Rights.CreateNewUsers))
  46. {
  47. return new JsonResponse() { Message = Resources.labels.notAuthorized };
  48. }
  49. else if (Utils.StringIsNullOrWhitespace(user))
  50. {
  51. return new JsonResponse() { Message = Resources.labels.userArgumentInvalid };
  52. }
  53. else if (Utils.StringIsNullOrWhitespace(pwd))
  54. {
  55. return new JsonResponse() { Message = Resources.labels.passwordArgumentInvalid };
  56. }
  57. else if (Utils.StringIsNullOrWhitespace(email) || !Utils.IsEmailValid(email))
  58. {
  59. return new JsonResponse() { Message = Resources.labels.emailArgumentInvalid };
  60. }
  61. user = user.Trim();
  62. email = email.Trim();
  63. pwd = pwd.Trim();
  64. if (Membership.GetUser(user) != null)
  65. {
  66. return new JsonResponse() { Message = string.Format(Resources.labels.userAlreadyExists, user) };
  67. }
  68. try
  69. {
  70. Membership.CreateUser(user, pwd, email);
  71. if (Security.IsAuthorizedTo(Rights.EditOtherUsersRoles))
  72. {
  73. if (roles.GetLength(0) > 0)
  74. {
  75. Roles.AddUsersToRoles(new string[] { user }, roles);
  76. }
  77. }
  78. return new JsonResponse() { Success = true, Message = string.Format(Resources.labels.userHasBeenCreated, user) };
  79. }
  80. catch (Exception ex)
  81. {
  82. Utils.Log("UserService.Add: ", ex);
  83. return new JsonResponse() { Message = string.Format(Resources.labels.couldNotCreateUser, user, ex.Message) };
  84. }
  85. }
  86. /// <summary>
  87. /// Deletes the specified id.
  88. /// </summary>
  89. /// <param name="id">The username.</param>
  90. /// <returns>JSON Response</returns>
  91. [WebMethod]
  92. public JsonResponse Delete(string id)
  93. {
  94. if (string.IsNullOrEmpty(id))
  95. {
  96. this.response.Success = false;
  97. this.response.Message = Resources.labels.userNameIsRequired;
  98. return this.response;
  99. }
  100. bool isSelf = id.Equals(Security.CurrentUser.Identity.Name, StringComparison.OrdinalIgnoreCase);
  101. if (isSelf && !Security.IsAuthorizedTo(Rights.DeleteUserSelf))
  102. {
  103. return new JsonResponse() { Message = Resources.labels.notAuthorized };
  104. }
  105. else if (!isSelf && !Security.IsAuthorizedTo(Rights.DeleteUsersOtherThanSelf))
  106. {
  107. return new JsonResponse() { Message = Resources.labels.notAuthorized };
  108. }
  109. // Last check - it should not be possible to remove the last use who has the right to Add and/or Edit other user accounts. If only one of such a
  110. // user remains, that user must be the current user, and can not be deleted, as it would lock the user out of the BE environment, left to fix
  111. // it in XML or SQL files / commands. See issue 11990
  112. bool adminsExist = false;
  113. MembershipUserCollection users = Membership.GetAllUsers();
  114. foreach (MembershipUser user in users)
  115. {
  116. string[] roles = Roles.GetRolesForUser(user.UserName);
  117. // look for admins other than 'id'
  118. if (!id.Equals(user.UserName, StringComparison.OrdinalIgnoreCase) && (Right.HasRight(Rights.EditOtherUsers, roles) || Right.HasRight(Rights.CreateNewUsers, roles)))
  119. {
  120. adminsExist = true;
  121. break;
  122. }
  123. }
  124. if (!adminsExist)
  125. {
  126. return new JsonResponse() { Message = Resources.labels.cannotDeleteLastAdmin };
  127. }
  128. string[] userRoles = Roles.GetRolesForUser(id);
  129. try
  130. {
  131. if (userRoles.Length > 0)
  132. {
  133. Roles.RemoveUsersFromRoles(new string[] { id }, userRoles);
  134. }
  135. Membership.DeleteUser(id);
  136. }
  137. catch (Exception ex)
  138. {
  139. Utils.Log(string.Format("Users.Delete : {0}", ex.Message));
  140. this.response.Success = false;
  141. this.response.Message = string.Format(Resources.labels.couldNotDeleteUser, id);
  142. return this.response;
  143. }
  144. this.response.Success = true;
  145. this.response.Message = string.Format(Resources.labels.userHasBeenDeleted, id);
  146. return this.response;
  147. }
  148. /// <summary>
  149. /// Edits the specified id.
  150. /// </summary>
  151. /// <param name="id">The user id.</param>
  152. /// <param name="bg">The background.</param>
  153. /// <param name="vals">The values.</param>
  154. /// <returns>JSON Response</returns>
  155. [WebMethod]
  156. public JsonResponse Edit(string id, string bg, string[] vals)
  157. {
  158. try
  159. {
  160. this.response.Success = false;
  161. bool isSelf = id.Equals(Security.CurrentUser.Identity.Name, StringComparison.OrdinalIgnoreCase);
  162. if (string.IsNullOrEmpty(vals[0]))
  163. {
  164. this.response.Message = Resources.labels.emailIsRequired;
  165. return this.response;
  166. }
  167. if (
  168. Membership.GetAllUsers().Cast<MembershipUser>().Any(
  169. u => u.Email.ToLowerInvariant() == vals[0].ToLowerInvariant()))
  170. {
  171. this.response.Message = Resources.labels.userWithEmailExists;
  172. return this.response;
  173. }
  174. if (isSelf && !Security.IsAuthorizedTo(Rights.EditOwnUser))
  175. {
  176. this.response.Message = Resources.labels.notAuthorized;
  177. return this.response;
  178. }
  179. else if (!isSelf && !Security.IsAuthorizedTo(Rights.EditOtherUsers))
  180. {
  181. this.response.Message = Resources.labels.notAuthorized;
  182. return this.response;
  183. }
  184. var usr = Membership.GetUser(id);
  185. if (usr != null)
  186. {
  187. usr.Email = vals[0];
  188. Membership.UpdateUser(usr);
  189. }
  190. this.response.Success = true;
  191. this.response.Message = string.Format(Resources.labels.userUpdated, id);
  192. return this.response;
  193. }
  194. catch (Exception ex)
  195. {
  196. Utils.Log(string.Format("UserService.Update: {0}", ex.Message));
  197. this.response.Message = string.Format(Resources.labels.couldNotUpdateUser, id);
  198. return this.response;
  199. }
  200. }
  201. #endregion
  202. }
  203. }