public ActionResult doRegister(RegisterModel registerModel) { if (ModelState.IsValid) { //convert form input to userDTO UserDTO user = new UserDTO(); user.FirstName = registerModel.FirstName; user.LastName = registerModel.LastName; user.Email = registerModel.Email; user.Password = Encoding.UTF8.GetBytes(registerModel.Password); user.TimeZone = registerModel.TimeZone; if (registerService.createNewUser(user)) { logger.Info("Register worked"); return RedirectToAction("RegisterComplete"); } else { ViewBag.UserExists = false; return View("Register"); } } return View("Register"); }
public UserDTO Login(string email, string password) { UserDTO userDTO = new UserDTO(); logger.Info("testing login functionality"); var users = unitOfWork.UserRepository.Get(u => u.Email.Equals(email)); User userEntity = null; if (users.Count() != 1) { return null; } userEntity = users.First(); if (!userEntity.AccountConfirmed) { return null; } else { Mapper.CreateMap<User, UserDTO>(); userDTO = Mapper.Map<User, UserDTO>(userEntity); byte[] submittedPassword = PasswordManagement.GenerateSaltedPassword(Encoding.UTF8.GetBytes(password), userDTO.Salt); if (PasswordManagement.ComparePasswords(userDTO.Password, submittedPassword)) { return userDTO; } else { return null; } } }
public ReportDTO GetReportForTimePeriod(DateTime begin, DateTime end, UserDTO user) { logger.Info("Inside get report for time period"); ReportDTO reportDTO = new ReportDTO(); logger.Info("Begin time: " + begin.ToString()); logger.Info("End time: " + end.ToString()); IList<LogEntry> logEntries = unitOfWork.LogEntryRepository.Get(l => l.StartTime > begin && l.StartTime < end && l.UserId == user.UserId).ToList(); logger.Info("Count in list: " + logEntries.Count()); if (logEntries.Count() != 0) { logger.Info("Loading results"); reportDTO.ActivityCount = logEntries.Count(); reportDTO.Calories = (int)logEntries.Sum(l => l.Calories); reportDTO.Duration = logEntries.Sum(l => l.Duration); reportDTO.Distance = logEntries.Sum(l => l.Distance); logger.Info("Testing activity count: " + reportDTO.ActivityCount); } else { reportDTO.ActivityCount = 0; reportDTO.Calories = 0; reportDTO.Duration = 0; reportDTO.Distance = 0; logger.Info("no results"); } return reportDTO; }
public void DeleteEntry(int logId, UserDTO user) { //make sure the entry to delete belongs to current user LogEntry logEntryToDelete = unitOfWork.LogEntryRepository.Get(l => l.LogId == logId && l.UserId == user.UserId).First(); unitOfWork.LogEntryRepository.Delete(logEntryToDelete); unitOfWork.Save(); }
public LogEntryDTO GetActivity(int activityId, UserDTO user) { LogEntry le; le = unitOfWork.LogEntryRepository.Get(l => l.LogId == activityId && l.UserId == user.UserId).First(); if (le == null) { return null; } LogEntryDTO leDTO = new LogEntryDTO(); Mapper.CreateMap<LogEntry, LogEntryDTO>(); leDTO = Mapper.Map<LogEntry, LogEntryDTO>(le); return leDTO; }
public bool AddActivity(LogEntryDTO logEntryDTO, UserDTO user) { logger.Info("Inside Add Activity Method"); LogEntry logEntryEntity = new LogEntry(); Mapper.CreateMap<LogEntryDTO, LogEntry>(); logEntryEntity = Mapper.Map<LogEntryDTO, LogEntry>(logEntryDTO); if (logEntryDTO.ShoeId.HasValue) { int _shoeId = (int)logEntryDTO.ShoeId; Shoe shoeEntity = unitOfWork.ShoeRepository.GetByID(_shoeId); shoeEntity.ShoeDistance += logEntryDTO.Distance; logEntryEntity.Shoe = shoeEntity; } logEntryEntity.User = unitOfWork.UserRepository.GetByID(user.UserId); logEntryEntity.ActivityType = unitOfWork.ActivityTypesRepository.GetByID(logEntryDTO.ActivityTypesId); unitOfWork.LogEntryRepository.Insert(logEntryEntity); unitOfWork.Save(); return true; }
public void CreateUser_UserDoesExist_ReturnsFalse() { byte[] salt = PasswordManagement.GenerateSalt(); byte[] pass = PasswordManagement.GenerateSaltedPassword(Encoding.UTF8.GetBytes("Password"), salt); var mock = new Mock<IUnitOfWork>(); mock.Setup(u => u.UserRepository.Get(It.IsAny<Expression<Func<User, bool>>>(), null, "")).Returns( new List<User> { new User { UserId = 4, FirstName = "Test4", LastName = "LastName", Email = "*****@*****.**", Salt = salt, Password = pass }, }); RegisterService registerService = new RegisterService(mock.Object); UserDTO testUser = new UserDTO(); testUser.Email = "*****@*****.**"; bool result = registerService.createNewUser(testUser); Assert.IsFalse(result); }
public bool createNewUser(UserDTO user) { user.ConfirmCode = PasswordManagement.RandomString(26, true); var users = unitOfWork.UserRepository.Get(u => u.Email.Equals(user.Email)); if (users.Count() > 0) //check if user already exists { return false; } else { createPassword(user); Mapper.CreateMap<UserDTO, User>(); User userEntity = Mapper.Map<UserDTO, User>(user); userEntity.DistanceType = "Miles"; unitOfWork.UserRepository.Insert(userEntity); unitOfWork.Save(); SendEmail(user); } return true; }
public int GetCountOfUserLogEntries(UserDTO user) { return unitOfWork.LogEntryRepository.Get(l => l.UserId == user.UserId).Count(); }
public void UpdateActivity(LogEntryDTO logEntryDTO, UserDTO user) { LogEntry logEntryEntity = new LogEntry(); Mapper.CreateMap<LogEntryDTO, LogEntry>(); logEntryEntity = Mapper.Map<LogEntryDTO, LogEntry>(logEntryDTO); if (logEntryDTO.ShoeId.HasValue) { int _shoeId = (int)logEntryDTO.ShoeId; Shoe shoeEntity = unitOfWork.ShoeRepository.GetByID(_shoeId); shoeEntity.ShoeDistance += logEntryDTO.Distance; logEntryEntity.Shoe = shoeEntity; } logEntryEntity.User = unitOfWork.UserRepository.GetByID(user.UserId); logEntryEntity.ActivityType = unitOfWork.ActivityTypesRepository.GetByID(logEntryDTO.ActivityTypesId); LogEntry OldLogEntry = unitOfWork.LogEntryRepository.GetByID(logEntryEntity.LogId); unitOfWork.LogEntryRepository.Update(logEntryEntity, OldLogEntry); unitOfWork.Save(); }
public IList<ShoeDTO> GetUserShoes(UserDTO user) { IList<Shoe> userShoes = unitOfWork.ShoeRepository.Get(s => s.UserId == user.UserId).ToList(); IList<ShoeDTO> userShoesDTO = new List<ShoeDTO>(); Mapper.CreateMap<UserDTO, User>(); Mapper.CreateMap<Shoe, ShoeDTO>(); User userEntity = Mapper.Map<UserDTO, User>(user); foreach (Shoe s in userShoes) { ShoeDTO temp = Mapper.Map<Shoe, ShoeDTO>(s); userShoesDTO.Add(temp); } logger.Info("Number of user shoes: " + userShoesDTO.Count); return userShoesDTO; }
public IList<LogEntryDTO> GetUserRunningLogEntries(UserDTO user, int page) { IList<LogEntry> logEntries = unitOfWork.LogEntryRepository.Get(l => l.UserId == user.UserId, q => q.OrderByDescending(l => l.StartTime)).Skip((page - 1) * PageSize).Take(PageSize).ToList(); IList<LogEntryDTO> logEntriesDTO = new List<LogEntryDTO>(); Mapper.CreateMap<LogEntry, LogEntryDTO>(); foreach (LogEntry l in logEntries) { LogEntryDTO temp = Mapper.Map<LogEntry, LogEntryDTO>(l); logEntriesDTO.Add(temp); } return logEntriesDTO; }
private static void SendEmail(UserDTO user) { //use thread pool to prevent the controller from blocking the SendAsync method ThreadPool.QueueUserWorkItem(t => { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(); mail.To.Add(user.Email); mail.Subject = "Runner's Tracker Password Reset"; mail.IsBodyHtml = true; mail.Body = createResetPasswordEmail(user); SmtpServer.SendCompleted += delegate(object sender, AsyncCompletedEventArgs e) { if (e.Error != null) { System.Diagnostics.Trace.TraceError(e.Error.ToString()); logger.Info(e.Error.ToString()); } MailMessage userMessage = e.UserState as MailMessage; if (userMessage != null) { userMessage.Dispose(); } }; SmtpServer.SendAsync(mail, mail); }); }
private static string createResetPasswordEmail(UserDTO user) { using (var sw = new MemoryStream()) { using (var xw = XmlWriter.Create(sw)) { xw.WriteStartDocument(); xw.WriteStartElement("password"); xw.WriteStartElement("firstName"); xw.WriteString(user.FirstName); xw.WriteEndElement(); xw.WriteStartElement("resetLink"); xw.WriteString("http://localhost/login/newpassword/" + user.PassResetCode); xw.WriteEndElement(); xw.WriteEndElement(); xw.WriteEndDocument(); } sw.Position = 0; //set to beginning of stream before reading XPathDocument xpath = new XPathDocument(sw); XslCompiledTransform myXslTrans = new XslCompiledTransform(); logger.Info(AppDomain.CurrentDomain.BaseDirectory); myXslTrans.Load(AppDomain.CurrentDomain.BaseDirectory + "..\\Business\\MailTemplates\\resetPassTemplate.xslt"); using (var ms = new MemoryStream()) { myXslTrans.Transform(xpath, null, ms); ms.Position = 0; //set tp beginning of stream before reading StreamReader reader = new StreamReader(ms); return reader.ReadToEnd(); } } }
public bool ResetPassword(string email) { UserDTO userDto = new UserDTO(); var users = unitOfWork.UserRepository.Get(u => u.Email.Equals(email)); if (users.Count() == 1) { User userEntity = users.First(); Mapper.CreateMap<User, UserDTO>(); UserDTO user = Mapper.Map<User, UserDTO>(userEntity); user.PassResetCode = PasswordManagement.RandomString(26, true); user.PassResetExpire = DateTime.Now.AddMinutes(10); Mapper.CreateMap<UserDTO, User>(); User userUpdated = Mapper.Map<UserDTO, User>(user); unitOfWork.UserRepository.Update(userUpdated, userEntity); unitOfWork.Save(); SendEmail(user); return true; } else { return false; } }
private UserDTO createPassword(UserDTO user) { user.Salt = PasswordManagement.GenerateSalt(); byte[] pass = user.Password; user.Password = PasswordManagement.GenerateSaltedPassword(pass, user.Salt); return user; }