Ejemplo n.º 1
0
        public ActionResult InstallFakeUsers()
        {
            //fake users
            for (int count = 0; count < 30; count++)
            {
                using (TransactionScope scope = new TransactionScope())
                {

                    membership _m = new membership
                    {
                        email = RandomString(6) + "@" + RandomString(6) + ".com",
                        joined_date = DateTime.Now.AddMonths(-1).AddDays(count),
                        is_approved = "Y",
                        username = "******" + (count + 1).ToString(),
                        password = "******" + (count + 1).ToString()
                    };
                    db.memberships.Add(_m);
                    db.SaveChanges();

                    user _u = new user
                    {
                        membership = _m,
                        last_name = RandomString(6),
                        first_name = RandomString(6),
                        gender = Gender.Unspecified.ToString(),
                        about = RandomString(200)
                    };
                    db.users.Add(_u);
                    db.SaveChanges();
                    scope.Complete();
                }
            }
            return RedirectToAction("Index", "User");
        }
Ejemplo n.º 2
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //start transaction block
                    using (TransactionScope scope = new TransactionScope())
                    {
                        /*
                         * check if username is already present
                         * in the membership table
                         * SELECT membership_id FROM membership
                         * WHERE username = "******" LIMIT 0,1
                         */

                        var isUsernameTaken = (from m in db.memberships
                                    where m.username == model.UserName
                                    select m.membership_id).SingleOrDefault();
                        if (isUsernameTaken > 1)
                        {
                            ModelState.AddModelError("",
                                "Username is already taken. Please choose another");
                            return View(model);
                        }
                        else
                        {

                            /*
                             * CREATE a new record in member with user
                             * provided username,password and email
                             * INSERT INTO membership
                             * (username,password,email,is_approved)
                             * VALUES (?,?,?,?)
                             *
                             *  we are skipping email validation for
                             *  the demo due lack of time :(
                             *  no password hashing for the sake of simplity right now
                             */
                            var newMember = new membership
                            {
                                username = model.UserName,
                                password = model.Password,
                                email = model.Email,
                                is_approved = "Y",
                                joined_date = DateTime.Now

                            };
                            db.memberships.Add(newMember);
                            db.SaveChanges();

                            /* NOW create a user record with the
                             * membership_id of previous insert. INSERT DEFAULT DATA FOR NOW.
                             * INSERT INTO users (gender,membership_id) VALUES (?,?)
                             */
                            var newUser = new user
                            {
                                gender = Gender.Unspecified.ToString(),
                                membership = newMember
                            };
                            db.users.Add(newUser);
                            db.SaveChanges();

                            //everything is OK. Set the username in session
                            Session.Add("LoggedInUser", model.UserName);
                            //transaction complete
                            scope.Complete();
                            return Redirect(string.Format("/User/{0}/Index", model.UserName));
                        }
                    }
                }
                catch (Exception ex)
                {
                    //exception occured. transaction scope is incomplete
                    //so no need to rollback. display some generic error
                    ModelState.AddModelError("", "Could not register");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }