public async Task <IActionResult> Login(LoginModel model, [FromServices] IGenPasswordHash genPassHash) { if (!ModelState.IsValid) { return(View(model)); } AdministratorUser user = await db.AdministratorUsers.FirstOrDefaultAsync(u => u.Login == model.Login); if (user != null) { if (genPassHash.IsPasswordMathcingHash(model.Password, user.PasswordHash)) { await Authenticate(user.Id, user.Login, user.Tier); // аутентификация return(RedirectToAction("Products", "Products")); } else { return(RedirectToRoute("ErrorMessage", new { Message = "Password invalid", RedirectRoute = "AdminLogin" })); } } else { return(RedirectToRoute("ErrorMessage", new { Message = $"Can't find user: {model.Login}", RedirectRoute = "AdminLogin" })); } }
public async void Edit() { string lastName = "reallyNotOleg"; fixture.db.AdministratorUsers.Add(new AdministratorUser { Login = "******", Name = "notoleg", LastName = "notoleg", PasswordHash = "n0701eG", Tier = 42 }); fixture.db.SaveChanges(); AdministratorUser user = await fixture.db.AdministratorUsers.AsNoTracking().FirstOrDefaultAsync(p => p.Login == "notoleg"); user.LastName = lastName; await controller.Edit(user); AdministratorUser actual = await fixture.db.AdministratorUsers.AsNoTracking().FirstOrDefaultAsync(p => p.Login == "notoleg"); Assert.Equal(lastName, actual.LastName); }
public async void Delete() { fixture.db.AdministratorUsers.Add(new AdministratorUser { Login = "******", Name = "ivan", LastName = "ivanov", PasswordHash = "1v@n", Tier = 42 }); fixture.db.AdministratorUsers.Add(new AdministratorUser { Login = "******", Name = "notivan", LastName = "notivanov", PasswordHash = "n071v@n", Tier = 42 }); fixture.db.SaveChanges(); AdministratorUser user = await fixture.db.AdministratorUsers.AsNoTracking().FirstOrDefaultAsync(p => p.Login == "ivan"); await controller.Delete(user.Id); AdministratorUser actual = await fixture.db.AdministratorUsers.FirstOrDefaultAsync(p => p.Login == "ivan"); Assert.Null(actual); }
public async Task <IActionResult> Edit(int accountId) { AdministratorUser user = await db.AdministratorUsers.SingleOrDefaultAsync(p => p.Id == accountId); ViewData["Tiers"] = await db.Tiers.ToListAsync(); return(View(user)); }
public async Task <IActionResult> Edit(AdministratorUser user) { AdministratorUser userFromDb = await db.AdministratorUsers.SingleOrDefaultAsync(p => p.Id == user.Id); userFromDb.Name = user.Name; userFromDb.LastName = user.LastName; userFromDb.Tier = user.Tier; await db.SaveChangesAsync(); await logger.AddToLogAsync($"Edited account ID:{user.Id}"); return(RedirectToAction("Accounts")); }
public AuthenticationServiceTests() { _userIdentityService = Substitute.For <IUserIdentityService>(); _nyssContext = Substitute.For <INyssContext>(); _user = new AdministratorUser { EmailAddress = UserEmail, Name = UserName }; _nyssContext.Users = new List <User> { _user }.AsQueryable().BuildMockDbSet(); _authenticationService = new AuthenticationService(_userIdentityService, _nyssContext); }
public async void Create() { RegisterModel regModel = new RegisterModel { Login = "******", Email = "*****@*****.**", Password = "******", PasswordCompare = "9oleg", Tier = 42 }; await controller.Create(fixture.genPassword, regModel); AdministratorUser user = fixture.db.AdministratorUsers.SingleOrDefault(p => p.Login == "oleg"); Assert.NotNull(user); }
public async Task <IActionResult> Delete(int accountId) { AdministratorUser user = await db.AdministratorUsers.SingleOrDefaultAsync(p => p.Id == accountId); if (user.Tier >= maxTier) { int maxTierUsers = await db.AdministratorUsers.Where(p => p.Tier >= maxTier).CountAsync(); if (maxTierUsers <= minimumMaxTierUsers) { return(RedirectToRoute("ErrorMessage", new { Message = $"Total number of users with Tier >= {maxTier} must be higher or equal to {minimumMaxTierUsers} after delete.", RedirectRoute = "AdminAccounts" })); } } db.AdministratorUsers.Remove(user); await db.SaveChangesAsync(); await logger.AddToLogAsync($"Deleted account ID:{accountId}"); return(RedirectToAction("Accounts")); }
public IHttpActionResult Register(AdministratorUser model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } int id = adminHandler.CheckEmailAvailability(model.Email); if (id == 0) { //administrator info var administrator = new AdministratorModel() { Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber, EmployeeNumber = model.employeeNumber, }; //password hash string mySalt = BCryptHelper.GenerateSalt(); string hashedPassword = BCryptHelper.HashPassword(model.Password, mySalt); adminHandler.Create(administrator, hashedPassword); return(Ok("Success")); } else { return(Ok("Email taken")); } } catch (Exception) { return(Ok("Something went wrong")); } }
/// <summary> /// Setup DataBase context for log storage /// </summary> /// <param name="db">EF DataBase context</param> public void Setup(BoorgerdbContext db) { this.db = db; userId = Int32.Parse(httpContext.HttpContext.User.Claims.First(x => x.Type == "UserId").Value); adminUser = db.AdministratorUsers.FirstOrDefault(u => u.Id == userId); }