public ActionResult RestoreBlock(string login)
        {
            var user = DB.Users.FirstOrDefault(
                x =>
                x.Email.ToLower() == login.ToLower() || x.Name.ToLower() == login.ToLower() ||
                x.Phone == login.ToLower() || x.Nick.ToLower() == login.ToLower());

            if (user == null)
            {
                ViewBag.Error = "Такой пользователь не зарегистрирован в системе.";
            }
            else
            {
                var    confirmKey = WebSecurity.GeneratePasswordResetToken(user.Name);
                string link       = Url.Action("ChangePass", "Home", new { key = confirmKey });

                NotifyMail.SendNotify("ForgotPassword", user.Email,
                                      format => string.Format(format, HostName),
                                      format => string.Format(format, HostName, link)
                                      );


                ViewBag.Message = "На вашу электронную почту отправлено письмо с инструкцией по восстановлению пароля";
            }
            return(PartialView());
        }
        public ActionResult Restore(LoginModel model)
        {
            if (model.Email.IsNullOrEmpty())
            {
                model.Message = "Неверно указан e-mail";
            }
            else
            {
                var user =
                    DB.Users.FirstOrDefault(
                        x => x.Email.ToLower() == model.Email.ToLower() || x.Name.ToLower() == model.Email.ToLower());
                if (user == null)
                {
                    model.Message = "Неверно указан e-mail";
                }
                else
                {
                    var newPass = new Random(DateTime.Now.Millisecond).GeneratePassword();
                    var token   = WebSecurity.GeneratePasswordResetToken(user.Name);
                    WebSecurity.ResetPassword(token, newPass);
                    NotifyMail.SendNotify("ForgotPassword", user.Email,
                                          format => string.Format(format, HostName),
                                          format => string.Format(format, user.Email, newPass, HostName));

                    model.Message = "Новый пароль отправлен на ваш E-mail";
                }
            }
            return(PartialView(model));
        }
