Пример #1
0
        public ActionResult Create(User user2)
        {
            curUser = User.Identity.Name;
            //first check to see if they are an existing friend
            IQueryable<Friend> fq =
                from frnd in db.Friends
                where (frnd.User1.UserName == curUser && user2.UserId == frnd.User2.UserId)
                    || (frnd.User2.UserName == curUser && user2.UserId == frnd.User1.UserId)
                select frnd;

            foreach (Friend frnd in fq.ToList())
            {
                if (frnd.Status != null)
                {
                    if (frnd.Status.Status == "blocked" || frnd.Status.Status == "accepted" || frnd.Status.Status == "requested") //nonzero, so this relationship exists
                    {
                        ViewBag.message = "You already have a friend relation with this user.";
                        return View("Error");
                    }
                }
            }

            //They aren't, so proeed with the request
            Friend friend = new Friend();
            friend.User1 = db.Users.Single( f => f.UserName == curUser);

            IQueryable<User> friendQuery =
                from user in db.Users
                where user.UserId == user2.UserId
                select user;

            friend.User2 = friendQuery.Single();

            IQueryable<FriendStatus> statQuery =
                from stat in db.FriendStatuses
                where stat.Status == "requested"
                select stat;
            friend.Status = statQuery.Single();
            //if (ModelState.IsValid)
            //{
                db.Friends.Add(friend);
                db.SaveChanges();
                return RedirectToAction("Index");
            //}
            //else Console.Write("bleh");

            //return View(friend);
        }
        public ActionResult AccountDetails(UserAccount account)
        {
            User user = new User();

            using (PYPContext db = new PYPContext())
            {
                user = db.Users.Single(u => u.UserName == User.Identity.Name);
                user.UserName = account.UserName;
                user.Name = account.Name;
                user.Email = account.Email;

                if (db.Entry(user).State == EntityState.Modified)
                {
                    db.SaveChanges();
                }
            }

            return RedirectToAction("Index", "Home");
        }
        public ActionResult Register(UserRegistration model)
        {
            if (ModelState.IsValid)
            {
                using (PYPContext db = new PYPContext())
                {
                    User user = new User();
                    user.Name = model.Name;
                    user.Email = model.Email;
                    user.UserName = model.UserName;
                    user.Salt = Crypto.GenerateSalt();
                    user.Password = Crypto.HashPassword(model.Password + user.Salt);

                    // Save the new user to the database
                    db.Users.Add(user);
                    db.SaveChanges();

                    // Login the new user
                    FormsAuthentication.SetAuthCookie(user.UserName, false);
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Response.Cookies.Get(FormsAuthentication.FormsCookieName).Value);
                    GenericPrincipal userPrincipal = new GenericPrincipal(new FormsIdentity(ticket), null);
                    System.Web.HttpContext.Current.User = userPrincipal;
                    Thread.CurrentPrincipal = userPrincipal;
                }

                // Redirect to Home
                return RedirectToAction("Index", "Home");
            }

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