Example #1
0
        public ActionResult RegisterSubmit(ABUserAuth model)
        {
            if (String.IsNullOrEmpty(model.Email) ||
                String.IsNullOrEmpty(model.Password) ||
                String.IsNullOrEmpty(model.ConfirmPassword) ||
                String.IsNullOrEmpty(model.FirstName) ||
                String.IsNullOrEmpty(model.LastName) ||
                String.IsNullOrEmpty(model.Country) ||
                String.IsNullOrEmpty(model.MaHC) ||
                String.IsNullOrEmpty(model.PostalCode) ||
                String.IsNullOrEmpty(model.Phone))
            {
                ViewBag.Error = "Please enter all required fields.";

                return(View("Register", model));
            }

            // get the country
            var c = Db.Select <Country>(x => x.Where(m => m.Code == model.Country).Limit(1)).FirstOrDefault();

            if (c == null)
            {
                ViewBag.Error = "Your selected country is not found";
                return(View("Register", model));
            }

            // validate the phone number
            if (!IsValidPhoneByCountry(model.Phone, c.Code, true))
            {
                ViewBag.Error = "We can not validate your phone number with your selected country.";
                return(View("Register", model));
            }

            if (!IsValidEmailAddress(model.Email))
            {
                ViewBag.Error = "We can not validate your email address format.";
                return(View("Register", model));
            }

            if (model.Password != model.ConfirmPassword)
            {
                ViewBag.Error = "Please enter same Password and Re password fields.";

                return(View("Register", model));
            }

            if (!new Regex(@"(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$", RegexOptions.Compiled).IsMatch(model.Password))
            {
                ViewBag.Error = "Password must contain at least 8 characters, including uppercase/lowercase and numbers";

                return(View("Register", model));
            }

            //if (!captchaValid)
            //{
            //    ViewBag.Error = "Your captcha is not match.";

            //    return View("Register", model);
            //}

            if (User_GetByEmail(model.Email) != null)
            {
                ViewBag.Error = "There is an user with same Email as you entered. Please use difference Email.";

                return(View("Register", model));
            }

            if (User_GetByUsername(model.UserName) != null)
            {
                ViewBag.Error = "There is an user with same Username as you entered. Please use difference Username.";

                return(View("Register", model));
            }

            var p = PasswordGenerate(model.Password);

            ABUserAuth user = new ABUserAuth()
            {
                Email    = model.Email,
                UserName = model.Email,
                Roles    = new List <string>()
                {
                    RoleEnum.Customer.ToString()
                },
                PasswordHash  = p.Id,
                Salt          = p.Name,
                FirstName     = model.FirstName,
                LastName      = model.LastName,
                Country       = model.Country,
                MaHC          = model.MaHC,
                PostalCode    = model.PostalCode,
                Phone         = model.Phone,
                DigestHa1Hash = encrypt.GetMD5HashData(model.Email),
                CreatedDate   = DateTime.Now,
                ActiveStatus  = true,
            };

            user.FullName    = user.FirstName + " " + user.LastName;
            user.DisplayName = user.FullName;

            try
            {
                Db.Insert <ABUserAuth>(user);

                user.Id = (int)Db.GetLastInsertId();

                var template = Get_MaillingListTemplate("register_notify_user");

                var template_helper = new EmailHelper(template.Title, template.Body);

                template_helper.Parameters.Add("Host", CurrentWebsite.Domain.First());

                template_helper.Parameters.Add("User", user.UserName);

                template_helper.Parameters.Add("Code", user.DigestHa1Hash);

                template_helper.Sender_Email = CurrentWebsite.Email_Support;

                template_helper.Sender_Name = CurrentWebsite.Name;

                template_helper.Receiver.Add(user.Email);

                SendMail(template_helper);

                template = Get_MaillingListTemplate("register_notify_admin");

                template_helper = new EmailHelper(template.Title, template.Body);

                template_helper.Parameters.Add("Host", InternalService.CurrentWebsiteDomainURL);

                template_helper.Parameters.Add("Id", user.Id.ToString());

                template_helper.Parameters.Add("User", user.UserName);

                template_helper.Parameters.Add("Email", user.Email);

                template_helper.Parameters.Add("Date", DateTime.Now.ToString());

                template_helper.Sender_Email = CurrentWebsite.Email_Support;

                template_helper.Sender_Name = CurrentWebsite.Name;

                template_helper.Receiver.Add(CurrentWebsite.Email_Admin);

                SendMail(template_helper);

                ViewBag.Message = "Your Account has been created! We just sent to you one email to confirm your account information. Please make sure to check your spam folder in your mail box. <br>Photobookmart also login for you automatically. Enjoy...";

                // do the auto login
                //return SignInSubmit(new LoginModel() { CheckRemember = true, Pass = model.Password, RedirectTo = model.RedirectTo, UserName = model.UserName });
                var authResponse = AuthService.Post(new Auth
                {
                    UserName = model.Email,
                    Password = model.Password,
                    Continue = ""
                });
            }
            catch (Exception ex)
            {
                ViewBag.RedirectTo = Url.Action("Register", "User", new { });

                ViewBag.Message = string.Format("{0}: {1}.", "There was an error when registering", ex.Message);
            }

            if (!string.IsNullOrEmpty(model.RedirectTo))
            {
                //ViewBag.RedirectTo = Url.Action("SignIn", new { redirectTo = model.RedirectTo });
                ViewBag.RedirectTo = model.RedirectTo;
            }
            else
            {
                //ViewBag.RedirectTo = Url.Action("SignIn");
                ViewBag.RedirectTo = "/";
            }
            return(View("Message"));
        }