public override async Task <AppDomainResult> Register([FromBody] RegisterModel register)
        {
            AppDomainResult appDomainResult = new AppDomainResult();

            if (ModelState.IsValid)
            {
                // Kiểm tra định dạng user name
                bool isValidUser = ValidateUserName.IsValidUserName(register.UserName);
                if (!isValidUser)
                {
                    throw new AppException("Vui lòng nhập số điện thoại hoặc email!");
                }

                var user = new Users()
                {
                    UserName     = register.UserName,
                    Password     = register.Password,
                    Created      = DateTime.Now,
                    CreatedBy    = register.UserName,
                    Active       = true,
                    Phone        = ValidateUserName.IsPhoneNumber(register.UserName) ? register.UserName : string.Empty,
                    Email        = ValidateUserName.IsEmail(register.UserName) ? register.UserName : string.Empty,
                    UserInGroups = new List <UserInGroups>(),
                };
                // Tạo mặc định trong group User
                var groupUserInfos = await userGroupService.GetAsync(e => e.Code == CoreContants.USER_GROUP);

                if (groupUserInfos != null && groupUserInfos.Any())
                {
                    UserInGroups userInGroups = new UserInGroups()
                    {
                        UserGroupId = groupUserInfos.FirstOrDefault().Id,
                        Created     = DateTime.Now,
                        CreatedBy   = register.UserName,
                        Deleted     = false,
                        Active      = true,
                    };
                    user.UserInGroups.Add(userInGroups);
                }

                // Kiểm tra item có tồn tại chưa?
                var messageUserCheck = await this.userService.GetExistItemMessage(user);

                if (!string.IsNullOrEmpty(messageUserCheck))
                {
                    throw new AppException(messageUserCheck);
                }
                user.Password           = SecurityUtils.HashSHA1(register.Password);
                appDomainResult.Success = await userService.CreateAsync(user);

                appDomainResult.ResultCode = (int)HttpStatusCode.OK;
            }
            else
            {
                var resultMessage = ModelState.GetErrorMessage();
                throw new AppException(resultMessage);
            }
            return(appDomainResult);
        }
Exemple #2
0
        public virtual async Task <AppDomainResult> ForgotPassword(string userName)
        {
            AppDomainResult appDomainResult = new AppDomainResult();
            bool            isValidEmail    = ValidateUserName.IsEmail(userName);
            bool            isValidPhone    = ValidateUserName.IsPhoneNumber(userName);

            // Kiểm tra đúng định dạng email và số điện thoại chưa
            if (!isValidEmail && !isValidPhone)
            {
                throw new AppException("Vui lòng nhập email hoặc số điện thoại!");
            }
            // Tạo mật khẩu mới
            // Kiểm tra email/phone đã tồn tại chưa?
            var userInfos = await this.userService.GetAsync(e => !e.Deleted &&
                                                            (e.UserName == userName ||
                                                             e.Email == userName ||
                                                             e.Phone == userName
                                                            )
                                                            );

            Users userInfo = null;

            if (userInfos != null && userInfos.Any())
            {
                userInfo = userInfos.FirstOrDefault();
            }
            if (userInfo == null)
            {
                throw new AppException("Số điện thoại hoặc email không tồn tại");
            }
            // Cấp mật khẩu mới
            var newPasswordRandom = RandomUtilities.RandomString(8);

            userInfo.Password = SecurityUtils.HashSHA1(newPasswordRandom);
            bool success = await this.userService.UpdateAsync(userInfo);

            if (success)
            {
                // Gửi mã qua Email
                if (isValidEmail)
                {
                    string emailBody = string.Format("<p>Your new password: {0}</p>", newPasswordRandom);
                    emailConfigurationService.Send("Change Password", new string[] { userInfo.Email }, null, null, new EmailContent()
                    {
                        Content = emailBody,
                        IsHtml  = true,
                    });
                }
                // Gửi SMS
                else if (isValidPhone)
                {
                }
            }
            return(appDomainResult);
        }
        /// <summary>
        /// Kiểm tra user đã tồn tại chưa?
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public override async Task <string> GetExistItemMessage(Users item)
        {
            List <string> messages     = new List <string>();
            string        result       = string.Empty;
            bool          isExistEmail = !string.IsNullOrEmpty(item.Email) && await Queryable.AnyAsync(x => !x.Deleted && x.Id != item.Id && x.Email == item.Email);

            bool isExistPhone = !string.IsNullOrEmpty(item.Phone) && await Queryable.AnyAsync(x => !x.Deleted && x.Id != item.Id && x.Phone == item.Phone);

            bool isExistUserName = !string.IsNullOrEmpty(item.UserName) &&
                                   await Queryable.AnyAsync(x => !x.Deleted && x.Id != item.Id &&
                                                            (x.UserName.Contains(item.UserName) ||
                                                             x.Email.Contains(item.UserName) ||
                                                             x.Phone.Contains(item.UserName)
                                                            ));

            bool isPhone = ValidateUserName.IsPhoneNumber(item.UserName);
            bool isEmail = ValidateUserName.IsEmail(item.UserName);

            if (isExistEmail)
            {
                messages.Add("Email đã tồn tại!");
            }
            if (isExistPhone)
            {
                messages.Add("Số điện thoại đã tồn tại!");
            }
            if (isExistUserName)
            {
                if (isPhone)
                {
                    messages.Add("Số điện thoại đã tồn tại!");
                }
                else if (isEmail)
                {
                    messages.Add("Email đã tồn tại!");
                }
                else
                {
                    messages.Add("User name đã tồn tại!");
                }
            }
            if (messages.Any())
            {
                result = string.Join(" ", messages);
            }
            return(result);
        }
Exemple #4
0
        public virtual async Task <AppDomainResult> Register([FromBody] RegisterModel register)
        {
            AppDomainResult appDomainResult = new AppDomainResult();

            if (ModelState.IsValid)
            {
                // Kiểm tra định dạng user name
                bool isValidUser = ValidateUserName.IsValidUserName(register.UserName);
                if (!isValidUser)
                {
                    throw new AppException("Vui lòng nhập số điện thoại hoặc email!");
                }

                var user = new Users()
                {
                    UserName  = register.UserName,
                    Password  = register.Password,
                    Created   = DateTime.Now,
                    CreatedBy = register.UserName,
                    Active    = true,
                    Phone     = ValidateUserName.IsPhoneNumber(register.UserName) ? register.UserName : string.Empty,
                    Email     = ValidateUserName.IsEmail(register.UserName) ? register.UserName : string.Empty,
                };
                // Kiểm tra item có tồn tại chưa?
                var messageUserCheck = await this.userService.GetExistItemMessage(user);

                if (!string.IsNullOrEmpty(messageUserCheck))
                {
                    throw new AppException(messageUserCheck);
                }
                user.Password           = SecurityUtils.HashSHA1(register.Password);
                appDomainResult.Success = await userService.CreateAsync(user);

                appDomainResult.ResultCode = (int)HttpStatusCode.OK;
            }
            else
            {
                var resultMessage = ModelState.GetErrorMessage();
                throw new AppException(resultMessage);
            }
            return(appDomainResult);
        }