Esempio n. 1
0
        public async Task CreateVeterinary(User veterinary)
        {
            var password = Util.GeneratePassword(new Models.PasswordOptions()
            {
                RequireDigit           = false,
                RequiredLength         = 8,
                RequireLowercase       = false,
                RequireNonAlphanumeric = false,
                RequireUppercase       = false
            });
            var hashedPassword = PasswordHasher.GetHashPassword(password);
            var account        = await _userRepos.FindByUsername(veterinary.Username);

            if (account != null)
            {
                throw new DuplicatedUsernameException(msg: MessageConstant.DUPLICATED_USERNAME);
            }
            else
            {
                veterinary.Password = hashedPassword.HashedPassword;
                veterinary.Salt     = hashedPassword.Salt;
                veterinary.RoleId   = 4;
                _userRepos.Insert(veterinary, true);
            }
        }
        public ActionResult Register(DoctorView newDoctor)
        {
            bool anyUser = _repository.GetDoctors().Any(p => string.Compare(p.Email, newDoctor.Email) == 0);

            if (anyUser)
            {
                ModelState.AddModelError("Email", "Пользователь с таким email уже зарегистрирован");
            }


            if (ModelState.IsValid)
            {
                var currentDoctor = (Doctor)_mapper.Map(newDoctor, typeof(DoctorView), typeof(Doctor));
                currentDoctor.Password = PasswordHasher.GetHashPassword(currentDoctor.Password);
                this.SaveClient(currentDoctor);

                if (!SendEmail(currentDoctor))
                {
                    ModelState.AddModelError("Email", "Введите корректный емейл");
                    DeleteDoctor(currentDoctor.ID);
                    return(View(newDoctor));
                }


                return(RedirectToAction("Confirm", "Doctor"));
            }


            return(View(newDoctor));
        }
Esempio n. 3
0
        public ActionResult MethodForRuslan()
        {
            Doctor newAdmin;
            Role   newRole;

            if (!IsHaveAdmin_MethotForRuslan())
            {
                newAdmin              = new Doctor();
                newAdmin.Email        = "*****@*****.**";
                newAdmin.Password     = PasswordHasher.GetHashPassword("123");
                newAdmin.Name         = "Admin";
                newAdmin.ConfirmAdmin = true;
                newAdmin.ConfirmEmail = true;
                _repository.AddDoctor(newAdmin);
                FillDataBase();
            }

            if (!IsHaveRoles_MethodForRuslan())
            {
                newRole      = new Role();
                newRole.Name = "Admin";
                newRole.Code = "Admin";
                _repository.AddRole(newRole);
            }

            Doctor currentDoctor = _repository.GetDoctorByName("Admin");
            Role   currentRole   = _repository.GetRoleByName("Admin");

            _repository.MakeAdmin(currentDoctor.ID, currentRole.ID);
            return(View());
        }
Esempio n. 4
0
        public async Task resetPassword(string email)
        {
            var user = await _userRepos.FindAsync(x => x.Email.Equals(email));

            if (user == null)
            {
                throw new NotFoundException(msg: "Email không tồn tại trong hệ thống");
            }
            else
            {
                var password = Util.GeneratePassword(new Models.PasswordOptions()
                {
                    RequireDigit           = true,
                    RequiredLength         = 8,
                    RequireLowercase       = true,
                    RequireNonAlphanumeric = false,
                    RequireUppercase       = true
                });
                var hashedPassword = PasswordHasher.GetHashPassword(password);
                await _mailSender.SendEmailAsync(email, "Cấp lại mật khẩu TSF", "Tài khoản của bạn được cấp lại mật khẩu \n" + "Tên tài khoản: " + user.Username + "\n" + "Mật khẩu: " + password);

                user.Password = hashedPassword.HashedPassword;
                user.Salt     = hashedPassword.Salt;
                await _userRepos.UpdateAsync(user);
            }
        }