Beispiel #3
0
        public ActionResult Register(UserView userView)
        {
            if (userView.Captcha != (string)Session[CaptchaImage.CaptchaValueKey])
            {
                ModelState.AddModelError("Captcha", "Текст с картинки введен неверно");
            }
            var anyUser = Repository.Users.Any(p => string.Compare(p.Email, userView.Email) == 0);

            if (anyUser)
            {
                ModelState.AddModelError("Email", "Пользователь с таким email уже зарегистрирован");
            }
            if (ModelState.IsValid)
            {
                var user = (User)ModelMapper.Map(userView, typeof(UserView), typeof(User));
                Repository.CreateUser(user);

                NotifyMail.SendNotify("Register", user.Email,
                                      subject => string.Format(subject, HostName),
                                      body => string.Format(body, "", HostName));

                return(RedirectToAction("Index"));
            }
            return(View(userView));
        }
 public ActionResult ForgotPassword(ForgotPasswordView forgotPasswordView)
 {
     if (ModelState.IsValid)
     {
         var user =
             Repository.Users.FirstOrDefault(p => string.Compare(p.Email, forgotPasswordView.Email, true) == 0);
         if (user != null)
         {
             NotifyMail.SendNotify("ForgotPassword", user.Email,
                                   format => string.Format(format, HostName),
                                   format => string.Format(format, user.Email, user.Password, HostName));
             return(View("ForgotPasswordSuccess"));
         }
         ModelState.AddModelError("Email", "Email user not found");
     }
     return(View(forgotPasswordView));
 }
        public ActionResult Register(RegisterUserView registerUserView)
        {
            if (Session[CaptchaImage.CaptchaValueKey] as string != registerUserView.Captcha)
            {
                ModelState.AddModelError("Captcha", "Numbers not correct");
            }
            if (ModelState.IsValid)
            {
                var user = (User)ModelMapper.Map(registerUserView, typeof(RegisterUserView), typeof(User));
                Repository.CreateUser(user);

                NotifyMail.SendNotify("Register", user.Email,
                                      format => string.Format(format, HostName),
                                      format => string.Format(format, user.ActivatedLink, HostName));

                return(View("RegisterSuccess", user));
            }
            return(View(registerUserView));
        }
        public ActionResult EditPlace(Place place, HttpPostedFileBase Logo, HttpPostedFileBase Image, FormCollection collection)
        {
            if (collection["Save"].IsFilled())
            {
                bool needCreateModer = false;
                place.IsPost = true;
                if (/*Logo == null || Logo.ContentLength == 0 || Image == null || Image.ContentLength == 0 ||*/
                    place.Name.IsNullOrEmpty() || place.Adress.IsNullOrEmpty() || place.Description.IsNullOrEmpty() ||
                    !place.MinPay.HasValue || !place.MaxPay.HasValue ||
                    place.Type.IsNullOrEmpty() || (place.WishToBeAdmin && !place.AdminMail.IsMailAdress()))
                {
                    return(View(place));
                }

                if (place.ID > 0)
                {
                    var p = DB.Places.First(x => x.ID == place.ID);
                    needCreateModer = p.AdminMail != place.AdminMail && place.AdminMail.IsMailAdress() && place.WishToBeAdmin;
                    p.LoadPossibleProperties(place, new[] { "Logo", "Approved" });
                    place = p;
                    if (!needCreateModer)
                    {
                        place.AdminMail = "";
                    }
                }


                if (!(Logo == null || Logo.ContentLength == 0))
                {
                    string fileNameLogo = Guid.NewGuid() + Path.GetExtension(Logo.FileName);
                    string pathLogo     = "/Content/Places/" + fileNameLogo;
                    Logo.SaveAs(Server.MapPath(pathLogo));
                    place.Logo = pathLogo;
                }
                if (!(Image == null || Image.ContentLength == 0))
                {
                    string fileNameImage = Guid.NewGuid() + Path.GetExtension(Image.FileName);
                    string pathImage     = "/Content/Places/" + fileNameImage;
                    Image.SaveAs(Server.MapPath(pathImage));
                    DB.PlaceImages.InsertOnSubmit(new PlaceImage()
                    {
                        Path = pathImage, Place = place
                    });
                }
                var isNew = false;
                if (place.ID == 0)
                {
                    isNew           = true;
                    needCreateModer = place.AdminMail.IsMailAdress() && place.WishToBeAdmin;
                    DB.Places.InsertOnSubmit(place);
                }

                if (needCreateModer)
                {
                    var u = DB.Users.FirstOrDefault(x => x.Email.ToLower().Trim() == place.AdminMail.ToLower().Trim());
                    if (u == null)
                    {
                        var dict = new Dictionary <string, object>();

                        var pass = new Random(DateTime.Now.Millisecond).GeneratePassword();
                        dict.Add("UserName", place.AdminMail);
                        dict.Add("Email", place.AdminMail);
                        MembershipProvider.CreateUserAndAccount(place.AdminMail, pass, false, dict);
                        RoleProvider.AddUsersToRoles(new[] { place.AdminMail }, new[] { "Client", "Moderator" });
                        NotifyMail.SendNotify("PlaceAdminCreate", place.AdminMail,
                                              format => string.Format(format, HostName),
                                              format => string.Format(format, place.AdminMail, pass, HostName)
                                              );

                        u = DB.Users.First(x => x.Email.ToLower().Trim() == place.AdminMail.ToLower().Trim());

                        var exist = DB.PlaceAdmins.FirstOrDefault(x => x.PlaceID == place.ID && x.UserID == u.ID);
                        if (exist == null)
                        {
                            exist = new PlaceAdmin()
                            {
                                PlaceID = place.ID, UserID = u.ID
                            };
                            DB.PlaceAdmins.InsertOnSubmit(exist);
                        }
                    }
                    else
                    {
                        NotifyMail.SendNotify("PlaceAdminExist", place.AdminMail,
                                              format => string.Format(format, HostName),
                                              format => string.Format(format, "", "", HostName)
                                              );


                        if (u.webpages_UsersInRoles.All(x => x.webpages_Role.RoleName != "Moderator"))
                        {
                            RoleProvider.AddUsersToRoles(new[] { u.Name }, new[] { "Moderator" });
                        }

                        var exist = DB.PlaceAdmins.FirstOrDefault(x => x.PlaceID == place.ID && x.UserID == u.ID);
                        if (exist == null)
                        {
                            exist = new PlaceAdmin()
                            {
                                PlaceID = place.ID, UserID = u.ID
                            };
                            DB.PlaceAdmins.InsertOnSubmit(exist);
                        }
                    }
                }
                else if (place.PlaceAdmins.Any())
                {
                    DB.PlaceAdmins.DeleteAllOnSubmit(place.PlaceAdmins);
                }

                DB.SubmitChanges();
                return(isNew ? RedirectToAction("Places") : RedirectToAction("EditPlace", new { ID = place.ID, FromSave = 1 }));
            }
            else
            {
                if (place.ID == 0)
                {
                    return(RedirectToAction("Places"));
                }
                var p = DB.Places.First(x => x.ID == place.ID);
                if (!p.Approved)
                {
                    p.Approved = true;

                    if (p.AdminMail.IsMailAdress())
                    {
                        var user = DB.Users.FirstOrDefault(x => x.Email.ToLower().Trim() == p.AdminMail.ToLower().Trim());
                        if (user == null)
                        {
                            var dict = new Dictionary <string, object>();

                            var pass = new Random(DateTime.Now.Millisecond).GeneratePassword();
                            dict.Add("UserName", p.AdminMail);
                            dict.Add("Email", p.AdminMail);
                            MembershipProvider.CreateUserAndAccount(p.AdminMail, pass, false, dict);
                            RoleProvider.AddUsersToRoles(new[] { p.AdminMail }, new[] { "Client", "Moderator" });
                            NotifyMail.SendNotify("PlaceAdminCreate", p.AdminMail,
                                                  format => string.Format(format, HostName),
                                                  format => string.Format(format, p.AdminMail, pass, HostName)
                                                  );

                            user = DB.Users.First(x => x.Email.ToLower().Trim() == p.AdminMail.ToLower().Trim());

                            var exist = DB.PlaceAdmins.FirstOrDefault(x => x.PlaceID == p.ID && x.UserID == user.ID);
                            if (exist == null)
                            {
                                exist = new PlaceAdmin()
                                {
                                    PlaceID = p.ID, UserID = user.ID
                                };
                                DB.PlaceAdmins.InsertOnSubmit(exist);
                            }
                        }
                        else
                        {
                            NotifyMail.SendNotify("PlaceAdminExist", p.AdminMail,
                                                  format => string.Format(format, HostName),
                                                  format => string.Format(format, "", "", HostName)
                                                  );


                            if (user.webpages_UsersInRoles.All(x => x.webpages_Role.RoleName != "Moderator"))
                            {
                                RoleProvider.AddUsersToRoles(new[] { user.Name }, new[] { "Moderator" });
                            }

                            var exist = DB.PlaceAdmins.FirstOrDefault(x => x.PlaceID == p.ID && x.UserID == user.ID);
                            if (exist == null)
                            {
                                exist = new PlaceAdmin()
                                {
                                    PlaceID = p.ID, UserID = user.ID
                                };
                                DB.PlaceAdmins.InsertOnSubmit(exist);
                            }
                        }
                    }
                }
                else
                {
                    p.Approved = false;
                    if (p.AdminMail.IsMailAdress())
                    {
                        NotifyMail.SendNotify("PlaceAdminBlock", p.AdminMail,
                                              format => string.Format(format, HostName),
                                              format => string.Format(format, "", "", HostName, p.Name)
                                              );
                    }
                }
                DB.SubmitChanges();
                return(RedirectToAction("Places"));
            }
        }
        public ActionResult RegBlockPartner(RegisterModelPartnerStep1 model)
        {
            model.IsPost = true;
            if (!model.Agree || model.Name.IsNullOrEmpty() || model.Surname.IsNullOrEmpty() || !model.Email.IsMailAdress() || model.Phone.IsNullOrEmpty())
            {
                return(PartialView(model));
            }


            var    rand    = new Random(DateTime.Now.Millisecond);
            var    pass    = rand.GeneratePassword();
            string digitID = "";

            for (int i = 0; i < 500; i++)
            {
                var digit = rand.Next(100000000, 999999999);
                if (!DB.Users.Any(x => x.DigitID == digit.ToString()))
                {
                    digitID = digit.ToString();
                    break;
                }
            }


            var confirmKey = Guid.NewGuid();


            string link = Url.Action("CheckKey", "Home", new { key = confirmKey });

            var message = NotifyMail.SendNotify("Register", model.Email,
                                                format => string.Format(format, HostName),
                                                format => string.Format(format, HostName, link)
                                                );

            if (message.IsNullOrEmpty())
            {
                var dict = new Dictionary <string, object>();
                dict.Add("UserName", model.Name);
                dict.Add("UserSurname", model.Surname);
                dict.Add("Email", model.Email);
                dict.Add("UserPatrinomic", model.Patrinomic);
                dict.Add("Phone", model.Phone);
                dict.Add("RegStep", 1);
                dict.Add("IsPhoneConfirmed", false);
                dict.Add("DigitID", digitID);
                dict.Add("ConfirmKey", confirmKey);

                try
                {
                    MembershipProvider.CreateUserAndAccount(model.Email, pass, false, dict);
                }
                catch
                {
                    model.Email   = "";
                    model.Message = "Пользователь с таким E-mail уже зарегистрирован.";
                    return(PartialView(model));
                }
                RoleProvider.AddUsersToRoles(new[] { model.Email }, new[] { "ShopOwner" });
                var user = DB.Users.FirstOrDefault(x => x.Email == model.Email);
                if (user != null)
                {
                    Logger.WriteEvent(Logger.EventType.UserRegister, "Регистрация в системе", user.ID);
                }
                model.Message =
                    "На указанный Вами электронный адрес было выслано письмо<br>Пожалуйста, перейдите по ссылке из письма для продолжения регистрации в системе";
            }
            else
            {
                model.Message = message;
            }
            return(PartialView(model));
        }
        public ActionResult Register(RegisterModel model, HttpPostedFileBase Photo)
        {
            model.IsPost = true;
            if (Photo != null && Photo.ContentLength > 0)
            {
                model.Photo = Photo.FileName;
            }

            if (model.Email.IsFilled())
            {
                var exist = DB.Users.Where(x => x.Name.ToLower() == model.Email.ToLower() || x.Email.ToLower() == model.Email.ToLower());
                if (exist.Any())
                {
                    model.Email = null;
                }
            }

            if (!model.Day.HasValue || !model.Month.HasValue || !model.Year.HasValue || !model.UserName.IsFilled() || !model.Town.HasValue || !model.Email.IsFilled() || !model.Email.IsMailAdress() || model.Password.IsNullOrEmpty() /*|| Photo == null || Photo.ContentLength == 0*/)
            {
                return(View(model));
            }

            DateTime date;

            try
            {
                date = new DateTime(model.Year.Value, model.Month.Value, model.Day.Value);
            }
            catch
            {
                model.Month = null;
                model.Day   = null;
                return(View(model));
            }

            /*var pass = new Random(DateTime.Now.Millisecond).GeneratePassword();*/
            var dict = new Dictionary <string, object>();

            dict.Add("BirthDate", date);
            dict.Add("TownID", model.Town);
            dict.Add("Email", model.Email);
            dict.Add("Sex", model.Sex);

            var name    = "";
            var surname = "";
            var ar      = model.UserName.Split <string>(" ");

            name = ar.ElementAt(0);
            if (ar.Count() > 1)
            {
                surname = ar.ElementAt(1);
            }

            dict.Add("UserName", name);
            dict.Add("UserSurname", surname);
            MembershipProvider.CreateUserAndAccount(model.Email, model.Password, false, dict);
            RoleProvider.AddUsersToRoles(new[] { model.Email }, new[] { "Client" });

            var user = DB.Users.FirstOrDefault(x => x.Email == model.Email);

            if (user != null && Photo != null && Photo.ContentLength != 0)
            {
                string fileName = Guid.NewGuid() + Path.GetExtension(Photo.FileName);
                string path     = "/Content/Avatars/" + fileName;
                Photo.SaveAs(Server.MapPath(path));

                DB.UserPhotos.InsertOnSubmit(new UserPhoto()
                {
                    Path = path, UserID = user.ID, IsAvatar = true
                });
                DB.SubmitChanges();
            }


            WebSecurity.Login(model.Email, model.Password, true);



            NotifyMail.SendNotify("Register", model.Email,
                                  format => string.Format(format, HostName),
                                  format => string.Format(format, model.Email, model.Password, HostName)
                                  );



            return(RedirectToAction("Index", "Cabinet"));


            /*
             *          NotifyMail.SendNotify("ForgotPassword", user.Email,
             *                          format => string.Format(format, HostName),
             *                          format => string.Format(format, user.Email, user.Password, HostName));
             */
        }