Exemplo n.º 1
0
        public async Task <IActionResult> ResetPassword([FromBody] Models.Credentials credentials)
        {
            //TODO add some abuse prevention mechanism
            //report user to newrelic
            NewRelic.Api.Agent.NewRelic.AddCustomParameter("credentials.email", credentials.email);
            var customer = await _customerRepository.Get(credentials.email);

            if (customer == null)
            {
                return(new BadRequestObjectResult("If you continue having problemas please contact us!!"));
            }

            if (customer.Tipo == (int)Models.Credentials.Types.Social)
            {
                return(new BadRequestObjectResult("Invalid option, you cannot reset your password from here!!"));
            }

            var destination = new List <SendGrid.Helpers.Mail.EmailAddress> {
                new SendGrid.Helpers.Mail.EmailAddress(credentials.email, credentials.email)
            };
            var payload = new
            {
                name     = credentials.email,
                password = Password.CreateWithRandomLength()
            };
            var result = await _customerRepository.UpdatePassword(new Models.Credentials {
                email = payload.name, password = payload.password
            });

            if (result)
            {
                var mail = await Email.SendTransactional(destination, Email.Templates.Transactional.PasswordReset, payload);

                if (mail.StatusCode == System.Net.HttpStatusCode.Accepted)
                {
                    return(new OkResult());
                }
                else
                {
                    var error = await mail.Body.ReadAsStringAsync();

                    return(new ObjectResult(error)
                    {
                        StatusCode = (int)mail.StatusCode
                    });
                }
            }
            return(new StatusCodeResult(304)); //not modified
        }