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

/CMSModules/Membership/Pages/Users/User_Edit_Password.aspx.cs

https://bitbucket.org/kudutest2/kenticogit
C# | 462 lines | 326 code | 76 blank | 60 comment | 66 complexity | 1603dedd18751ad9cffe7915665419c6 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using CMS.SiteProvider;
  12. using CMS.GlobalHelper;
  13. using CMS.CMSHelper;
  14. using CMS.EmailEngine;
  15. using CMS.SettingsProvider;
  16. using CMS.EventLog;
  17. using CMS.UIControls;
  18. public partial class CMSModules_Membership_Pages_Users_User_Edit_Password : CMSUsersPage
  19. {
  20. const string hiddenPassword = "********";
  21. #region "Private fields"
  22. private int mUserID = 0;
  23. private UserInfo ui = null;
  24. #endregion
  25. #region "Private properties"
  26. /// <summary>
  27. /// Current user ID.
  28. /// </summary>
  29. private int UserID
  30. {
  31. get
  32. {
  33. if (this.mUserID == 0)
  34. {
  35. this.mUserID = QueryHelper.GetInteger("userid", 0);
  36. }
  37. return this.mUserID;
  38. }
  39. }
  40. #endregion
  41. protected void Page_Load(object sender, EventArgs e)
  42. {
  43. ButtonSetPassword.Text = GetString("Administration-User_Edit_Password.SetPassword");
  44. LabelPassword.Text = GetString("Administration-User_Edit_Password.NewPassword");
  45. LabelConfirmPassword.Text = GetString("Administration-User_Edit_Password.ConfirmPassword");
  46. this.chkSendEmail.Text = GetString("Administration-User_Edit_Password.SendEmail");
  47. this.btnGenerateNew.Text = GetString("Administration-User_Edit_Password.gennew");
  48. this.btnSendPswd.Text = GetString("Administration-User_Edit_Password.sendpswd");
  49. imgGenPassword.ImageUrl = GetImageUrl("Objects/CMS_User/passwordgenerate.png");
  50. imgSendPassword.ImageUrl = GetImageUrl("Objects/CMS_User/passwordsend.png");
  51. if (!RequestHelper.IsPostBack())
  52. {
  53. if (this.UserID > 0)
  54. {
  55. // Check that only global administrator can edit global administrator's accouns
  56. ui = UserInfoProvider.GetUserInfo(UserID);
  57. EditedObject = ui;
  58. CheckUserAvaibleOnSite(ui);
  59. if (!CheckGlobalAdminEdit(ui))
  60. {
  61. plcTable.Visible = false;
  62. lblError.Text = GetString("Administration-User_List.ErrorGlobalAdmin");
  63. lblError.Visible = true;
  64. return;
  65. }
  66. if (ui != null)
  67. {
  68. if (ui.GetValue("UserPassword") != null)
  69. {
  70. string password = ui.GetValue("UserPassword").ToString();
  71. if (password.Length > 0)
  72. {
  73. passStrength.TextBoxAttributes.Add("value", hiddenPassword);
  74. TextBoxConfirmPassword.Attributes.Add("value", hiddenPassword);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. // Handle 'Send password' button
  81. DisplaySendPaswd();
  82. HandleGeneratePassword();
  83. }
  84. /// <summary>
  85. /// Check whether current user is allowed to modify another user. Return "" or error message.
  86. /// </summary>
  87. /// <param name="userId">Modified user</param>
  88. protected string ValidateGlobalAndDeskAdmin()
  89. {
  90. string result = String.Empty;
  91. if (CMSContext.CurrentUser.IsGlobalAdministrator)
  92. {
  93. return result;
  94. }
  95. UserInfo userInfo = UserInfoProvider.GetUserInfo(this.UserID);
  96. if (userInfo == null)
  97. {
  98. result = GetString("Administration-User.WrongUserId");
  99. }
  100. else
  101. {
  102. if (userInfo.IsGlobalAdministrator)
  103. {
  104. result = GetString("Administration-User.NotAllowedToModify");
  105. }
  106. }
  107. return result;
  108. }
  109. #region "Event handlers"
  110. /// <summary>
  111. /// Generates new password and sends it to the user.
  112. /// </summary>
  113. protected void btnGenerateNew_Click(object sender, EventArgs e)
  114. {
  115. // Check modify permission
  116. CheckModifyPermissions();
  117. string result = ValidateGlobalAndDeskAdmin();
  118. if (result == String.Empty)
  119. {
  120. string pswd = UserInfoProvider.GenerateNewPassword(CMSContext.CurrentSiteName);
  121. string userName = UserInfoProvider.GetUserNameById(this.UserID);
  122. UserInfoProvider.SetPassword(userName, pswd);
  123. // Show actual information to the user
  124. if (passStrength.Text != String.Empty)
  125. {
  126. passStrength.TextBoxAttributes.Add("value", hiddenPassword);
  127. TextBoxConfirmPassword.Attributes.Add("value", hiddenPassword);
  128. }
  129. else
  130. {
  131. passStrength.TextBoxAttributes.Add("value", "");
  132. TextBoxConfirmPassword.Attributes.Add("value", "");
  133. }
  134. lblInfo.Visible = true;
  135. lblInfo.Text = GetString("General.ChangesSaved");
  136. // Process e-mail sending
  137. SendEmail(GetString("Administration-User_Edit_Password.NewGen"), pswd, this.UserID, "changed", true);
  138. ReloadPassword();
  139. }
  140. if (result != String.Empty)
  141. {
  142. lblError.Visible = true;
  143. lblError.Text = result;
  144. }
  145. }
  146. /// <summary>
  147. /// Sends the actual password of the current user.
  148. /// </summary>
  149. protected void btnSendPswd_Click(object sender, EventArgs e)
  150. {
  151. // Check permissions
  152. CheckModifyPermissions();
  153. string result = ValidateGlobalAndDeskAdmin();
  154. if (result == String.Empty)
  155. {
  156. string pswd = UserInfoProvider.GetUserInfo(this.UserID).GetValue("UserPassword").ToString();
  157. // Process e-mail sending
  158. SendEmail(GetString("Administration-User_Edit_Password.Resend"), pswd, this.UserID, "RESEND", false);
  159. }
  160. if (result != String.Empty)
  161. {
  162. lblError.Visible = true;
  163. lblError.Text = result;
  164. }
  165. }
  166. /// <summary>
  167. /// Sets password of current user.
  168. /// </summary>
  169. protected void ButtonSetPassword_Click(object sender, EventArgs e)
  170. {
  171. // Check modify permission
  172. CheckModifyPermissions();
  173. string result = ValidateGlobalAndDeskAdmin();
  174. if ((result == String.Empty) && (ui != null))
  175. {
  176. if (TextBoxConfirmPassword.Text == passStrength.Text)
  177. {
  178. if (passStrength.IsValid())
  179. {
  180. if (passStrength.Text != hiddenPassword) //password has been changed
  181. {
  182. string pswd = this.passStrength.Text;
  183. UserInfoProvider.SetPassword(ui, passStrength.Text);
  184. // Show actual information to the user
  185. if (passStrength.Text != String.Empty)
  186. {
  187. passStrength.TextBoxAttributes.Add("value", hiddenPassword);
  188. TextBoxConfirmPassword.Attributes.Add("value", hiddenPassword);
  189. }
  190. else
  191. {
  192. passStrength.TextBoxAttributes.Add("value", "");
  193. TextBoxConfirmPassword.Attributes.Add("value", "");
  194. }
  195. lblInfo.Visible = true;
  196. lblInfo.Text = GetString("General.ChangesSaved");
  197. if (this.chkSendEmail.Checked)
  198. {
  199. // Process e-mail sending
  200. SendEmail(GetString("Administration-User_Edit_Password.Changed"), pswd, this.UserID, "CHANGED", false);
  201. }
  202. }
  203. }
  204. else
  205. {
  206. result = UserInfoProvider.GetPolicyViolationMessage(CMSContext.CurrentSiteName);
  207. }
  208. }
  209. else
  210. {
  211. result = GetString("Administration-User_Edit_Password.PasswordsDoNotMatch");
  212. }
  213. }
  214. if (result != String.Empty)
  215. {
  216. lblError.Visible = true;
  217. lblError.Text = result;
  218. }
  219. }
  220. #endregion
  221. #region "Private methods"
  222. /// <summary>
  223. /// Loads the user password to the password fields.
  224. /// </summary>
  225. private void ReloadPassword()
  226. {
  227. UserInfo ui = UserInfoProvider.GetUserInfo(this.UserID);
  228. if (ui != null)
  229. {
  230. string passwd = ui.GetValue("UserPassword").ToString();
  231. if (!string.IsNullOrEmpty(passwd))
  232. {
  233. this.passStrength.TextBoxAttributes.Add("value", hiddenPassword);
  234. this.TextBoxConfirmPassword.Attributes.Add("value", hiddenPassword);
  235. }
  236. }
  237. }
  238. /// <summary>
  239. /// Sends e-mail with password if required.
  240. /// </summary>
  241. /// <param name="pswd">Password to send</param>
  242. /// <param name="toEmail">E-mail address of the mail recepient</param>
  243. /// <param name="subject">Subject of the e-mail sent</param>
  244. /// <param name="emailType">Type of the e-mail specificating the template used (NEW, CHANGED, RESEND)</param>
  245. /// <param name="showPassword">Indicates whether password is shown in message.</param>
  246. private void SendEmail(string subject, string pswd, int userId, string emailType, bool showPassword)
  247. {
  248. // Check whether the 'From' elemtn was specified
  249. string emailFrom = SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSSendPasswordEmailsFrom");
  250. bool fromMissing = string.IsNullOrEmpty(emailFrom);
  251. if ((!string.IsNullOrEmpty(emailType)) && (ui != null) && (!fromMissing))
  252. {
  253. if (!string.IsNullOrEmpty(ui.Email))
  254. {
  255. EmailMessage em = new EmailMessage();
  256. em.From = emailFrom;
  257. em.Recipients = ui.Email;
  258. em.Subject = subject;
  259. em.EmailFormat = EmailFormatEnum.Default;
  260. string templateName = null;
  261. // Get e-mail template name
  262. switch (emailType.ToLower())
  263. {
  264. case "new":
  265. templateName = "Membership.NewPassword";
  266. break;
  267. case "changed":
  268. templateName = "Membership.ChangedPassword";
  269. break;
  270. case "resend":
  271. templateName = "Membership.ResendPassword";
  272. break;
  273. default:
  274. break;
  275. }
  276. // Get template info object
  277. if (templateName != null)
  278. {
  279. try
  280. {
  281. // Get e-mail template
  282. EmailTemplateInfo template = EmailTemplateProvider.GetEmailTemplate(templateName, null);
  283. if (template != null)
  284. {
  285. em.Body = template.TemplateText;
  286. // Macros
  287. string[,] macros = new string[2, 2];
  288. macros[0, 0] = "UserName";
  289. macros[0, 1] = ui.UserName;
  290. macros[1, 0] = "Password";
  291. macros[1, 1] = pswd;
  292. // Create macro resolver
  293. ContextResolver resolver = CMSContext.CurrentResolver;
  294. resolver.SourceParameters = macros;
  295. // Add template attachments
  296. MetaFileInfoProvider.ResolveMetaFileImages(em, template.TemplateID, EmailObjectType.EMAILTEMPLATE, MetaFileInfoProvider.OBJECT_CATEGORY_TEMPLATE);
  297. // Send message immediately (+ resolve macros)
  298. EmailSender.SendEmailWithTemplateText(CMSContext.CurrentSiteName, em, template, resolver, true);
  299. // Inform on success
  300. this.lblInfo.Text += " " + GetString("Administration-User_Edit_Password.PasswordsSent") + " " + HTMLHelper.HTMLEncode(ui.Email);
  301. this.lblInfo.Visible = true;
  302. return;
  303. }
  304. }
  305. catch (Exception ex)
  306. {
  307. // Log the error to the event log
  308. EventLogProvider eventLog = new EventLogProvider();
  309. eventLog.LogEvent("Password retrieval", "USERPASSWORD", ex);
  310. this.lblError.Text = "Failed to send the password: " + ex.Message;
  311. }
  312. }
  313. }
  314. else
  315. {
  316. // Inform on error
  317. this.lblInfo.Visible = true;
  318. if (showPassword)
  319. {
  320. this.lblInfo.Text = string.Format(GetString("Administration-User_Edit_Password.passshow"), pswd);
  321. }
  322. else
  323. {
  324. this.lblInfo.Text = GetString("Administration-User_Edit_Password.PassChangedNotSent");
  325. }
  326. return;
  327. }
  328. }
  329. // Inform on error
  330. this.lblError.Visible = true;
  331. this.lblError.Text = GetString("Administration-User_Edit_Password.PasswordsNotSent");
  332. if (fromMissing)
  333. {
  334. this.lblError.Text += GetString("Administration-User_Edit_Password.FromMissing") + " ";
  335. }
  336. }
  337. /// <summary>
  338. /// Decides whether the 'Send password' button should be enabled or not.
  339. /// </summary>
  340. private void DisplaySendPaswd()
  341. {
  342. if (ui == null)
  343. {
  344. ui = UserInfoProvider.GetUserInfo(this.UserID);
  345. }
  346. if (ui != null)
  347. {
  348. // Password is stored in plain text, allow sending
  349. if (string.IsNullOrEmpty(ui.UserPasswordFormat) && !string.IsNullOrEmpty(ui.Email))
  350. {
  351. return;
  352. }
  353. }
  354. this.btnSendPswd.Visible = false;
  355. imgSendPassword.Visible = false;
  356. }
  357. /// <summary>
  358. /// Decides whether enable genererate new password e-mail.
  359. /// </summary>
  360. private void HandleGeneratePassword()
  361. {
  362. if (ui == null)
  363. {
  364. ui = UserInfoProvider.GetUserInfo(this.UserID);
  365. }
  366. if (ui != null)
  367. {
  368. if (string.IsNullOrEmpty(ui.Email))
  369. {
  370. btnGenerateNew.OnClientClick = "return confirm('" + GetString("user.showpasswarning") + "');";
  371. }
  372. }
  373. }
  374. /// <summary>
  375. /// Checks if the user is alloed to perform this action.
  376. /// </summary>
  377. private void CheckModifyPermissions()
  378. {
  379. // Check "modify" permission
  380. if (!CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.Users", "Modify"))
  381. {
  382. RedirectToAccessDenied("CMS.Users", "Modify");
  383. }
  384. }
  385. #endregion
  386. }