internal ApiResult SaveSystemUser(SystemUser systemUser) { ApiResult apiResult = new ApiResult(); try { if (!systemUser.IsValid()) { apiResult.SetFailuresAsStatusInResponseFields(systemUser.StatusDesc); return(apiResult); } SystemUser old = SystemUser.QueryWithStoredProc("GetSystemUserByID", systemUser.Username).FirstOrDefault(); systemUser.Id = old != null ? old.Id : systemUser.Id; systemUser.Save(); apiResult.SetSuccessAsStatusInResponseFields(); } catch (Exception ex) { HandleError(nameof(AttachSystemAffectedToChangeRequest), "EXCEPTION", ex.Message); apiResult.SetFailuresAsStatusInResponseFields(ex.Message); } return(apiResult); }
internal ApiResult SendOneTimePIN(string username, string MethodOfSending) { ApiResult apiResult = new ApiResult(); try { //Thread.Sleep(new TimeSpan(0, 1, 0)); if (string.IsNullOrEmpty(username)) { apiResult.StatusCode = Globals.FAILURE_STATUS_CODE; apiResult.StatusDesc = $"Please Supply a Username"; return(apiResult); } if (!Globals.AcceptableMethodsOfSendingOTP.Contains(MethodOfSending.ToUpper())) { apiResult.StatusCode = Globals.FAILURE_STATUS_CODE; apiResult.StatusDesc = $"Please Specify how you want to recieve the OTP"; return(apiResult); } SystemUser[] systemUsers = SystemUser.QueryWithStoredProc("GetSystemUserByID", username); if (systemUsers.Count() <= 0) { apiResult.StatusCode = Globals.FAILURE_STATUS_CODE; apiResult.StatusDesc = $"User with Username [{username}] doesnt exist"; return(apiResult); } SystemUser user = systemUsers[0]; OneTimePassword oneTimePassword = new OneTimePassword(); oneTimePassword.CompanyCode = user.CompanyCode; oneTimePassword.Password = "******"; oneTimePassword.ValidityDurationInSeconds = 5 * 60; oneTimePassword.Username = user.Username; oneTimePassword.Save(); ApiResult sendResult = MethodOfSending.ToUpper() == "PHONE" ? NotificationsHandler.SendOneTimePINByPhone(user.PhoneNumber, oneTimePassword.Password) : NotificationsHandler.SendOneTimePINByEmail(user.Email, oneTimePassword.Password); if (sendResult.StatusCode != Globals.SUCCESS_STATUS_CODE) { apiResult.StatusCode = Globals.FAILURE_STATUS_CODE; apiResult.StatusDesc = "Send One Time PIN failed: " + sendResult.StatusDesc; return(apiResult); } apiResult.StatusCode = Globals.SUCCESS_STATUS_CODE; apiResult.StatusDesc = $"Successfully Sent One time Password by {MethodOfSending} to {sendResult.PegPayID}. Its Valid for {oneTimePassword.ValidityDurationInSeconds / 60} minute(s)"; return(apiResult); } catch (Exception ex) { apiResult = HandleException(nameof(SendOneTimePIN), $"{username}, Error:{ex.Message}", ex); } return(apiResult); }
internal static ApiResult SendTbarApprovedEmail(TimeBoundAccessRequest tbar) { ApiResult result = new ApiResult(); try { SystemUser user = SystemUser.QueryWithStoredProc("GetSystemUserById", tbar.UserId).FirstOrDefault(); if (user == null) { result.SetFailuresAsStatusInResponseFields($"USER WITH ID {tbar.UserId} NOT FOUND"); return(result); } MailApi.Email mail = new MailApi.Email(); MailApi.EmailAddress addr = new MailApi.EmailAddress(); addr.Address = user.Email; addr.Name = tbar.UserId; addr.AddressType = MailApi.EmailAddressType.To; MailApi.EmailAddress[] addresses = { addr }; mail.MailAddresses = addresses; mail.From = Globals.MAIL_FROM; string dateFormat = Globals.DATE_FORMAT; mail.Message = $"Hi,\n<br/>" + $"Congrajulations. Your Change Request has been APPROVED!!\n<br/>\n<br/>" + $"ID: {tbar.TBPAccessId}\n<br/>" + $"System Name: {tbar.SystemCode}\n<br/>" + $"Duration In Minutes: {tbar.DurationInMinutes}\n<br/>" + $"Reason: {tbar.Reason}\n<br/>" + $"Date Of CR: {tbar.CreatedOn}\n<br/>" + $"Reason : {tbar.Reason}\n<br/>" + $"You can login at {tbar.StartTime.ToString(dateFormat)} and Use Tbar\n<br/>" + $"\n<br/>Thank you.\n<br/>" + $"Pegasus Change Request System.\n<br/>"; mail.Subject = "T.B.A.R REJECTED"; MailApi.MessengerSoapClient mapi = new MailApi.MessengerSoapClient(); MailApi.Result resp = mapi.PostEmail(mail); if (resp.StatusCode != "0") { result.StatusCode = Globals.FAILURE_STATUS_CODE; result.StatusDesc = resp.StatusDesc; return(result); } result.StatusCode = Globals.SUCCESS_STATUS_CODE; result.StatusDesc = Globals.SUCCESS_STATUS_TEXT; } catch (Exception ex) { result = HandleException(nameof(SendCrRejectedEmail), $"{tbar.TBPAccessId}", ex); } return(result); }
internal SystemUser Login(string username, string password) { SystemUser user = new SystemUser(); try { if (string.IsNullOrEmpty(username)) { user.StatusCode = Globals.FAILURE_STATUS_CODE; user.StatusDesc = $"Please Supply a Username"; return(user); } if (string.IsNullOrEmpty(password)) { user.StatusCode = Globals.FAILURE_STATUS_CODE; user.StatusDesc = $"Please Supply a password"; return(user); } SystemUser[] systemUsers = SystemUser.QueryWithStoredProc("GetSystemUserByID", username); if (systemUsers.Count() <= 0) { user.StatusCode = Globals.FAILURE_STATUS_CODE; user.StatusDesc = $"Invalid username supplied"; return(user); } user = systemUsers[0]; //IEnumerable OneTimePassword[] oneTimePasswords = OneTimePassword.QueryWithStoredProc("GetLatestOTP", username, password); if (oneTimePasswords.Count() <= 0) { user.StatusCode = Globals.FAILURE_STATUS_CODE; user.StatusDesc = $"No OTP found for {username}"; return(user); } OneTimePassword oneTimePassword = oneTimePasswords[0]; if (oneTimePassword.Password != password) { user.StatusCode = Globals.FAILURE_STATUS_CODE; user.StatusDesc = $"Invalid Password Supplied"; return(user); } user.StatusCode = Globals.SUCCESS_STATUS_CODE; user.StatusDesc = Globals.SUCCESS_STATUS_TEXT; } catch (Exception ex) { Task.Factory.StartNew(() => { DatabaseHandler dh = new DatabaseHandler(); dh.LogError(nameof(SendOneTimePIN), $"{username}, Error:{ex.Message}"); }); user.StatusCode = Globals.FAILURE_STATUS_CODE; user.StatusDesc = $"EXCEPTION: {ex.Message}"; } return(user); }
public static ApiResult SendApproveCrEmail(ApproverForChangeRequest link) { ApiResult result = new ApiResult(); try { SystemUser approver = SystemUser.QueryWithStoredProc("GetSystemUserById", link.ApproverId).FirstOrDefault(); if (approver == null) { result.StatusCode = Globals.FAILURE_STATUS_CODE; result.StatusDesc = $"USER WITH THE APPROVER ID {link.ApproverId} WAS NOT FOUND"; return(result); } ChangeRequest changeRequest = ChangeRequest.QueryWithStoredProc("GetChangeRequestById", link.ChangeRequestId).FirstOrDefault(); if (changeRequest == null) { result.StatusCode = Globals.FAILURE_STATUS_CODE; result.StatusDesc = $"CHANGE REQUEST WITH THE ID {link.ChangeRequestId} WAS NOT FOUND"; return(result); } string filePath = SystemSetting.QueryWithStoredProc("GetSystemSettingById", Globals.FILE_PATH_TO_APPROVE_CR_EMAIL_TEMPLATE).FirstOrDefault()?.SettingValue; if (filePath == null) { result.StatusCode = Globals.FAILURE_STATUS_CODE; result.StatusDesc = $"{nameof(Globals.FILE_PATH_TO_APPROVE_CR_EMAIL_TEMPLATE)} NOT FOUND"; return(result); } string ApproveURL = SystemSetting.QueryWithStoredProc("GetSystemSettingById", Globals.APPROVE_CR_URL).FirstOrDefault()?.SettingValue; if (ApproveURL == null) { result.StatusCode = Globals.FAILURE_STATUS_CODE; result.StatusDesc = $"{nameof(Globals.APPROVE_CR_URL)} NOT FOUND"; return(result); } string msg = File.ReadAllText(filePath); string dateFormat = "yyyy-MM-dd HH:mm"; msg = msg.Replace("[APPROVER_NAME]", approver.Username); msg = msg.Replace("[TITLE]", changeRequest.Title); msg = msg.Replace("[CURRENT_PROBLEM]", changeRequest.Problem); msg = msg.Replace("[PROPOSED_SOLUTION]", changeRequest.Solution); msg = msg.Replace("[CHANGE_CATEGORY]", changeRequest.ChangeCategoryId); msg = msg.Replace("[JUSTIFICATION]", changeRequest.Justification); msg = msg.Replace("[IMPACT]", changeRequest.ImpactOfNotImplementing); msg = msg.Replace("[REQUESTOR_NAME]", changeRequest.RequesterName); msg = msg.Replace("[REQUESTOR_EMAIL]", changeRequest.RequesterEmail); msg = msg.Replace("[REQUESTOR_PHONE]", changeRequest.RequesterPhone); msg = msg.Replace("[REQUESTOR_COMPANY]", changeRequest.RequesterCompany); msg = msg.Replace("[IMPLEMENTER_NAME]", changeRequest.ImplementerName); msg = msg.Replace("[IMPLEMENTER_EMAIL]", changeRequest.ImplementerEmail); msg = msg.Replace("[IMPLEMENTER_PHONE]", changeRequest.ImplementerPhone); msg = msg.Replace("[START_DATE]", changeRequest.ChangeStartDateTime.ToString(dateFormat)); msg = msg.Replace("[END_DATE]", changeRequest.ChangeEndDateTime.ToString(dateFormat)); msg = msg.Replace("[APPROVE_URL]", ApproveURL); msg = msg.Replace("[USER_ID]", link.ApproverId); msg = msg.Replace("[CR_ID]", changeRequest.ChangeRequestId); MailMessage mail = new MailMessage(Globals.FROM_EMAIL, approver.Email); SmtpClient client = new SmtpClient(); client.Port = 587; client.Host = Globals.SMPTP_SERVER; client.EnableSsl = true; client.Timeout = 10000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(Globals.SMTP_USERNAME, Globals.SMTP_PASSWORD); mail.Subject = Globals.MAIL_SUBJECT_APPROVE_CR_EMAIL; mail.Body = msg; mail.IsBodyHtml = true; client.Send(mail); result.StatusCode = Globals.SUCCESS_STATUS_CODE; result.StatusDesc = Globals.SUCCESS_STATUS_TEXT; } catch (Exception ex) { result.StatusCode = Globals.FAILURE_STATUS_CODE; result.StatusDesc = "EXCEPTION: " + ex.Message; return(result); } return(result); }
public static ApiResult SendApproveTbarEmail(TimeBoundAccessRequest tbar) { ApiResult result = new ApiResult(); try { SystemUser approver = SystemUser.QueryWithStoredProc("GetSystemUserById", tbar.Approver).FirstOrDefault(); if (approver == null) { result.StatusCode = Globals.FAILURE_STATUS_CODE; result.StatusDesc = $"USER WITH THE APPROVER ID {tbar.Approver} WAS NOT FOUND"; return(result); } string filePath = SystemSetting.QueryWithStoredProc("GetSystemSettingById", Globals.FILE_PATH_TO_APPROVE_TBAR_EMAIL_TEMPLATE).FirstOrDefault()?.SettingValue; if (filePath == null) { result.StatusCode = Globals.FAILURE_STATUS_CODE; result.StatusDesc = $"{nameof(Globals.FILE_PATH_TO_APPROVE_TBAR_EMAIL_TEMPLATE)} NOT FOUND"; return(result); } string ApproveURL = SystemSetting.QueryWithStoredProc("GetSystemSettingById", Globals.APPROVE_TBAR_URL).FirstOrDefault()?.SettingValue; if (ApproveURL == null) { result.StatusCode = Globals.FAILURE_STATUS_CODE; result.StatusDesc = $"{nameof(Globals.APPROVE_TBAR_URL)} NOT FOUND"; return(result); } string msg = File.ReadAllText(filePath); string dateFormat = "yyyy-MM-dd HH:mm"; msg = msg.Replace("[APPROVER_NAME]", approver.Username); msg = msg.Replace("[REQUESTOR_NAME]", tbar.UserId); msg = msg.Replace("[SYSTEM_NAME]", tbar.SystemCode); msg = msg.Replace("[TYPE_OF_ACCESS]", tbar.TypeOfAccess); msg = msg.Replace("[START_DATE]", tbar.StartTime.ToString(dateFormat)); msg = msg.Replace("[DURATION]", tbar.DurationInMinutes.ToString()); msg = msg.Replace("[REASON]", tbar.Reason); msg = msg.Replace("[APPROVE_URL]", ApproveURL); msg = msg.Replace("[USER_ID]", approver.Username); msg = msg.Replace("[TBAR_ID]", tbar.TBPAccessId); MailMessage mail = new MailMessage(Globals.FROM_EMAIL, approver.Email); SmtpClient client = new SmtpClient(); client.Port = 587; client.Host = Globals.SMPTP_SERVER; client.EnableSsl = true; client.Timeout = 10000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(Globals.SMTP_USERNAME, Globals.SMTP_PASSWORD); mail.Subject = Globals.MAIL_SUBJECT_APPROVE_TBAR_EMAIL; mail.Body = msg; mail.IsBodyHtml = true; client.Send(mail); result.StatusCode = Globals.SUCCESS_STATUS_CODE; result.StatusDesc = Globals.SUCCESS_STATUS_TEXT; } catch (Exception ex) { result.StatusCode = Globals.FAILURE_STATUS_CODE; result.StatusDesc = "EXCEPTION: " + ex.Message; return(result); } return(result); }