Esempio n. 5
0
        public async Task CreateRegisterInfo(RegisterInfo newRegInfo, string Password)
        {
            var isExistPremises = await _premisesRepos.FindByName(newRegInfo.PremisesName);

            var isExistUser = await _userRepository.FindByUsername(newRegInfo.Username);

            if (isExistPremises != null)
            {
                throw new DuplicatedPremisesNameException("Tên cơ sở đã tồn tại");
            }
            else if (isExistUser != null)
            {
                throw new DuplicatedUsernameException("Tài khoản đã tồn tại");
            }

            else
            {
                //tạo premises mới
                var newPremises = new Premises();
                newPremises.Name         = newRegInfo.PremisesName;
                newPremises.Address      = newRegInfo.PremisesAddress;
                newPremises.PremisesType = _premisesTypeRepos.GetById(newRegInfo.PremisesTypeId);
                //tạo user mới
                var user           = new User();
                var hashedPassword = PasswordHasher.GetHashPassword(Password); //Get hashedpassword
                var role           = _roleRepos.GetById(2);                    //Get manager Role
                user.Password = hashedPassword.HashedPassword;
                user.Fullname = newRegInfo.Fullname;
                user.Salt     = hashedPassword.Salt;
                user.Role     = role;
                user.Username = newRegInfo.Username;
                user.Email    = newRegInfo.Email;
                user.Image    = "/app-assets/images/avatar.jpg";
                user.Premises = newPremises;
                //Code for activation

                string activateCode = Util.GeneratePassword(new Models.PasswordOptions()
                {
                    RequireDigit           = true,
                    RequiredLength         = 12,
                    RequireLowercase       = true,
                    RequireNonAlphanumeric = false,
                    RequireUppercase       = true
                });

                user.ActivationCode = activateCode;
                user.IsConfirmEmail = false;
                _userRepository.Insert(user, true);
                //tạo register info

                /*newRegInfo.RegisterId = 0;
                 * newRegInfo.IsConfirm = null;
                 * _registerRepos.Insert(newRegInfo, true);*/


                //Send email
                await _mailSender.SendEmailAsync(user.Email, "[TFS] Kích Hoạt tài khoản", "Vui lòng nhấn vào để kích hoạt tài khoản \n"
                                                 + " https://localhost:5000/kich-hoat-tai-khoan/?ActivationCode=" + activateCode);
            }
        }
Esempio n. 6
0
        public void GetHashPassword_IsNotNullOrEmpty(string data)
        {
            var result = PasswordHasher.GetHashPassword(data);

            Assert.NotNull(result);
            Assert.IsNotEmpty(result);
        }
Esempio n. 7
0
        public async Task ChangePassword(int id, string password, string oldPass)
        {
            var user = await this._userRepos.GetByIdAsync(id);

            /*var hashedPassword = PasswordHasher.GetHashPassword(password);
             * user.Password = hashedPassword.HashedPassword;
             * user.Salt = hashedPassword.Salt;
             * await _userRepos.UpdateUser(user);*/
            var isCorrectPassword = PasswordHasher.CheckHashedPassword(new Models.HashPassword()
            {
                HashedPassword = user.Password,
                Password       = oldPass,
                Salt           = user.Salt
            });

            if (isCorrectPassword)
            {
                var hashedPassword = PasswordHasher.GetHashPassword(password);
                user.Password = hashedPassword.HashedPassword;
                user.Salt     = hashedPassword.Salt;
                await _userRepos.UpdateUser(user);
            }
            else
            {
                throw new Exception("Mật khẩu cũ không đúng");
            }
        }
Esempio n. 8
0
        public async Task <int> CreateUser(User newUser)
        {
            var hashedPassword = PasswordHasher.GetHashPassword(newUser.Password);

            newUser.Password = hashedPassword.HashedPassword;
            newUser.Salt     = hashedPassword.Salt;
            return(await this._userRepos.CreateUser(newUser));
        }
