/web/studio/ASC.Web.Studio/Core/EmailOperationService.cs

https://gitlab.com/rekby-archive/onlyoffice-CommunityServer · C# · 236 lines · 178 code · 33 blank · 25 comment · 40 complexity · 7a595bbc94c2e23fee6850cc13f59181 MD5 · raw file

  1. /*
  2. *
  3. * (c) Copyright Ascensio System Limited 2010-2021
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. */
  16. using System;
  17. using System.Web;
  18. using AjaxPro;
  19. using ASC.Core;
  20. using ASC.Core.Users;
  21. using ASC.MessagingSystem;
  22. using ASC.Web.Studio.Core.Notify;
  23. using ASC.Web.Studio.Core.Users;
  24. using ASC.Web.Studio.PublicResources;
  25. namespace ASC.Web.Studio.Core
  26. {
  27. [AjaxNamespace("EmailOperationService")]
  28. public class EmailOperationService
  29. {
  30. public class InvalidEmailException : Exception
  31. {
  32. public InvalidEmailException()
  33. {
  34. }
  35. public InvalidEmailException(string message) : base(message)
  36. {
  37. }
  38. }
  39. public class AccessDeniedException : Exception
  40. {
  41. public AccessDeniedException()
  42. {
  43. }
  44. public AccessDeniedException(string message) : base(message)
  45. {
  46. }
  47. }
  48. public class UserNotFoundException : Exception
  49. {
  50. public UserNotFoundException()
  51. {
  52. }
  53. public UserNotFoundException(string message) : base(message)
  54. {
  55. }
  56. }
  57. public class InputException : Exception
  58. {
  59. public InputException()
  60. {
  61. }
  62. public InputException(string message) : base(message)
  63. {
  64. }
  65. }
  66. /// <summary>
  67. /// Sends the email activation instructions to the specified email
  68. /// </summary>
  69. /// <param name="userID">The ID of the user who should activate the email</param>
  70. /// <param name="email">Email</param>
  71. [AjaxMethod]
  72. public string SendEmailActivationInstructions(Guid userID, string email)
  73. {
  74. if (userID == Guid.Empty) throw new ArgumentNullException("userID");
  75. email = (email ?? "").Trim();
  76. if (String.IsNullOrEmpty(email)) throw new ArgumentNullException(Resource.ErrorEmailEmpty);
  77. if (!email.TestEmailRegex()) throw new InvalidEmailException(Resource.ErrorNotCorrectEmail);
  78. try
  79. {
  80. var viewer = CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID);
  81. var user = CoreContext.UserManager.GetUsers(userID);
  82. if (user == null) throw new UserNotFoundException(Resource.ErrorUserNotFound);
  83. if (viewer == null) throw new AccessDeniedException(Resource.ErrorAccessDenied);
  84. if (viewer.IsAdmin() || viewer.ID == user.ID)
  85. {
  86. var existentUser = CoreContext.UserManager.GetUserByEmail(email);
  87. if (existentUser.ID != ASC.Core.Users.Constants.LostUser.ID && existentUser.ID != userID)
  88. throw new InputException(CustomNamingPeople.Substitute<Resource>("ErrorEmailAlreadyExists"));
  89. user.Email = email;
  90. if (user.ActivationStatus == EmployeeActivationStatus.Activated)
  91. {
  92. user.ActivationStatus = EmployeeActivationStatus.NotActivated;
  93. }
  94. if (user.ActivationStatus == (EmployeeActivationStatus.AutoGenerated | EmployeeActivationStatus.Activated))
  95. {
  96. user.ActivationStatus = EmployeeActivationStatus.AutoGenerated;
  97. }
  98. CoreContext.UserManager.SaveUserInfo(user);
  99. }
  100. else
  101. {
  102. email = user.Email;
  103. }
  104. if (user.ActivationStatus == EmployeeActivationStatus.Pending && !user.IsLDAP())
  105. {
  106. if (user.IsVisitor())
  107. {
  108. StudioNotifyService.Instance.GuestInfoActivation(user);
  109. }
  110. else
  111. {
  112. StudioNotifyService.Instance.UserInfoActivation(user);
  113. }
  114. }
  115. else
  116. {
  117. StudioNotifyService.Instance.SendEmailActivationInstructions(user, email);
  118. }
  119. MessageService.Send(HttpContext.Current.Request, MessageAction.UserSentActivationInstructions, user.DisplayUserName(false));
  120. return String.Format(Resource.MessageEmailActivationInstuctionsSentOnEmail, "<b>" + email + "</b>");
  121. }
  122. catch (UserNotFoundException)
  123. {
  124. throw;
  125. }
  126. catch (AccessDeniedException)
  127. {
  128. throw;
  129. }
  130. catch (InputException)
  131. {
  132. throw;
  133. }
  134. catch (Exception)
  135. {
  136. throw new Exception(Resource.UnknownError);
  137. }
  138. }
  139. /// <summary>
  140. /// Sends the email change instructions to the specified email
  141. /// </summary>
  142. /// <param name="userID">The ID of the user who is changing the email</param>
  143. /// <param name="email">Email</param>
  144. [AjaxMethod]
  145. public string SendEmailChangeInstructions(Guid userID, string email)
  146. {
  147. if (userID == Guid.Empty) throw new ArgumentNullException("userID");
  148. email = (email ?? "").Trim();
  149. if (String.IsNullOrEmpty(email)) throw new Exception(Resource.ErrorEmailEmpty);
  150. if (!email.TestEmailRegex()) throw new Exception(Resource.ErrorNotCorrectEmail);
  151. try
  152. {
  153. var viewer = CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID);
  154. var user = CoreContext.UserManager.GetUsers(userID);
  155. if (user == null)
  156. throw new UserNotFoundException(Resource.ErrorUserNotFound);
  157. if (viewer == null || (user.IsOwner() && viewer.ID != user.ID))
  158. throw new AccessDeniedException(Resource.ErrorAccessDenied);
  159. var existentUser = CoreContext.UserManager.GetUserByEmail(email);
  160. if (existentUser.ID != ASC.Core.Users.Constants.LostUser.ID)
  161. throw new InputException(CustomNamingPeople.Substitute<Resource>("ErrorEmailAlreadyExists"));
  162. if (!viewer.IsAdmin())
  163. {
  164. StudioNotifyService.Instance.SendEmailChangeInstructions(user, email);
  165. }
  166. else
  167. {
  168. if (email == user.Email)
  169. throw new InputException(Resource.ErrorEmailsAreTheSame);
  170. user.Email = email;
  171. if (user.ActivationStatus.HasFlag(EmployeeActivationStatus.AutoGenerated))
  172. {
  173. user.ActivationStatus = EmployeeActivationStatus.AutoGenerated;
  174. }
  175. else
  176. {
  177. user.ActivationStatus = EmployeeActivationStatus.NotActivated;
  178. }
  179. CoreContext.UserManager.SaveUserInfo(user);
  180. StudioNotifyService.Instance.SendEmailActivationInstructions(user, email);
  181. }
  182. MessageService.Send(HttpContext.Current.Request, MessageAction.UserSentEmailChangeInstructions, user.DisplayUserName(false));
  183. return String.Format(Resource.MessageEmailChangeInstuctionsSentOnEmail, "<b>" + email + "</b>");
  184. }
  185. catch (AccessDeniedException)
  186. {
  187. throw;
  188. }
  189. catch (UserNotFoundException)
  190. {
  191. throw;
  192. }
  193. catch (InputException)
  194. {
  195. throw;
  196. }
  197. catch (Exception)
  198. {
  199. throw new Exception(Resource.UnknownError);
  200. }
  201. }
  202. }
  203. }