public void DALTest1() { using (StepMapDbContext ctx = new StepMapDbContext()) { User u = new User(); u.Email = "*****@*****.**"; u.LastLogin = DateTime.UtcNow; u.Name = "test user"; u.PasswordHash = "hash"; u.UserRole = UserRole.Member; u.UserState = UserState.Active; u = ctx.Users.Add(u); ctx.SaveChanges(); try { User du = ctx.Users.Single(tu => tu.Email == "*****@*****.**"); Assert.AreEqual(u.LastLogin, du.LastLogin); Assert.AreEqual(u.Name, du.Name); Assert.AreEqual(u.PasswordHash, du.PasswordHash); Assert.AreEqual(u.UserRole, du.UserRole); Assert.AreEqual(u.UserState, du.UserState); } finally { ctx.Users.Remove(u); ctx.SaveChanges(); } } }
public void UserManagerTest1() { Mock<ILogger> logger = new Mock<ILogger>(); Mock<IStepMapConfig> config = new Mock<IStepMapConfig>(); Mock<IRegexHelper> regexHelper = new Mock<IRegexHelper>(); Mock<INotificationManager> notificationManager = new Mock<INotificationManager>(); regexHelper.Setup(r => r.IsValidEmail(It.IsAny<string>())).Returns(true); UserManager um = new UserManager(logger.Object, config.Object, regexHelper.Object, notificationManager.Object); um.Register("test user", "*****@*****.**", "hash"); using (var ctx = new StepMapDbContext()) { User u = ctx.Users.Single(tu => tu.Email == "*****@*****.**"); try { Assert.AreEqual("test user", u.Name); Assert.AreEqual("*****@*****.**", u.Email); Assert.AreEqual("hash", u.PasswordHash); } finally { ctx.Users.Remove(u); ctx.SaveChanges(); } } }
public bool IsPasswordValid(string userName, string pwdHash) { using (var ctx = new StepMapDbContext()) { var user = ctx.Users.Where(u => u.Name == userName && u.PasswordHash == pwdHash).SingleOrDefault(); return user != null; } }
public void AddProject(Project project) { var l = project.FinishedSteps.ToList(); using (var ctx = new StepMapDbContext()) { project = ctx.Projects.Add(project); ctx.SaveChanges(); } }
public void DALTest2() { using (StepMapDbContext ctx = new StepMapDbContext()) { User u = new User(); u.Email = "*****@*****.**"; u.LastLogin = DateTime.UtcNow; u.Name = "test user"; u.PasswordHash = "hash"; u.UserRole = UserRole.Member; u.UserState = UserState.Active; Project p = new Project(); p.Name = "test project"; p.User = u; Step s = new Step(); s.Name = "test step name"; s.Project = p; Reminder r = new Reminder(); r.Step = s; r.EmailAddress = "*****@*****.**"; r.Message = "test message"; r.SentDate = DateTime.UtcNow; r.Subject = "test subject"; ctx.Projects.Add(p); ctx.Steps.Add(s); ctx.Reminders.Add(r); ctx.SaveChanges(); try { Project dp = ctx.Projects.Single(tu => tu.Name == "test project"); Assert.AreEqual(p.UserId, dp.UserId); Step ds = dp.FinishedSteps.Single(); Assert.AreEqual(s.Name, ds.Name); Reminder dr = ds.SentReminders.Single(); Assert.AreEqual(dr.EmailAddress, r.EmailAddress); Assert.AreEqual(dr.Message, r.Message); Assert.AreEqual(dr.SentDate, r.SentDate); Assert.AreEqual(dr.Subject, r.Subject); } finally { s.SentReminders.Clear(); p.FinishedSteps.Clear(); ctx.Reminders.Remove(r); ctx.Steps.Remove(s); ctx.Projects.Remove(p); ctx.Users.Remove(u); ctx.SaveChanges(); } } }
public IEnumerable<Project> GetProjects(User user) { List<Project> ret = new List<Project>(7); using (var ctx = new StepMapDbContext()) { var pl = ctx.Projects.Where(p => p.UserId == user.Id).Include(s => s.FinishedSteps).Include(s => s.User).ToList(); for (int i = 0; i < 7; i++) { ret.Add(pl.SingleOrDefault(p => p.Position == i)); } } return ret; }
public DAL.User GetUser(string userName) { using (var ctx = new StepMapDbContext()) { var user = ctx.Users.Where(u => u.Name == userName).SingleOrDefault(); if (user == null) { throw new InvalidOperationException(string.Format("User not found: {0}!", userName)); } else { return user; } } }
public void UpdateProject(Project project) { using (var ctx = new StepMapDbContext()) { project = ctx.Projects.Attach(project); ctx.Entry(project).State = EntityState.Modified; foreach(var step in ctx.Steps.Where(s=>s.Id != 0)) { var s = ctx.Steps.Attach(step); ctx.Entry(s).State = EntityState.Modified; } ctx.Steps.AddRange(project.FinishedSteps.Where(s => s.Id == 0)); ctx.SaveChanges(); } }
public void DeleteProject(int projectId) { using (var ctx = new StepMapDbContext()) { var project = ctx.Projects.Include(s => s.FinishedSteps).SingleOrDefault(p => p.Id == projectId); if (project == null) { throw new ArgumentException(string.Format("Project does not exsist: {0}.", projectId)); } else { ctx.Steps.RemoveRange(project.FinishedSteps); ctx.Projects.Remove(project); ctx.SaveChanges(); } } }
private void SendConfirmationEmail(User user) { using (var ctx = new StepMapDbContext()) { user = ctx.Users.Attach(user); UserConfirmation uc = new UserConfirmation(); uc.User = user; uc.ConfirmationGuid = Guid.NewGuid().ToString(); ctx.UserConfirmations.Add(uc); ctx.SaveChanges(); string link = config.ClientBaseAddress + "/Account/ConfirmEmail/?guid=" + uc.ConfirmationGuid; //TODO: do not hardcode text notificationManager.SendEmail(user, "registration on stepmap.xyz", string.Format("you are registered on stepmap.xyz. please confirm your accout visiting this link: {0}!", link)); //LOCSTR } }
public void CheckAllProjectsProgress() { List<Project> projects; using(var ctx = new StepMapDbContext()) { projects = ctx.Projects.Include(p=>p.User).Include(p=>p.FinishedSteps.Select(x=>x.SentReminders)).ToList(); } //Parallel.ForEach(ctx.Projects, (project) => //{ // projectManager.CheckProjectProgress(project); //}); foreach(var proj in projects) { projectManager.CheckProjectProgress(proj); } }
private void SentFirstReminder(User user, Project project, Step step) { using (var ctx = new StepMapDbContext()) { //TODO: Config, customize, randomize Reminder reminder = new Reminder() { EmailAddress = user.Email, Message = string.Format("Your current step ({0}) in project {1} is delayed! Get yourself together!", step.Name, project.Name), //LOCSTR Subject = "First reminder", //LOCSTR SentDate = DateTime.UtcNow, StepId = step.Id }; ctx.Reminders.Add(reminder); notificationManager.SendEmail(user, reminder.Subject, reminder.Message); ctx.SaveChanges(); } }
public void Register(string userName, string email, string pwdHash) { bool isValid = regexHelper.IsValidEmail(email); if (isValid) { using (var ctx = new StepMapDbContext()) { User oldUser = ctx.Users.SingleOrDefault(u => u.Name == userName); if (oldUser == null) { User oldEmail = ctx.Users.SingleOrDefault(u => u.Email == email); if (oldEmail == null) { User user = new User() { Name = userName, Email = email, PasswordHash = pwdHash, UserRole = UserRole.Member, UserState = UserState.NotActivatedYet }; user = ctx.Users.Add(user); ctx.SaveChanges(); SendConfirmationEmail(user); } else { throw new UserAlreadyExistException(string.Format("Email already exist! {0}", email)); } } else { throw new UserAlreadyExistException(string.Format("User name already exist! {0}", userName)); } } } else { logger.Warning("Attempt to register invalid email: {0}! Username: {1}.", email, userName); } }
public User ConfirmEmail(string guid) { User ret; using (var ctx = new StepMapDbContext()) { UserConfirmation userConfirmation; try { userConfirmation = ctx.UserConfirmations.Single(uc => uc.ConfirmationGuid == guid); } catch (InvalidOperationException ex) { throw new ConfirmationGuidNotValidException(string.Format("Unkown guid: {0}!", guid), ex); } ret = userConfirmation.User; userConfirmation.User.UserState = UserState.Active; ctx.UserConfirmations.Remove(userConfirmation); ctx.SaveChanges(); } return ret; }
public void DALTest3() { using (StepMapDbContext ctx = new StepMapDbContext()) { User u = new User(); u.Email = "*****@*****.**"; u.LastLogin = DateTime.UtcNow; u.Name = "test user"; u.PasswordHash = "hash"; u.UserRole = UserRole.Member; u.UserState = UserState.Active; u = ctx.Users.Add(u); ctx.SaveChanges(); UserConfirmation uc = new UserConfirmation(); uc.User = u; uc.ConfirmationGuid = "guid"; uc = ctx.UserConfirmations.Add(uc); ctx.SaveChanges(); try { UserConfirmation du = ctx.UserConfirmations.Single(tu => tu.ConfirmationGuid == "guid"); Assert.AreEqual(u.Id, du.User.Id); } finally { ctx.UserConfirmations.Remove(uc); ctx.Users.Remove(u); ctx.SaveChanges(); } } }
public void CheckProjectProgress(Project project) { using (var ctx = new StepMapDbContext()) { Step currentStep = project.FinishedSteps.Last(); if (currentStep.Deadline < DateTime.UtcNow) { project.BadPoint++; if (!currentStep.SentReminders.Any()) { SentFirstReminder(project.User, project, currentStep); ctx.Projects.Attach(project); } } ctx.SaveChanges(); } }
public void Login(User user) { using (var ctx = new StepMapDbContext()) { user = ctx.Users.Attach(user); if (user.UserState == UserState.Active) { user.LastLogin = DateTime.UtcNow; ctx.SaveChanges(); } else { throw new AccountIsNotActivatedException("Please confirm your email: " + user.Email); } } }