示例#1
0
        /// <summary>
        /// Generates a new password for a given user and e-mails them the new credentials.
        /// </summary>
        /// <param name="u">User object</param>
        /// <returns>True if e-mail was sent ::: False if we encountered an error.</returns>
        public static Boolean sendNewPass(user u)
        {
            // Get the user information
            DocsLinqDataContext doc_db = new DocsLinqDataContext();
            user thisUser = (from users in doc_db.users
                            where users.userID.Equals(u.userID)
                            select users).FirstOrDefault<user>();

            // Generate the new password
            PasswordGenerator pg = new PasswordGenerator();
            string newPass = pg.Generate();

            // Assign to user
            thisUser.password = newPass;

            try { // Attempt to committ the changes to the database

                // Save the changes
                doc_db.SubmitChanges();

                // Attempt to send e-mail
                try {
                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient();

                    mail.To.Add(thisUser.email);
                    mail.Subject = "CURT Documentation Account Recovery";

                    mail.IsBodyHtml = true;
                    string htmlBody;

                    htmlBody    =   "<div style='margin-top: 15px;font-family: Arial;font-size: 10pt;'>";
                    htmlBody    +=  "<h4>Dear " + thisUser.fname + " " + thisUser.lname + ",</h4>";
                    htmlBody    +=  "<p>There has been a password change for {"+thisUser.username+"}. You're new credentials for CURT Manufacturing Documentation are: </p>";
                    htmlBody    +=  "<p style='margin:2px 0px'>Username: <strong>" + thisUser.username + "</strong></p>";
                    htmlBody    +=  "<p style='margin:2px 0px'>Password: <strong>" + newPass + "</strong></p>";
                    htmlBody    +=  "______________________________________________________________________";
                    htmlBody += "<p>If you feel this has been sent by mistake, please contact Web Support at <a href='mailto:[email protected]' target='_blank'>[email protected]</a>.</p>";
                    htmlBody    +=  "<br /><span style='color:#999'>Thank you,</span>";
                    htmlBody    +=  "<br /><br /><br />";
                    htmlBody    +=  "<span style='line-height:75px;color:#999'>CURT Documentation Administrator</span>";
                    htmlBody    +=  "</div>";

                    mail.Body = htmlBody;

                    SmtpServer.Send(mail);
                } catch (Exception e) {
                    Console.Write(e.Message);
                    return false;
                }

                return true;
            } catch (ChangeConflictException e) {
                return false;
            }
        }
        public ActionResult Signup(string fname)
        {
            // Assign form fields
            fname = Request.Form["fname"].Trim();
            string lname = Request.Form["lname"].Trim();
            string new_username = Request.Form["new_username"].Trim();
            string email = Request.Form["email"].Trim();
            string address = Request.Form["address"].Trim();
            string phone = Request.Form["phone"].Trim().Replace("-", "");
            string city = Request.Form["city"].Trim();
            int stateID = Convert.ToInt32(Request.Form["stateID"].Trim());
            int isDealer = (Request.Form["dealer"] != null)?1:0;
            string comments = Request.Form["comments"];

            // Initiate error list
            List<string> error_messages = new List<string>();

            /******* Validate form fields ******/
            if (fname.Length == 0) { error_messages.Add("First name is required."); }
            if (lname.Length == 0) { error_messages.Add("Last name is required."); }
            if (new_username.Length < 6) { error_messages.Add("Username must be at least 6 characters."); }
            if (email.Length == 0) { error_messages.Add("E-Mail is required."); }
            if (!email.Contains("curtmfg.com")) { error_messages.Add("CURT Manufacturing E-Mail address is required."); }
            if (phone.Length == 0) { error_messages.Add("Phone number is required."); }
            if (address.Length == 0) { error_messages.Add("Address is required."); }
            if (city.Length == 0) { error_messages.Add("City is required."); }
            if (stateID == 0) { error_messages.Add("State is required."); }
            if (comments.Length == 0) { error_messages.Add("Comments are required."); }

            DocsLinqDataContext doc_db = new DocsLinqDataContext();

            // Make sure we don't have a user for this e-mail address
            List<user> u = (from users in doc_db.users
                              where users.email.Equals(email)
                              select users).ToList<user>();
            if (u.Count != 0) { error_messages.Add("A user with this e-mail already exists in the database."); }

            // Make sure we don't have a user with this username
            int username_count = (from uc in doc_db.users
                                  where uc.username.Equals(new_username)
                                  select uc).Count();
            if (username_count > 0) { error_messages.Add("Username is taken."); }

            if(error_messages.Count == 0){ // Store user information and send e-mail to rep
                PasswordGenerator pg = new PasswordGenerator();
                string password = pg.Generate();

                user newUser = new user {
                    username = new_username,
                    password = password,
                    email = email,
                    fname = fname,
                    lname = lname,
                    phone = phone,
                    comments = comments,
                    stateID = stateID,
                    city = city,
                    address = address,
                    dateAdded = DateTime.Now,
                    isDealer = isDealer
                };

                doc_db.users.InsertOnSubmit(newUser);
                try{
                    doc_db.SubmitChanges();
                    Users.AlertRep(newUser);
                    ViewBag.submitted = 1;
                }catch(Exception e){
                    error_messages.Add(e.Message);
                    ViewBag.error_messages = error_messages;

                    // Get the states
                    List<State> states = (from s in doc_db.States
                                          orderby s.abbr
                                          select s).ToList<State>();
                    ViewBag.states = states;
                }

            }else{ // Present error messages to user
                ViewBag.error_messages = error_messages;
                ViewBag.fname = fname;
                ViewBag.lname = lname;
                ViewBag.new_username = new_username;
                ViewBag.email = email;
                ViewBag.address = address;
                ViewBag.phone = phone;
                ViewBag.city = city;
                ViewBag.stateID = stateID;
                ViewBag.comments = comments;
                ViewBag.isDealer = isDealer;

                // Get the states
                List<State> states = (from s in doc_db.States
                                      orderby s.abbr
                                      select s).ToList<State>();
                ViewBag.states = states;

            }

            return View();
        }