diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-05-11 08:24:34 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-05-11 08:24:34 +0300 |
| commit | f4da5783b128b3da55eae406b054ae0f6e4d1b55 (patch) | |
| tree | b8ec8b39b4c4c1da04f5f9df1f439a4d2799002a /Software/Visual_Studio/Web/Tango.MachineService/Controllers | |
| parent | 7b820ac51cd61b34c41a83de0d46d253cec38a67 (diff) | |
| download | Tango-f4da5783b128b3da55eae406b054ae0f6e4d1b55.tar.gz Tango-f4da5783b128b3da55eae406b054ae0f6e4d1b55.zip | |
Forgot Password !
Reset Password !
Diffstat (limited to 'Software/Visual_Studio/Web/Tango.MachineService/Controllers')
| -rw-r--r-- | Software/Visual_Studio/Web/Tango.MachineService/Controllers/FSEAccountController.cs | 67 | ||||
| -rw-r--r-- | Software/Visual_Studio/Web/Tango.MachineService/Controllers/FSEController.cs | 71 |
2 files changed, 126 insertions, 12 deletions
diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/FSEAccountController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/FSEAccountController.cs new file mode 100644 index 000000000..b7728af6a --- /dev/null +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/FSEAccountController.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using Tango.BL; +using Tango.MachineService.Filters; +using Tango.MachineService.Models; +using Tango.Web.Helpers; +using System.Data.Entity; +using Tango.Web.Storage; +using System.IO; +using Microsoft.WindowsAzure.Storage.Blob; +using System.Net.Http; +using System.Net; +using System.Net.Http.Headers; +using System.Net.Mime; +using Tango.MachineService.Views.FSEAccount; + +namespace Tango.MachineService.Controllers +{ + public class FSEAccountController : Controller + { + private static Random rnd = new Random(); + + public ActionResult ResetPassword(String id) + { + ResetPasswordVM vm = new ResetPasswordVM(); + vm.FullName = "Full Name"; + vm.Password = "Password"; + + var reset = FSEController.PendingPasswordResets.SingleOrDefault(x => x.ID == id); + + if (reset != null) + { + using (ObservablesContext db = ObservablesContextHelper.CreateContext()) + { + var user = db.Users.SingleOrDefault(x => x.Guid == reset.UserGuid); + + if (user != null) + { + String newPass = GenerateRandomPassword(); + user.Password = Tango.BL.Entities.User.GetPasswordHash(newPass); + user.PasswordChangeRequired = true; + vm.Password = newPass; + vm.FullName = reset.FullName; + db.SaveChanges(); + } + } + } + + return View(vm); + } + + private String GenerateRandomPassword() + { + String pass = String.Empty; + + for (int i = 0; i < 4; i++) + { + pass += rnd.Next(0, 9).ToString(); + } + + return pass; + } + } +}
\ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/FSEController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/FSEController.cs index 01878f16a..e471ed20c 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/FSEController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/FSEController.cs @@ -23,6 +23,7 @@ using Tango.Web.Security; using Tango.Web.SMO; using Tango.Web.SQLServer; using Tango.Web.Storage; +using System.Data.Entity; using static Tango.MachineService.Controllers.FSEController; namespace Tango.MachineService.Controllers @@ -34,6 +35,20 @@ namespace Tango.MachineService.Controllers public String UserGuid { get; set; } } + public class PasswordReset + { + public String ID { get; set; } + public String UserGuid { get; set; } + public String FullName { get; set; } + } + + public static List<PasswordReset> PendingPasswordResets { get; set; } + + static FSEController() + { + PendingPasswordResets = new List<PasswordReset>(); + } + [HttpPost] public LoginResponse Login(LoginRequest request) { @@ -222,9 +237,13 @@ namespace Tango.MachineService.Controllers [JwtTokenFilter] public UserInvitationEmailResponse SendUserInvitationEmail(UserInvitationEmailRequest request) { + User user; + using (ObservablesContext db = ObservablesContextHelper.CreateContext()) { - if (!db.Users.Any(x => x.Email.ToLower() == request.Email.ToLower())) + user = db.Users.Include(x => x.Contact).SingleOrDefault(x => x.Guid == request.UserGuid); + + if (user == null) { throw new InvalidOperationException("User not found."); } @@ -233,15 +252,15 @@ namespace Tango.MachineService.Controllers var client = new SendGridClient(MachineServiceConfig.SEND_GRID_API_KEY); SendGridMessage msg = new SendGridMessage(); msg.SetFrom("info@twine-s.com", "Twine Solutions LTD"); - msg.AddTo(request.Email); + msg.AddTo(user.Email); msg.Subject = "Welcome To Tango FSE"; msg.SetTemplateId("d-2af42ed0ea3c44b3abaa61016223555a"); var dynamicTemplateData = new { DownloadUrl = $"{request.MachineServiceAddress}/fse", - request.FullName, - request.Password, + FullName = user.Contact.FirstName, + Password = request.Password, }; msg.SetTemplateData(dynamicTemplateData); @@ -259,22 +278,50 @@ namespace Tango.MachineService.Controllers [HttpPost] public ForgotPasswordResponse SendForgotPasswordEmail(ForgotPasswordRequest request) { + User user; + using (ObservablesContext db = ObservablesContextHelper.CreateContext()) { - if (!db.Users.Any(x => x.Email.ToLower() == request.Email.ToLower())) + user = db.Users.Include(x => x.Contact).SingleOrDefault(x => x.Email.ToLower() == request.Email.ToLower()); + + if (user == null) { throw new InvalidOperationException("User not found."); } } - //TODO: create a special link that when surfed will reset the password for the user and display the new temp password. - return new ForgotPasswordResponse(); - } + String resetId = Guid.NewGuid().ToString(); - [HttpGet] - public String ResetPassword(String guid) - { - return String.Empty; //Reset the password and return the new temp password. + var client = new SendGridClient(MachineServiceConfig.SEND_GRID_API_KEY); + SendGridMessage msg = new SendGridMessage(); + msg.SetFrom("info@twine-s.com", "Twine Solutions LTD"); + msg.AddTo(request.Email); + msg.Subject = "Tango FSE Password Reset"; + msg.SetTemplateId("d-18065487dae4456b8684d4b47a91e4a6"); + + var dynamicTemplateData = new + { + ResetPasswordUrl = $"{request.MachineServiceAddress}/FSEAccount/ResetPassword?id={resetId}", + FullName = user.Contact.FirstName, + }; + + msg.SetTemplateData(dynamicTemplateData); + + var result = client.SendEmailAsync(msg).GetAwaiter().GetResult(); + + if (result.StatusCode != HttpStatusCode.Accepted) + { + throw new HttpException(result.StatusCode.ToString()); + } + + PendingPasswordResets.Add(new PasswordReset() + { + ID = resetId, + UserGuid = user.Guid, + FullName = user.Contact.FirstName, + }); + + return new ForgotPasswordResponse(); } } } |
