Esempio n. 1
0
        /// <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));
        }
Esempio n. 2
0
 public HttpResponseMessage ResetPasswordSoftwarePlatform_Post([FromBody] JsonResetPasswordRequest jsonResetPasswordRequest)
 {
     return(ResetPasswordSoftwarePlatform(jsonResetPasswordRequest));
 }