Beispiel #1
0
        public ActionResult Settings(UserModel model)
        {
            if (!LS.isLogined())
            {
                return(Redirect("~/"));
            }
            if (ModelState.IsValid)
            {
                var user = _db.Users.FirstOrDefault(x => x.ID == LS.CurrentUser.ID);
                if (user != null)
                {
                    user.FirstName   = model.FirstName;
                    user.LastName    = model.LastName;
                    user.CompanyName = model.CompanyName;
                    user.Phone       = model.Phone;
                    user.Email       = model.Email;
                    if (model.NewsLetter)
                    {
                        var nl = _db.Newsletters.FirstOrDefault(x => x.NewsletterEmail == model.Email);
                        if (nl == null)
                        {
                            Newsletter n = new Newsletter()
                            {
                                NewsletterAccept = true,
                                NewsletterDate   = DateTime.UtcNow,
                                NewsletterEmail  = user.Email,
                                NewsletterName   = user.FirstName + " " + user.LastName,
                                RoleDefault      = "Register"
                            };
                            _db.Newsletters.Add(n);
                            _db.SaveChanges();
                            SF.AddToNewsletter(n);
                        }
                    }
                    else
                    {
                        var nl = _db.Newsletters.FirstOrDefault(x => x.NewsletterEmail == model.Email);
                        if (nl != null)
                        {
                            _db.Newsletters.Remove(nl);
                            _db.SaveChanges();
                        }
                    }
                    _db.SaveChanges();
                    TempData["SuccessMessage"] = "Save Successful";
                    return(RedirectToAction("Settings"));
                }
            }

            TempData["ViewData"] = ViewData;
            return(RedirectToAction("Settings"));
        }
        public ActionResult _NewsletterAdd(Newsletter n, string InvisibleCaptchaValue)
        {
            if (!CaptchaController.IsInvisibleCaptchaValid(InvisibleCaptchaValue))
            {
                ModelState.AddModelError(string.Empty, "Captcha error.");
                SF.LogError("_NewsletterAdd Captcha error");
                return(Content("Error: Captcha", "text/html"));
            }

            if (ModelState.IsValid)
            {
                n.NewsletterDate = DateTime.Now;
                _db.Newsletters.Add(n);
                _db.SaveChanges();
                SF.AddToNewsletter(n);

                return(Content(RP.GetTextComponent("_NewsletterAdd"), "text/html"));
            }
            else
            {
                SF.LogError("_NewsletterAdd Model not valid");
                return(Content("Error: Model not valid", "text/html"));
            }
        }
        public ActionResult GoogleAjx(string ID, string Name, string Image, string Email)
        {
            // google 111929910329405075066
            // long   9223372036854775807
            if (!string.IsNullOrEmpty(ID) && ID != "0")
            {
                string email = Email;
                if (string.IsNullOrEmpty(email))
                {
                    email = ID + "@gmail.com.com";
                }


                User uLogin = _db.Users.FirstOrDefault(r => r.GoogleID == ID);
                if (uLogin != null)
                {
                    //login
                    LS.Authorize(uLogin);
                    // var messService = new MessageService(_db);
                    // messService.SendUserRegisterEmailToUser(u);
                    return(Json(new { result = "ok", url = "" }));
                }
                else
                {
                    //register

                    //generate code
                    var chars    = "0123456789abcdefghjklmnopqrstuwvxyzQAZWSXEDCRFVTGBYHNUJMIKLOP@$&%";
                    var random   = new Random();
                    var password = new string(
                        Enumerable.Repeat(chars, 8)
                        .Select(s => s[random.Next(s.Length)])
                        .ToArray());

                    User uExist = _db.Users.FirstOrDefault(r => r.UserName == email || r.Email == email);
                    if (uExist != null)
                    {
                        uExist.GoogleID = ID;

                        _db.SaveChanges();//update facebook ID
                        //login
                        LS.Authorize(uExist);

                        return(Json(new { result = "ok", url = "" }));
                    }
                    MembershipCreateStatus createStatus;
                    MembershipUser         newUser = Membership.CreateUser(email, password, email, "-", "-", true, out createStatus);

                    if (createStatus != MembershipCreateStatus.Success)
                    {
                        ModelState.AddModelError(string.Empty, RP.T("Account.Register.Error." + createStatus.ToString()).ToString());
                    }
                    else
                    {
                        User u = _db.Users.FirstOrDefault(r => r.UserName == email);
                        u.Roles     = SF.RolesStringAdd(u.Roles, "Register");
                        u.FirstName = "";
                        u.LastName  = "";
                        if (!string.IsNullOrEmpty(Name))
                        {
                            string[] firstLast = Name.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
                            if (firstLast.Length > 0)
                            {
                                u.FirstName = firstLast[0];
                            }
                            if (firstLast.Length > 1)
                            {
                                u.LastName = firstLast[1];
                            }
                        }
                        u.RoleDefault      = "Register";
                        u.GoogleID         = ID;
                        u.IsApproved       = true;
                        _db.Entry(u).State = EntityState.Modified;
                        _db.SaveChanges();

                        Newsletter n = new Newsletter()
                        {
                            NewsletterAccept = true,
                            NewsletterDate   = DateTime.UtcNow,
                            NewsletterEmail  = u.Email,
                            NewsletterName   = u.FirstName + " " + u.LastName,
                            RoleDefault      = "Register"
                        };
                        _db.Newsletters.Add(n);
                        _db.SaveChanges();
                        SF.AddToNewsletter(n);


                        if (Membership.ValidateUser(u.UserName, u.Password))
                        {
                            LS.Authorize(u);

                            var messService = new MessageService(_db);
                            messService.SendUserRegisterEmailToUser(u);
                            return(Json(new { result = "ok", url = "" }));
                        }
                        else
                        {
                            ModelState.AddModelError("", RP.T("Account.Logon.PasswordOrUserIncorrect").ToString());
                        }
                    }
                }
                return(Json(new { result = "error", message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));
            }
            return(Json(new { result = "error", message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));
        }
        public ActionResult RegAjx(RegisterAjxModel model)
        {
            if (ModelState.IsValid)
            {
                MembershipCreateStatus createStatus;
                MembershipUser         newUser = Membership.CreateUser(model.Email, model.Password, model.Email, "-", "-", true, out createStatus);

                if (createStatus != MembershipCreateStatus.Success)
                {
                    ModelState.AddModelError(string.Empty, RP.T("Account.Register.Error." + createStatus.ToString()).ToString());
                }
                else
                {
                    User u = _db.Users.FirstOrDefault(r => r.UserName == model.Email);
                    u.Roles       = SF.RolesStringAdd(u.Roles, "Register");
                    u.FirstName   = model.FirstName.Trim();
                    u.LastName    = model.LastName.Trim();
                    u.RoleDefault = "Register";
                    if (Session["address"] != null)
                    {
                        u.AddressMap = (string)Session["address"];
                    }
                    if (Session["longitude"] != null)
                    {
                        u.Longitude = (decimal)Session["longitude"];
                    }
                    if (Session["latitude"] != null)
                    {
                        u.Latitude = (decimal)Session["latitude"];
                    }

                    u.IsApproved       = true;
                    _db.Entry(u).State = EntityState.Modified;
                    _db.SaveChanges();
                    if (model.NewsLetter)
                    {
                        Newsletter n = new Newsletter()
                        {
                            NewsletterAccept = true,
                            NewsletterDate   = DateTime.UtcNow,
                            NewsletterEmail  = u.Email,
                            NewsletterName   = u.FirstName + " " + u.LastName,
                            RoleDefault      = "Register"
                        };
                        _db.Newsletters.Add(n);
                        _db.SaveChanges();
                        SF.AddToNewsletter(n);
                    }
                    if (Membership.ValidateUser(u.UserName, u.Password))
                    {
                        LS.Authorize(u);
                        var messService = new MessageService(_db);

                        messService.SendUserRegisterEmailToUser(u);
                        return(Json(new { result = "ok", url = "" }));
                    }
                    else
                    {
                        ModelState.AddModelError("", RP.T("Account.Logon.PasswordOrUserIncorrect").ToString());
                    }
                }
            }
            return(Json(new { result = "error", message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));

            //for future needed
            //   return Json(new { result = "error", message = new Dictionary<string,List<string>>().Select(x=> new { Value = x.Key,errors=x.Value  }) });
        }
        public ActionResult FacebookAjx(string token)
        {
            try
            {
                if (!string.IsNullOrEmpty(token))
                {
                    WebClient client     = new WebClient();
                    string    JsonResult = client.DownloadString(string.Concat(
                                                                     "https://graph.facebook.com/me?access_token=", token));

                    JObject jsonUserInfo = JObject.Parse(JsonResult);

                    FacebookProfile fp = new FacebookProfile();
                    fp.FacebookUsername = jsonUserInfo.Value <string>("username");
                    fp.FacebookEmail    = jsonUserInfo.Value <string>("email");
                    fp.FacebookLocale   = jsonUserInfo.Value <string>("locale");
                    fp.FacebookID       = jsonUserInfo.Value <long>("id");
                    fp.FacebookToken    = token;

                    fp.FacebookName        = jsonUserInfo.Value <string>("name");
                    fp.FacebookFirstName   = jsonUserInfo.Value <string>("first_name");
                    fp.FacebookLastName    = jsonUserInfo.Value <string>("last_name");
                    fp.FacebookLink        = jsonUserInfo.Value <string>("link");
                    fp.FacebookGender      = jsonUserInfo.Value <string>("gender");
                    fp.FacebookTimezone    = jsonUserInfo.Value <int>("timezone");
                    fp.FacebookVerified    = jsonUserInfo.Value <bool>("verified");
                    fp.FacebookUpdatedTime = jsonUserInfo.Value <DateTime>("updated_time");

                    User uLogin = _db.Users.FirstOrDefault(r => r.FacebookID == fp.FacebookID);
                    if (uLogin != null)
                    {
                        //login
                        LS.Authorize(uLogin);
                        // var messService = new MessageService(_db);
                        // messService.SendUserRegisterEmailToUser(u);
                        return(Json(new { result = "ok", url = "" }));
                    }
                    else
                    {
                        //register
                        string email = fp.FacebookEmail;
                        //generate code
                        var chars    = "0123456789abcdefghjklmnopqrstuwvxyzQAZWSXEDCRFVTGBYHNUJMIKLOP@$&%";
                        var random   = new Random();
                        var password = new string(
                            Enumerable.Repeat(chars, 8)
                            .Select(s => s[random.Next(s.Length)])
                            .ToArray());
                        if (string.IsNullOrEmpty(email))
                        {
                            if (!string.IsNullOrEmpty(fp.FacebookUsername))
                            {
                                email = fp.FacebookUsername + "@facebook.com";
                            }
                            else
                            {
                                email = fp.FacebookID.ToString() + "@facebook.com";
                            }
                        }
                        User uExist = _db.Users.FirstOrDefault(r => r.UserName == email);
                        if (uExist != null)
                        {
                            uExist.FacebookID = fp.FacebookID;
                            _db.SaveChanges();//update facebook ID
                            //login
                            LS.Authorize(uExist);

                            return(Json(new { result = "ok", url = "" }));
                        }
                        MembershipCreateStatus createStatus;
                        MembershipUser         newUser = Membership.CreateUser(email, password, email, "-", "-", true, out createStatus);

                        if (createStatus != MembershipCreateStatus.Success)
                        {
                            ModelState.AddModelError(string.Empty, RP.T("Account.Register.Error." + createStatus.ToString()).ToString());
                        }
                        else
                        {
                            User u = _db.Users.FirstOrDefault(r => r.UserName == email);
                            u.Roles            = SF.RolesStringAdd(u.Roles, "Register");
                            u.FirstName        = fp.FacebookFirstName;
                            u.LastName         = fp.FacebookLastName;
                            u.RoleDefault      = "Register";
                            u.FacebookID       = fp.FacebookID;
                            u.IsApproved       = true;
                            _db.Entry(u).State = EntityState.Modified;
                            _db.SaveChanges();

                            Newsletter n = new Newsletter()
                            {
                                NewsletterAccept = true,
                                NewsletterDate   = DateTime.UtcNow,
                                NewsletterEmail  = u.Email,
                                NewsletterName   = u.FirstName + " " + u.LastName,
                                RoleDefault      = "Register"
                            };
                            _db.Newsletters.Add(n);
                            _db.SaveChanges();
                            SF.AddToNewsletter(n);

                            if (Membership.ValidateUser(u.UserName, u.Password))
                            {
                                LS.Authorize(u);
                                var messService = new MessageService(_db);

                                messService.SendUserRegisterEmailToUser(u);
                                return(Json(new { result = "ok", url = "" }));
                            }
                            else
                            {
                                ModelState.AddModelError("", RP.T("Account.Logon.PasswordOrUserIncorrect").ToString());
                            }
                        }
                    }
                    return(Json(new { result = "error", json = JsonResult, facebook = fp, message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));
                }
                return(Json(new { result = "error", message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));
            }
            catch (Exception error)
            {
                SF.LogError(error);
                return(Json(new { result = "error", message = error.Message }));
            }
        }