public void UpdatedNoneToHaving() { var account = new UserAccount(); account.Save(); account = account.AsWritable <UserAccount>(); account.AccountHolder = new Person(); account.Save(); var person = Entity.Get <Person>(account.AccountHolder.Id); Assert.That(person.CalcPersonHasAccount ?? false, Is.True); }
/// <summary> /// Reset the specified user account password. /// </summary> /// <param name="jsonResetPasswordRequest">The password reset details.</param> /// <returns></returns> private HttpResponseMessage ResetPasswordSoftwarePlatform(JsonResetPasswordRequest jsonResetPasswordRequest) { string tenant = jsonResetPasswordRequest.Tenant; string newPassword = jsonResetPasswordRequest.NewPassword; if (string.IsNullOrEmpty(newPassword)) { return(new HttpResponseMessage <string>("The new password is invalid", HttpStatusCode.BadRequest)); } if (string.IsNullOrEmpty(tenant)) { return(new HttpResponseMessage <string>("The tenant is invalid", HttpStatusCode.BadRequest)); } try { using (new TenantAdministratorContext(tenant)) { string key = jsonResetPasswordRequest.Key; // Create query var type = new EntityRef("core", "userAccount"); var query = new EDC.ReadiNow.Metadata.Query.Structured.StructuredQuery { RootEntity = new EDC.ReadiNow.Metadata.Query.Structured.ResourceEntity(type) }; var passwordResetEntity = new EDC.ReadiNow.Metadata.Query.Structured.RelatedResource(new EntityRef("core:passwordReset")); query.RootEntity.RelatedEntities.Add(passwordResetEntity); query.Conditions.Add(new EDC.ReadiNow.Metadata.Query.Structured.QueryCondition { Expression = new EDC.ReadiNow.Metadata.Query.Structured.ResourceDataColumn(passwordResetEntity, new EntityRef("core:passwordResetKey")), Operator = EDC.ReadiNow.Metadata.Query.Structured.ConditionType.Equal, Argument = new EDC.ReadiNow.Metadata.TypedValue(key) }); // Get results IEnumerable <UserAccount> entities = ReadiNow.Model.Entity.GetMatches <UserAccount>(query); UserAccount accountInfo = entities != null && entities.Count() > 0 ? entities.FirstOrDefault() : null; if (accountInfo == null || accountInfo.PasswordReset == null) { return(new HttpResponseMessage <string>("This key is invalid, Please contact your administrator.", HttpStatusCode.BadRequest)); } if (accountInfo.PasswordReset.PasswordResetExpiry < DateTime.UtcNow) { return(new HttpResponseMessage <string>("This key is expired, Please contact your administrator.", HttpStatusCode.BadRequest)); } UserAccount account = accountInfo.AsWritable <UserAccount>(); if (account.AccountStatus_Enum == UserAccountStatusEnum_Enumeration.Expired || account.AccountStatus_Enum == UserAccountStatusEnum_Enumeration.Disabled) { return(new HttpResponseMessage <string>("Your account has expired or has been disabled. Please contact your administrator", HttpStatusCode.BadRequest)); } if (account == null) { throw new InvalidCredentialException("Invalid account, Please contact your administrator."); } account.Password = newPassword; account.PasswordReset.AsWritable <PasswordResetRecord>(); account.PasswordReset.Delete(); //unlock the account account.AccountStatus_Enum = UserAccountStatusEnum_Enumeration.Active; try { account.Save(); } catch (Exception ex) { EventLog.Application.WriteError("Reset Password: Unhandled internal exception: " + ex.ToString()); return(new HttpResponseMessage <string>("There is an error to reset your new password. Please contact your administrator", HttpStatusCode.BadRequest)); } } } catch (EntityNotFoundException tenantEx) { string errorMessage = tenantEx.InnerException != null ? tenantEx.InnerException.Message : tenantEx.Message; EventLog.Application.WriteError("Reset Password: Unhandled internal Entity Not Found Exception: " + errorMessage); return(new HttpResponseMessage <string>(errorMessage, HttpStatusCode.BadRequest)); } catch (Exception otherEx) { EventLog.Application.WriteError("Reset Password: Unhandled internal Exception: " + otherEx.Message); return(new HttpResponseMessage <string>(otherEx.Message, HttpStatusCode.BadRequest)); } return(new HttpResponseMessage(HttpStatusCode.OK)); }
/// <summary> /// Send reset password Email. /// </summary> /// <param name="displayName">The display name.</param> /// <returns></returns> private bool SendEmail(UserAccount account, string email, string tenant) { bool successSentEmail = false; try { account = account.AsWritable <UserAccount>(); //only check current account is available or not if (account == null) { return(successSentEmail); } string key = EDC.Security.CryptoHelper.GetMd5Hash(account.Id.ToString()); if (account.PasswordReset != null) { account.PasswordReset.AsWritable <PasswordResetRecord>(); account.PasswordReset.Delete(); } account.PasswordReset = new PasswordResetRecord { PasswordResetKey = key, PasswordResetExpiry = DateTime.UtcNow.AddDays(1.0) }; account.Save(); EventLog.Application.WriteInformation("set password reset key {0}", key); var mail = new SentEmailMessage() { EmIsHtml = true, EmSentDate = DateTime.UtcNow, }; string accountFullName = account.AccountHolder.FirstName + " " + account.AccountHolder.LastName; if (accountFullName.Trim().Length == 0) { accountFullName = account.AccountHolder.Name; } //string toMail = email; const string subject = "Reset Password - ReadiNow"; string link = string.Format("{0}#/{1}/?key={2}&type=reset", Request.Headers.Referrer.ToString(), tenant, key); string mailBoday = string.Format("Hi {0} <br> <br> We've received a request to reset your account {1} password for email address {2} <br> If you didn't make the request, please contact your administrator. <br> Otherwise, you can reset your password using this link: {3} <br><br>Best Regards, <br>ReadiNow", accountFullName, account.Name, email, link); var emailServer = ReadiNow.Model.Entity.Get <TenantEmailSetting>("tenantEmailSettingsInstance", TenantEmailSetting.AllFields); if (!string.IsNullOrEmpty(emailServer.SmtpServer) && !string.IsNullOrEmpty(emailServer.EmailNoReplyAddress)) { EventLog.Application.WriteInformation("Send user {0} reset password email to account '{1}' by email '{2}'", accountFullName, account.Name, email); var sentMessage = new SentEmailMessage() { EmTo = email, EmFrom = emailServer.EmailNoReplyAddress, EmSubject = subject, EmBody = mailBoday, EmIsHtml = true, EmSentDate = DateTime.UtcNow, SentFromEmailServer = emailServer }; sentMessage.Save(); var sender = new SmtpEmailSender(emailServer); sender.SendMessages(sentMessage.ToMailMessage().ToEnumerable().ToList()); } successSentEmail = true; } catch (Exception ex) { EventLog.Application.WriteError("Send Email: Unhandled internal exception: " + ex.ToString()); } return(successSentEmail); }