/// <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> /// Generates the code /// </summary> /// <param name="purpose">The purpose to generate it for</param> /// <param name="user">The user to generate the token for</param> /// <returns>A token generated for the user</returns> protected string GenerateCode(string purpose, User user) { AccessHandlerManager ahm = new AccessHandlerManager(); UserSecurityCode code = ahm.UserAccessHandler.GetSecurityCode(user.Id, purpose); if (code == null || code.ExpiresAt < DateTime.Now) { code = UserSecurityCode.CreateSecurityCode(user, purpose); } else { code.ResetExpiry(); } new AccessHandlerManager().UserAccessHandler.StoreSecurityCode(code); return(code.Code); }
/// <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); }