You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.6 KiB

  1. using System;
  2. using System.ComponentModel;
  3. using System.Data;
  4. using Swift.DAL.BL.System.UserManagement;
  5. using Swift.DAL.SwiftDAL;
  6. using Swift.web.Library;
  7. namespace Swift.web.SwiftSystem.UserManagement.AdminUserSetup
  8. {
  9. public partial class ResetPassword : System.Web.UI.Page
  10. {
  11. private const string ViewFunctionId = "10101370";
  12. private readonly ApplicationUserDao _obj = new ApplicationUserDao();
  13. private readonly SwiftLibrary _sl = new SwiftLibrary();
  14. private SmtpMailSetting smtpMailSetting = new SmtpMailSetting();
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. PopulateUserName();
  18. if (!IsPostBack)
  19. {
  20. Authenticate();
  21. //LoadTab();
  22. }
  23. }
  24. //private void LoadTab()
  25. //{
  26. // pnlBreadCrumb.Visible = true;
  27. //}
  28. protected void btnReset_Click(object sender, EventArgs e)
  29. {
  30. Update();
  31. }
  32. private void Authenticate()
  33. {
  34. _sl.CheckAuthentication(ViewFunctionId);
  35. btnReset.Visible = _sl.HasRight(ViewFunctionId);
  36. }
  37. protected string GetUserName()
  38. {
  39. return GetStatic.ReadQueryString("userName", "");
  40. }
  41. protected void PopulateUserName()
  42. {
  43. userName.Text = GetUserName();
  44. userName.Enabled = false;
  45. }
  46. private void Update()
  47. {
  48. DbResult dbResult = _obj.ResetPassword(GetUserName(), pwd.Text, GetStatic.GetUser());
  49. ManageMessage(dbResult);
  50. }
  51. private void ManageMessage(DbResult dbResult)
  52. {
  53. GetStatic.SetMessage(dbResult);
  54. if (dbResult.ErrorCode == "0")
  55. {
  56. SetupEmailSetting();
  57. SendMail();
  58. Response.Redirect("List.aspx");
  59. }
  60. else
  61. {
  62. GetStatic.PrintMessage(Page);
  63. }
  64. }
  65. private delegate void DoStuff(); //delegate for the action
  66. private void SendMail()
  67. {
  68. var myAction = new DoStuff(AsyncMailProcessing);
  69. //invoke it asynchrnously, control passes to next statement
  70. myAction.BeginInvoke(null, null);
  71. }
  72. private void AsyncMailProcessing()
  73. {
  74. var bw = new BackgroundWorker();
  75. // this allows our worker to report progress during work
  76. bw.WorkerReportsProgress = true;
  77. // what to do in the background thread
  78. bw.DoWork += new DoWorkEventHandler(
  79. delegate(object o, DoWorkEventArgs args)
  80. {
  81. var b = o as BackgroundWorker;
  82. smtpMailSetting.SendSmtpMail(smtpMailSetting);
  83. });
  84. // what to do when progress changed (update the progress bar for example)
  85. bw.ProgressChanged += new ProgressChangedEventHandler(
  86. delegate(object o, ProgressChangedEventArgs args)
  87. {
  88. //label1.Text = string.Format("{0}% Completed", args.ProgressPercentage);
  89. });
  90. // what to do when worker completes its task (notify the user)
  91. bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
  92. delegate(object o, RunWorkerCompletedEventArgs args)
  93. {
  94. var dbResult = new DbResult();
  95. dbResult.SetError("0", "Mail Sent Successfully", "");
  96. GetStatic.PrintMessage(Page, dbResult);
  97. });
  98. bw.RunWorkerAsync();
  99. }
  100. private void SetupEmailSetting()
  101. {
  102. //var obj = new TranViewDao();
  103. //var ds = obj.GetEmailFormat(GetStatic.GetUser(), "PwdReset", userName.Text, "", "");
  104. //if (ds == null)
  105. // return;
  106. //if (ds.Tables.Count == 0)
  107. // return;
  108. //if (ds.Tables.Count > 1)
  109. //{
  110. // //Email Server Settings
  111. // if (ds.Tables[0].Rows.Count > 0)
  112. // {
  113. // var dr1 = ds.Tables[0].Rows[0];
  114. // smtpMailSetting.SmtpServer = dr1["smtpServer"].ToString();
  115. // smtpMailSetting.SmtpPort = Convert.ToInt32(dr1["smtpPort"]);
  116. // smtpMailSetting.SendEmailId = dr1["sendID"].ToString();
  117. // smtpMailSetting.SendEmailPwd = dr1["sendPSW"].ToString();
  118. // }
  119. // if (ds.Tables[1].Rows.Count == 0)
  120. // return;
  121. // //Email Receiver
  122. // if (ds.Tables[1].Rows.Count > 0)
  123. // {
  124. // var dt = ds.Tables[1];
  125. // foreach (DataRow dr2 in dt.Rows)
  126. // {
  127. // if (!string.IsNullOrEmpty(smtpMailSetting.ToEmails))
  128. // smtpMailSetting.ToEmails = smtpMailSetting.ToEmails + ",";
  129. // smtpMailSetting.ToEmails = smtpMailSetting.ToEmails + dr2["email"].ToString();
  130. // }
  131. // }
  132. // //Email Subject and Body
  133. // if (ds.Tables[2].Rows.Count > 0)
  134. // {
  135. // var dr3 = ds.Tables[2].Rows[0];
  136. // if (dr3 == null)
  137. // return;
  138. // smtpMailSetting.MsgSubject = dr3[0].ToString();
  139. // smtpMailSetting.MsgBody = dr3[1].ToString();
  140. // }
  141. //}
  142. }
  143. protected void btnBack_Click(object sender, EventArgs e)
  144. {
  145. Response.Redirect("List.aspx");
  146. }
  147. }
  148. }