Beispiel #1
0
        public ActionResult CreateUser(user model)
        {
            using (DisDBContext db = new DisDBContext())
            {
                var newUser = User as CustomPrincipal;
                if (newUser != null)
                {
                    user _userAdmin = db.Users.FirstOrDefault(m => m.id == newUser.UserId && m.email == newUser.UserEmail);
                    if (_userAdmin.position == 1 || _userAdmin.position == 0)
                    {
                        user _userNew = new user();
                        _userNew = db.Users.FirstOrDefault(p => p.email.ToLower() == model.email.ToLower()); //проверяем есть ли п ользователь с таким email
                        if (_userNew == null)
                        {
                            _userNew               = new user();
                            _userNew.first_name    = model.first_name;
                            _userNew.last_name     = model.last_name;
                            _userNew.sur_name      = model.sur_name;
                            _userNew.register_day  = DateTime.Now;
                            _userNew.birth_day     = model.birth_day;
                            _userNew.iin           = model.iin;
                            _userNew.sex           = model.sex;
                            _userNew.tel_number    = model.tel_number;
                            _userNew.department_id = model.department_id;

                            _userNew.check    = 1;
                            _userNew.position = 2;

                            HashMD5 Hash        = new HashMD5();
                            string  newPassWord = Hash.CreatePassword(6);
                            _userNew.security_stamp = Hash.HashMD5String(DateTime.Now.ToString());
                            _userNew.password       = newPassWord;

                            _userNew.email      = model.email;
                            _userNew.company_id = _userAdmin.company_id;
                            db.Users.Add(_userNew);
                            db.SaveChanges();

                            _userNew = db.Users.Where(u => u.email.Trim() == model.email && u.password.Trim() == newPassWord).FirstOrDefault();


                            if (_userNew != null)
                            {
                                #region sendEmail
                                string htmlCodeFull;
                                string code        = _userNew.email;
                                var    callbackUrl = Url.Action("ConfirmEmail", "Home", new { userId = _userNew.id, code = code }, protocol: Request.Url.Scheme);

                                StreamReader readerhtmlCodeHead = new StreamReader(Server.MapPath("~/Views/Mailing/emailConfirmHead.html"));
                                string       htmlCodeHead       = readerhtmlCodeHead.ReadToEnd();

                                StreamReader readerhtmlCodeFooter = new StreamReader(Server.MapPath("~/Views/Mailing/emailConfirmFooter.html"));
                                string       htmlCodeFooter       = readerhtmlCodeFooter.ReadToEnd();


                                string htmlCodeMiddle = "<table id=\"rec85502797\" style=\"width:100%; border-collapse:collapse; border-spacing:0; margin:0; border:0;\" cellpadding=\"0\" cellspacing=\"0\" data-record-type=\"618\"><tr><td style=\"padding-left:15px; padding-right:15px; \"><table id=\"recin85502797\" class=\"r\" style=\"margin: 0 auto;background-color:#ffffff;border-spacing: 0;width:600px;\" align=\"center\"><tr><td style=\"padding-top:30px;padding-bottom:30px;padding-left:30px;padding-right:30px;\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"width:100%;\"><tr><td><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin: 0 auto;\"><tr><td><a style=\"display: table-cell; text-decoration: none; font-size: 15px; text-align: center; font-weight: bold; font-family:Helvetica Neue, Helvetica, Arial, sans-serif; width: 100%;color:#ffffff; border:0px solid ;\" href=\" " + callbackUrl + " \"><img src=\"https://i.imgur.com/5p3P6ft.png\" alt=\"Confirm\" width=\"200px\" height=\"70px\"></a></td></tr></table></td></tr></table></td></tr></table></td></tr></table>";

                                string htmlCodeMiddle2 = "<table id=\"rec85502797\" style=\"width:100%; border-collapse:collapse; border-spacing:0; margin:0; border:0;\" cellpadding=\"0\" cellspacing=\"0\" data-record-type=\"618\"><tr><td style=\"padding-left:15px; padding-right:15px; \"><table id=\"recin85502797\" class=\"r\" style=\"margin: 0 auto;background-color:#ffffff;border-spacing: 0;width:600px;\" align=\"center\"><tr><td style=\"padding-top:30px;padding-bottom:30px;padding-left:30px;padding-right:30px;\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"width:100%;\"><tr><td><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin: 0 auto;\"><tr><td><div style=\"margin-right: auto; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; color:#444444;font-size:20px;line-height:1.45;\"><p>Email: " + _userNew.email + "</p><p>Password: "******"</p><br /></div></td></tr></table></td></tr></table></td></tr></table></td></tr></table>";
                                htmlCodeFull = htmlCodeHead + htmlCodeMiddle + htmlCodeMiddle2 + htmlCodeFooter;

                                Method m = new Method();
                                m.sendEmail(_userNew.email.Trim(), "Confirm registration", htmlCodeFull);
                                #endregion
                                return(Json("success", JsonRequestBehavior.AllowGet)); // return RedirectToAction("ConfirmWaitingPage", "Home");
                            }
                            else
                            {
                                return(Json("error", JsonRequestBehavior.AllowGet));
                            }
                        }
                        else
                        {
                            return(Json("errorMail", JsonRequestBehavior.AllowGet)); //Пользователь с таким email уже зарегистрирован
                        }
                    }
                }
                return(RedirectToAction("AccessDenied", "Home"));
            }
        }