public void Update(User userParam, string password = null) { var user = _context.GetRepository <User>().GetList().Items.Single(x => x.Id == userParam.Id); if (user == null) { throw new AppException("User not found"); } if (userParam.Username != user.Username) { // username has changed so check if the new username is already taken if (_context.GetRepository <User>().GetList().Items.Any(x => x.Username == userParam.Username)) { throw new AppException("Username " + userParam.Username + " is already taken"); } } // update user properties user.FirstName = userParam.FirstName; user.LastName = userParam.LastName; user.Username = userParam.Username; // update password if it was entered if (!string.IsNullOrWhiteSpace(password)) { byte[] passwordHash, passwordSalt; UserUtil.CreatePasswordHash(password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; } _context.GetRepository <User>().Update(user); _context.SaveChanges(); }
public User Create(User user, string password) { // validation if (string.IsNullOrWhiteSpace(password)) { throw new AppException("Password is required"); } if (_context.GetRepository <User>().GetList().Items.Any(x => x.Username == user.Username)) { throw new AppException("Username \"" + user.Username + "\" is already taken"); } byte[] passwordHash, passwordSalt; UserUtil.CreatePasswordHash(password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; _context.GetRepository <User>().Add(user); _context.SaveChanges(); return(user); }