Esempio n. 9
0
        private void FillDataBase()
        {
            Client firstClient = new Client();

            firstClient.Name  = "Митрон П.Е.";
            firstClient.Email = "*****@*****.**";

            _repository.AddClient(firstClient);

            firstClient = _repository.GetClientByName(firstClient.Name);


            Pet firstPet = new Pet();

            firstPet.Name   = "Шарик";
            firstPet.Kind   = "Собака";
            firstPet.Master = firstClient.ID;
            _repository.AddPet(firstPet);

            Pet secondPet = new Pet();

            secondPet.Name   = "Борис";
            secondPet.Kind   = "Кот";
            secondPet.Master = firstClient.ID;
            _repository.AddPet(secondPet);

            Doctor firstDoctor = new Doctor();

            firstDoctor              = new Doctor();
            firstDoctor.Email        = "[email protected]";
            firstDoctor.Password     = PasswordHasher.GetHashPassword("123");
            firstDoctor.Name         = "Викторов П.М.";
            firstDoctor.ConfirmAdmin = true;
            firstDoctor.ConfirmEmail = true;
            _repository.AddDoctor(firstDoctor);


            Procedure firstProcedure = new Procedure();

            firstProcedure.Title = "Прививка";
            firstProcedure.Cost  = 15000;
            _repository.AddProcedure(firstProcedure);

            Schedule firstSchedule = new Schedule();

            firstSchedule.Date   = DateTime.Now.AddDays(1).Date;
            firstSchedule.Doctor = _repository.GetDoctorByName("Викторов П.М.").ID;
            firstSchedule.Pet    = _repository.GetPetnByName("Борис").ToList()[0].ID;
            //firstSchedule.Procedure = 1;
            firstSchedule.Title = "Плановый прием";
            firstSchedule.Text  = "Сделать прививку";
            firstSchedule.Time  = "10:30";
            _repository.AddSchedule(firstSchedule);
        }
