/// <summary> /// Sends a forgotten password token by email to the user for resetting the password /// </summary> /// <param name="username">The name of the user</param> public void ForgottenPassword(string username) { User u = null; try { u = this.FindByName(username); if (u == null) { throw this.manager.MessageHandler.GetError(ErrorCodes.USER_UNKNOWN); } string token = this.GeneratePasswordResetToken(u.Id); // HttpUtility.UrlEncode(MachineKeyEncryption.Encrypt(u.Id + "|" + this.GeneratePasswordResetToken(u.Id))); TextParser parser = new TextParser(this.manager); TextDefinition start = parser.ParseMessage("NotificationStart", new Dictionary <ReplaceableObjectKeys, object>() { { ReplaceableObjectKeys.User, u }, { ReplaceableObjectKeys.Code, token } }); TextDefinition end = parser.ParseMessage("NotificationEnd", null); TextDefinition td = parser.ParseMessage("PasswordReset", new Dictionary <ReplaceableObjectKeys, object>() { { ReplaceableObjectKeys.User, u }, { ReplaceableObjectKeys.Code, token } }); string text = start.Text + td.Text + end.Text; string html = start.Html + td.Html + end.Html; SmtpMailClient.SendMail(u.Email, "Password reset token", text, html); Logger.Audit(new Audit(Actions.FORGOT_PASSWORD, AuditEventType.READ, u)); } catch (Exception ex) { Logger.Audit(new Audit(Actions.FORGOT_PASSWORD, AuditEventType.READ, typeof(User), "UserName", username, false, ex.Message)); throw ex; } }
/// <summary> /// Sends registration token for the given username /// </summary> /// <param name="user">User to send the registration token</param> public void SendRegistrationToken(User user) { UserSecurityCode confirmationToken = null; do { confirmationToken = UserSecurityCode.CreateSecurityCode(user, "Registration"); } while (this.FindUserForRegistrationToken(confirmationToken.Code) != null); user.RegistrationConfirmationToken = confirmationToken.EncryptedCode; UserManagerExtensions.Update(this, user); TextParser parser = new TextParser(this.manager); TextDefinition td = parser.ParseMessage("RegistrationEmail", new Dictionary <Model.Messages.ReplaceableObjectKeys, object>() { { ReplaceableObjectKeys.User, user }, { ReplaceableObjectKeys.Code, confirmationToken.Code } }); IdentityResult result = new IdentityResult(); try { SmtpMailClient.SendMail(user.Email, "OPSMC RePLAY Registration", td.Text, td.Html); } catch (Exception ex) { // TODO add logger audit throw ex; } }
/// <summary> /// Sends an email message asynchronously /// Gets the subject and body from the MessageHandler as Text Definitions. The definitions requested are based upon the message.Subject and message.Body respectively /// </summary> /// <param name="message">The message to send</param> /// <returns>The async message</returns> public Task SendAsync(IdentityMessage message) { AccessHandlerManager manager = new AccessHandlerManager(); TextDefinition subject = manager.MessageHandler.GetTextDefinitionByCode(message.Subject); TextDefinition bodyFormat = manager.MessageHandler.GetTextDefinitionByCode(message.Body); SmtpMailClient.SendMail(message.Destination, subject == null ? message.Subject : subject.Text, bodyFormat == null ? message.Body : bodyFormat.Text, bodyFormat == null ? message.Body : bodyFormat.Html); return(Task.FromResult(0)); }
/// <summary> /// Notifies the given user of the token /// </summary> /// <param name="token">The token to send</param> /// <param name="manager">The manger that requested it</param> /// <param name="user">The user to send the token to</param> /// <returns>A task that is sending the token</returns> public override Task NotifyAsync(string token, Microsoft.AspNet.Identity.UserManager <Model.Users.User, string> manager, Model.Users.User user) { TextParser parser = new TextParser(this.Manager); TextDefinition subject = parser.ParseMessage(this.Subject, new Dictionary <ReplaceableObjectKeys, object>() { { ReplaceableObjectKeys.Code, token }, { ReplaceableObjectKeys.User, user } }); TextDefinition body = parser.ParseMessage(this.BodyFormat, new Dictionary <ReplaceableObjectKeys, object>() { { ReplaceableObjectKeys.Code, token }, { ReplaceableObjectKeys.User, user } }); new TaskFactory().StartNew(() => { SmtpMailClient.SendMail(user.Email, subject.Text, body.Text, body.Html); }); return(Task.FromResult <int>(0)); }
/// <summary> /// Creates or updates a patient. /// If a user with the given Username doesn't exist it will be created, if it does exist, the patient will be added to that user /// </summary> /// <param name="externalId">The external ID of the patient</param> /// <param name="userName">The username of the user</param> /// <param name="email">The email</param> /// <param name="title">the title of the patient</param> /// <param name="firstName">The first name</param> /// <param name="lastName">The last name</param> /// <param name="dateOfBirth">The date of birth</param> /// <param name="mobilePhone">The patients mobile phone</param> /// <returns>The created or updated Patient</returns> public Patient CreateOrUpdatePatient(string externalId, string userName, string email, string title, string firstName, string lastName, DateTime dateOfBirth, string mobilePhone) { try { SecuritySession.Current.VerifyAccess(Actions.CREATE_OR_UPDATE_PATIENT); if (userName.Length > 450) { throw this.manager.MessageHandler.GetError(ErrorCodes.USERNAME_LENGTH_EXCEEDED); } if (userName.Contains("\\") || userName.Contains("/")) { throw this.manager.MessageHandler.GetError(ErrorCodes.USERNAME_CONTAINS_ILLEGAL_CHARACTERS); } } catch (Exception ex) { Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, typeof(Patient), "Email", email, false, ex.Message)); throw ex; } IdentityResult result = new IdentityResult(null); User existing = this.Users.Where(u => u.UserName == userName).SingleOrDefault(); User user = null; if (existing == null) { try { result = UserManagerExtensions.Create(this, new User() { UserName = userName, Email = email, PhoneNumber = mobilePhone, Title = title, FirstName = firstName, LastName = lastName }); if (result.Succeeded) { user = this.Users.Where(u => u.UserName == userName).SingleOrDefault(); } else { throw new PCHIError(ErrorCodes.GENERAL_IDENTITY_RESULT_ERROR, result.Errors.Aggregate((s1, s2) => { return(s1 + "\n" + s2); })); } Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, user)); } catch (Exception ex) { Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, typeof(User), "UserName", userName, false, ex.Message)); throw ex; } } else { user = existing; } Patient patient = null; Patient newPatient = null; if (user != null) { patient = !string.IsNullOrWhiteSpace(externalId) ? this.manager.UserAccessHandler.GetPatientByExternalId(externalId) : null; try { if (patient != null) { Patient p = patient; p.Title = title; p.FirstName = firstName; p.LastName = lastName; p.ProxyUserPatientMap.Add(new ProxyUserPatientMap(user, p)); p.DateOfBirth = dateOfBirth; p.Email = email; p.PhoneNumber = mobilePhone; p.ExternalId = externalId; this.manager.UserAccessHandler.Update(p); Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.MODIFIED, p)); } else { Patient p = new Patient(); p.Title = title; p.FirstName = firstName; p.LastName = lastName; p.ProxyUserPatientMap.Add(new ProxyUserPatientMap(user, p)); p.DateOfBirth = dateOfBirth; p.Email = email; p.PhoneNumber = mobilePhone; p.ExternalId = externalId; this.manager.UserAccessHandler.Add(p); newPatient = p; Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, p)); } this.AddToRole(user.Id, "PatientProxy"); } catch (Exception ex) { Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, typeof(Patient), "Email", email, false, ex.Message)); throw ex; } } // Only send the registration mail if the user is created (i.e. existing is null) if (existing == null && user != null) { UserSecurityCode confirmationToken = null; do { confirmationToken = UserSecurityCode.CreateSecurityCode(user, "Registration"); } while (this.FindUserForRegistrationToken(confirmationToken.Code) != null); user.RegistrationConfirmationToken = confirmationToken.EncryptedCode; UserManagerExtensions.Update(this, user); // string confirmationToken = HttpUtility.UrlEncode(MachineKeyEncryption.Encrypt(user.UserName)); TextParser parser = new TextParser(this.manager); TextDefinition td = parser.ParseMessage("RegistrationEmail", new Dictionary <Model.Messages.ReplaceableObjectKeys, object>() { { ReplaceableObjectKeys.Patient, newPatient }, { ReplaceableObjectKeys.Code, confirmationToken.Code } }); SmtpMailClient.SendMail(user.Email, "OPSMC RePLAY Registration", td.Text, td.Html); } if (newPatient != null) { try { QuestionnaireUserResponseGroup group = this.manager.QuestionnaireAccessHandler.CreateQuestionnaireUserResponseGroup(newPatient.Id, BusinessLogic.Properties.Settings.Default.NewRegistrationQuestionnaire, null, null); Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, group)); } catch (Exception ex) { Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, typeof(QuestionnaireUserResponseGroup), "Id", null, false, ex.Message)); throw ex; } } return(newPatient == null ? patient : newPatient); }