public Users Register(Users user) { using (UnitOfWork uow = new UnitOfWork()) { Users existingUser = (uow.UserRepository.Find(u => u.Email == user.Email)).FirstOrDefault(); if (existingUser != null) { throw new ValidationException("Account email is already taken!"); } // user.DateCreated = DateTime.UtcNow; user.Role = Role.Admin; user.IsActive = true; user.Password = PasswordHelper.CreateHash(user.Password); user.DateModified = DateTime.UtcNow; user.DateCreated = DateTime.UtcNow; uow.UserRepository.Insert(user); uow.Save(); Users newUser = uow.UserRepository.GetById(user.Id); return(newUser); } }
public User Register(User clientModel) { using (UnitOfWork uow = new UnitOfWork()) { if (!string.IsNullOrEmpty(clientModel.Username) && !string.IsNullOrEmpty(clientModel.Password)) { List <User> existingUsers = uow.UserRepository.Find(u => u.Username == clientModel.Username || u.Email == clientModel.Email); if (existingUsers.Any()) { throw new ValidationException("Account is already taken!"); } } User userDb = new User(); userDb.FirstName = clientModel.FirstName; userDb.LastName = clientModel.LastName; userDb.Username = clientModel.Username; userDb.Email = clientModel.Email; userDb.Password = PasswordHelper.CreateHash(clientModel.Password); userDb.Admin = false; userDb.Image = clientModel.Image; userDb.DateCreated = DateTime.UtcNow; uow.UserRepository.Insert(userDb); uow.Save(); return(userDb); } }
public void AddProfessor(UserModel user) { using (var uow = new UnitOfWork()) { if (!string.IsNullOrEmpty(user.Email)) { var existingUsers = uow.UserRepository.Find(u => u.Email == user.Email); if (existingUsers != null) { throw new ValidationException("Email is already taken!"); } } var newUser = new User { DateCreated = DateTime.UtcNow, Email = user.Email, FirstName = user.FirstName, LastName = user.LastName, Role = (int)Role.Professor, }; var pass = PasswordHelper.RandomPasswordGenerator(8); newUser.Password = PasswordHelper.CreateHash(pass); var cert = CertHelper.GenerateCertificate(); newUser.Cert.Add(new Cert { Active = true, CertNumber = cert.SerialNumber }); uow.UserRepository.Add(newUser); uow.Save(); try { SmtpClient client = new SmtpClient("smtp.gmail.com") { UseDefaultCredentials = false, EnableSsl = true, Credentials = new NetworkCredential("*****@*****.**", "Mementomori1!") }; MailMessage mailMessage = new MailMessage { From = new MailAddress("*****@*****.**", "Subjector Customer Support") }; mailMessage.To.Add(user.Email); mailMessage.Attachments.Add(new Attachment(cert.Path)); mailMessage.Body = $"Hello {user.FirstName} {user.LastName}, \n \n You have been added into Subjector project. \n You will need to install certificate we've sent you as attachment. \n \n Here is your password: {pass} \n \n We would advice you to change password when you log in."; mailMessage.Subject = "Subjector invitation"; Task.Run(() => client.Send(mailMessage)); } catch (Exception e) { throw new ValidationException(e.Message); } } }
public void ChangePassword(int currentUserId, string currentPassword, string newPassword) { using (var uow = new UnitOfWork()) { var user = uow.UserRepository.Get(currentUserId); ValidationHelper.ValidateNotNull(user); if (!PasswordHelper.ValidatePassword(currentPassword, user.Password)) { throw new ValidationException("Incorrect current password!"); } user.Password = PasswordHelper.CreateHash(newPassword); uow.Save(); } }
public Users NewPasswordUpdate(int userId, string password) { using (UnitOfWork uow = new UnitOfWork()) { Users userFromDb = uow.UserRepository.GetById(userId); ValidationHelper.ValidateNotNull(userFromDb); if (!string.IsNullOrWhiteSpace(password)) { userFromDb.Password = PasswordHelper.CreateHash(password); uow.UserRepository.Update(userFromDb); uow.Save(); } return(userFromDb); } }
public User Register(User user) { using (var uow = new UnitOfWork()) { var userExisits = uow.UserRepository.Any(a => a.Username.ToLower() == user.Username.Trim().ToLower()); if (userExisits) { throw new ValidationException("Username already exists!"); } user.Password = PasswordHelper.CreateHash(user.Password); user.RoleId = (int)UserRole.Student; uow.UserRepository.Add(user); uow.Save(); return(user); } }
public User UpdateUserProfile(User client) { using (UnitOfWork uow = new UnitOfWork()) { User db = uow.UserRepository.Find(x => x.Username == client.Username).FirstOrDefault(); ValidationHelper.ValidateNotNull(db); db.FirstName = client.FirstName; db.LastName = client.LastName; db.Email = client.Email; db.Password = PasswordHelper.CreateHash(client.Password); db.Image = client.Image; uow.UserRepository.Update(db); uow.Save(); return(db); } }
public void AcceptRequest(int userId) { using (var uow = new UnitOfWork()) { var user = uow.UserRepository.Find(a => a.Id == userId); if (user == null) { throw new ValidationException("User doesn't exist"); } if (user.Role != (int)Role.PendingStudent) { throw new ValidationException("User is already active"); } user.Role = (int)Role.Student; var pass = PasswordHelper.RandomPasswordGenerator(8); user.Password = PasswordHelper.CreateHash(pass); try { SmtpClient client = new SmtpClient("smtp.gmail.com") { UseDefaultCredentials = false, EnableSsl = true, Credentials = new NetworkCredential("*****@*****.**", "Mementomori1!") }; MailMessage mailMessage = new MailMessage { From = new MailAddress("*****@*****.**", "Subjector Customer Support") }; mailMessage.To.Add(user.Email); mailMessage.Body = $"Hello {user.FirstName} {user.LastName}, \n \n your request to join Subjector has been approved. \n Here is your password: {pass} \n \n We would advice you to change password when you log in."; mailMessage.Subject = "Subjector approval"; client.Send(mailMessage); } catch (Exception e) { throw new ValidationException(e.Message); } uow.Save(); } }
public void ChangePassword(int memberId, string oldPassword, string newPassword) { using (UnitOfWork uow = new UnitOfWork()) { Users user = uow.UserRepository.GetById(memberId); ValidationHelper.ValidateNotNull(user); if (!PasswordHelper.ValidatePassword(oldPassword, user.Password)) { throw new ValidationException("Wrong email or password!"); } if (!string.IsNullOrWhiteSpace(newPassword)) { user.Password = PasswordHelper.CreateHash(newPassword); uow.UserRepository.Update(user); uow.Save(); } } }
public IActionResult Register([FromBody] RegistracijaRequest request) { var createPassword = PasswordHelper.CreateHash(request.Lozinka); var nastavnik = _mapper.Map <Nastavnik>(request); nastavnik.Lozinka = createPassword; if (string.IsNullOrWhiteSpace(nastavnik.Lozinka)) { return(new BadRequestObjectResult("Lozinka je obavezna!")); } if (_iNastavnikService.GetAll().Any(x => x.KorisnickoIme == request.KorisnickoIme)) { return(new BadRequestObjectResult("Korisnicko ime " + request.KorisnickoIme + " vec postoji!")); } _iNastavnikService.Insert(nastavnik); return(Ok()); }
public Users UpdateUser(Users modifiedUser) { using (UnitOfWork uow = new UnitOfWork()) { Users userFromDb = uow.UserRepository.GetById(modifiedUser.Id); ValidationHelper.ValidateNotNull(userFromDb); userFromDb.FirstName = modifiedUser.FirstName; userFromDb.LastName = modifiedUser.LastName; userFromDb.Password = modifiedUser.Password != null?PasswordHelper.CreateHash(modifiedUser.Password) : userFromDb.Password; userFromDb.Email = modifiedUser.Email; userFromDb.Username = modifiedUser.Username; userFromDb.DateModified = DateTime.UtcNow; uow.UserRepository.Update(userFromDb); uow.Save(); return(userFromDb); } }
private void UpdateUserInfoCore(UserInfoData user, User entity, AppDbContext db) { // update first and last name entity.FirstName = user.FirstName; entity.LastName = user.LastName; entity.ImageUrl = user.ImageUrl; // update the password if (!string.IsNullOrWhiteSpace(user.Password)) { var passwordData = PasswordHelper.CreateHash(user.Password); entity.PasswordSalt = passwordData.PasswordSalt; entity.PasswordHash = passwordData.PasswordHash; } // update the e-mail and check e-mail uniqueness if (db.Users.Any(u => u.Id != user.Id && u.Email == user.Email)) { throw new Exception($"The user with e-mail address '{user.Email}' already exists!"); } entity.Email = user.Email; }
public bool Insert(RegisterDTO register) { try { var password = PasswordHelper.CreateHash(register.Password); var patient = new DAL.Patient() { ChipId = register.ChipId, PasswordHash = password.PasswordHash, PasswordSalt = password.PasswordSalt }; patientMapper.Insert(patient); return(true); } catch { return(false); } }
public void Seed() { context.Database.Migrate(); if (!context.Users.Any()) { // create sample groups var prague = new Group { Name = "Group 1", Currency = "CZK" }; context.Groups.Add(prague); var brno = new Group { Name = "Group 2", Currency = "EUR" }; context.Groups.Add(brno); // create sample users var password1 = PasswordHelper.CreateHash("Pa$$w0rd"); var user1 = new User { FirstName = "John", LastName = "Newman", Email = "*****@*****.**", UserRole = UserRole.User, PasswordHash = password1.PasswordHash, PasswordSalt = password1.PasswordSalt }; context.Users.Add(user1); context.UserGroups.Add(new UserGroup() { User = user1, Group = prague }); var password2 = PasswordHelper.CreateHash("Pa$$w0rd"); var user2 = new User { FirstName = "John", LastName = "Smith", Email = "*****@*****.**", UserRole = UserRole.Admin, PasswordHash = password2.PasswordHash, PasswordSalt = password2.PasswordSalt }; context.Users.Add(user2); context.UserGroups.Add(new UserGroup() { User = user2, Group = prague }); context.UserGroups.Add(new UserGroup() { User = user2, Group = brno }); var password3 = PasswordHelper.CreateHash("Pa$$w0rd"); var user3 = new User { FirstName = "David", LastName = "Anderson", Email = "*****@*****.**", UserRole = UserRole.User, PasswordHash = password3.PasswordHash, PasswordSalt = password3.PasswordSalt }; context.Users.Add(user3); context.UserGroups.Add(new UserGroup() { User = user3, Group = prague }); context.UserGroups.Add(new UserGroup() { User = user3, Group = brno }); context.SaveChanges(); } }
protected override void Seed(DataAccess.Context.AppContext context) { if (!context.Users.Any()) { // create sample groups var prague = new Group { Name = "Lunches - Riganti Office", Currency = "CZK" }; context.Groups.Add(prague); var brno = new Group { Name = "Brno - Other Activities", Currency = "EUR" }; context.Groups.Add(brno); // create sample users var password1 = PasswordHelper.CreateHash("Pa$$w0rd"); var user1 = new User { FirstName = "John", LastName = "Newman", Email = "*****@*****.**", UserRole = UserRole.User, PasswordHash = password1.PasswordHash, PasswordSalt = password1.PasswordSalt }; context.Users.Add(user1); context.UserGroups.Add(new UserGroup() { User = user1, Group = prague }); var password2 = PasswordHelper.CreateHash("Pa$$w0rd"); var user2 = new User { FirstName = "John", LastName = "Smith", Email = "*****@*****.**", UserRole = UserRole.Admin, PasswordHash = password2.PasswordHash, PasswordSalt = password2.PasswordSalt }; context.Users.Add(user2); context.UserGroups.Add(new UserGroup() { User = user2, Group = prague }); context.UserGroups.Add(new UserGroup() { User = user2, Group = brno }); var password3 = PasswordHelper.CreateHash("Pa$$w0rd"); var user3 = new User { FirstName = "David", LastName = "Anderson", Email = "*****@*****.**", UserRole = UserRole.User, PasswordHash = password3.PasswordHash, PasswordSalt = password3.PasswordSalt }; context.Users.Add(user3); context.UserGroups.Add(new UserGroup() { User = user3, Group = prague }); context.UserGroups.Add(new UserGroup() { User = user3, Group = brno }); } }