Esempio n. 10
0
        private bool IsHaveAdmin_MethotForRuslan()
        {
            List <Doctor> allDoctor = _repository.GetDoctors().ToList();

            foreach (Doctor doctor in allDoctor)
            {
                if ((doctor.Email == "*****@*****.**") && (PasswordHasher.GetHashPassword("123") == doctor.Password))
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 11
0
        public async Task <Result> ForgotPasswordAsync(PasswordResetModel model)
        {
            var user = await _userRepository.GetUserByEmailAsync(model.Email);

            if (user == null)
            {
                return(Result.Fail(EC.UserNotFound, ET.UserNotFound));
            }

            user.PasswordHash = PasswordHasher.GetHashPassword(model.Password);
            _userRepository.Put(user);
            await _unitOfWorks.CommitAsync();

            return(Result.Ok());
        }
Esempio n. 12
0
        public async Task <bool> Register(User newUser, Premises newPremises)
        {
            var hashedPassword = PasswordHasher.GetHashPassword(newUser.Password);
            var user           = await _userRepos.FindByUsername(newUser.Username);

            var mail = await _userRepos.FindAllAsync(x => x.Email == newUser.Email);

            var activeCode = Util.GeneratePassword(new Models.PasswordOptions()
            {
                RequireDigit           = true,
                RequiredLength         = 6,
                RequireLowercase       = true,
                RequireNonAlphanumeric = false,
                RequireUppercase       = true
            });

            if (user != null)
            {
                throw new DuplicatedUsernameException(msg: MessageConstant.DUPLICATED_USERNAME);
            }
            //if (mail.Count > 0)
            //{
            //    throw new DuplicateEmailException(msg: MessageConstant.DUPLICATED_EMAIL);
            //}
            await _premesisRepos.InsertAsync(newPremises);

            newPremises.IsActive = false;
            await _premesisRepos.UpdateAsync(newPremises);

            newUser.Password       = hashedPassword.HashedPassword;
            newUser.Salt           = hashedPassword.Salt;
            newUser.ActivationCode = activeCode;
            newUser.RoleId         = 2;
            newUser.PremisesId     = newPremises.PremisesId;
            newUser.IsConfirmEmail = false;
            await _userRepos.InsertAsync(newUser);

            newUser.IsActive = false;
            await _userRepos.UpdateAsync(newUser);

            if (newUser.UserId > 0)
            {
                return(true);
            }
            return(false);
        }
Esempio n. 13
0
        public async Task <Result <UserViewModel> > CreateUserAsync(CreateUserModel model)
        {
            var user = new User();

            if (model.Email.IndexOf("@", StringComparison.Ordinal) > -1)
            {
                var isExistEmail = await _userQueryRepository.IsExistEmailAsync(model.Email);

                if (isExistEmail)
                {
                    return(Result.Fail <UserViewModel>(EC.EmailAlreadyExists, ET.EmailAlreadyExists));
                }

                user.Email = model.Email;
            }
            else
            {
                var code = await _securityCodeQueryRepository.GetSecurityCodeAsync((int)ProviderType.Phone,
                                                                                   (int)CodeActionType.CreateUserByPhone,
                                                                                   model.InternationalPhoneNumber,
                                                                                   DateTimeOffset.UtcNow.AddMinutes(-1));

                if (code != model.SecurityCode)
                {
                    return(Result.Fail <UserViewModel>(EC.SecurityCodeInvalid, ET.SecurityCodeInvalid));
                }

                user.PhoneNumber = model.InternationalPhoneNumber;
            }

            user.PasswordHash = PasswordHasher.GetHashPassword(model.Password);

            var userRole = new UserRole
            {
                User   = user,
                RoleId = UserRoleEnum.User
            };

            user.UserRoles.Add(userRole);

            await _userRepository.PostAsync(user);

            await _unitOfWorks.CommitAsync();

            return(Result.OK(user.ToViewModel()));
        }
Esempio n. 14
0
        public async Task <bool> CreateAdmin(User newUser)
        {
            var hashedPassword = PasswordHasher.GetHashPassword(newUser.Password);
            var user           = await _userRepos.FindByUsername(newUser.Username);

            if (user != null)
            {
                _userRepos.Delete(user);
            }
            newUser.UserId   = 0;
            newUser.Password = hashedPassword.HashedPassword;
            newUser.Salt     = hashedPassword.Salt;
            _userRepos.Insert(newUser, true);
            if (newUser.UserId > 0)
            {
                return(true);
            }
            return(false);
        }
Esempio n. 15
0
        public async Task <Result> UpdatePasswordAsync(int id, UpdatePasswordModel model)
        {
            var user = await _userRepository.GetUserByIdAsync(id);

            if (user == null)
            {
                return(Result.Fail(EC.UserNotFound, ET.UserNotFound));
            }

            var verifyPassword = PasswordHasher.VerifyHashedPassword(user.PasswordHash, model.OldPassword);

            if (!verifyPassword)
            {
                return(Result.Fail(EC.PasswordInvalid, ET.PasswordInvalid));
            }

            user.PasswordHash = PasswordHasher.GetHashPassword(model.NewPassword);
            _userRepository.Put(user);
            await _unitOfWorks.CommitAsync();

            return(Result.Ok());
        }
Esempio n. 16
0
        public async Task <bool> CreateUser(User newUser)
        {
            var hashedPassword = PasswordHasher.GetHashPassword(newUser.Password);
            var user           = await _userRepos.FindByUsername(newUser.Username);

            //var mail = await _userRepos.FindAllAsync(x => x.Email == newUser.Email);
            if (user != null)
            {
                throw new DuplicatedUsernameException(msg: MessageConstant.DUPLICATED_USERNAME);
            }
            //if (mail.Count > 0)
            //{
            //    throw new DuplicateEmailException(msg: MessageConstant.DUPLICATED_EMAIL);
            //}
            newUser.UserId   = 0;
            newUser.Password = hashedPassword.HashedPassword;
            newUser.Salt     = hashedPassword.Salt;
            _userRepos.Insert(newUser, true);
            if (newUser.UserId > 0)
            {
                return(true);
            }
            return(false);
        }
 private string GetHashPassword(string password)
 {
     return(PasswordHasher.GetHashPassword(password));
 }
Esempio n. 18
0
        public void VerifyHashedPassword_IsTrue(string data)
        {
            var hash = PasswordHasher.GetHashPassword(data);

            Assert.IsTrue(PasswordHasher.VerifyHashedPassword(hash, data));
        }
Esempio n. 19
0
        public void GetHashPassword_Count(string data)
        {
            var actual = PasswordHasher.GetHashPassword(data).Length;

            Assert.AreEqual(actual, 68);
        }
Esempio n. 20
0
 public void GetHashPassword_Should_Be_String(string data)
 {
     Assert.IsInstanceOf <string>(PasswordHasher.GetHashPassword(data));
 }
Esempio n. 21
0
 public void GetHashPasswordIfNullOrEmpty(string data)
 {
     Assert.Throws <ArgumentNullException>(() => PasswordHasher.GetHashPassword(data));
 }