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.

190 lines
6.3 KiB

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