예제 #1
0
        /// <summary>
        /// Login a user.
        /// </summary>
        /// <param name="loggedInUser">The user to log in.</param>
        /// <returns></returns>
        public ActionResult LoginExecute(User loggedInUser)
        {
            var privateToken = _context.Users.Find(loggedInUser.Username).PrivateToken;

            var validUsers = _context.Users.Count(x => x.Username == loggedInUser.Username && x.Password == loggedInUser.Password);

            if (validUsers == 1)
            {
                Session.Add("PrivateToken", privateToken);
                return RedirectToAction("Home", new { @username = loggedInUser.Username });
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
예제 #2
0
        public ActionResult SetupDemoUser()
        {
            var rand = new Random((int)DateTime.Now.Ticks);

            var demoUser = new User
                {
                    Password = "******",
                    Username = "******",
                    PrivateToken = TokenFactory.GenerateToken(),

                };

            // First remove user.
            if (_context.Users.Count(x => x.Username == "*****@*****.**")==1)
            {
                var user = _context.Users.Find(demoUser.Username);
                var weightPlots = new WeightPlot[user.WeightPlots.Count];
                user.WeightPlots.CopyTo(weightPlots,0);

                foreach(var plot in weightPlots)
                {
                    _context.WeightPlots.Remove(plot);
                }
                _context.Users.Remove(user);
            }
            _context.SaveChanges();

            // Add user again.
            _context.Users.Add(demoUser);
            _context.SaveChanges();

            // Get the user back again.
            demoUser = _context.Users.Find(demoUser.Username);

            // Add plots.
            var date = DateTime.Parse("2011-01-01");
            var weight = 110.0;
            double weightLossPerDay = 0.20;
            const double weightLossPerDayDecay = 0.995;
            for(var i=0;i<365;i++)
            {
                var plot = new WeightPlot
                {
                    Weight = weight + (rand.NextDouble()*2),
                    PlotId = Guid.NewGuid(),
                    PlotStamp = date
                };

                // Loose weight
                weight = weight - weightLossPerDay + rand.NextDouble()/rand.Next(5,50);
                weightLossPerDay *= weightLossPerDayDecay;

                // Append +1 days.
                date = date.AddDays(1);

                // Skip most of the days.
                if (rand.Next(0, 100) > 25) continue;

                demoUser.WeightPlots.Add(plot);
            }

            _context.SaveChanges();

            return RedirectToAction("Index");
        }
예제 #3
0
        /// <summary>
        /// Register new user.
        /// </summary>
        /// <param name="newUser">The new user to register.</param>
        /// <returns></returns>
        public ActionResult RegisterExecute(User newUser)
        {
            // Create the private token.
            newUser.PrivateToken = TokenFactory.GenerateToken();

            // ===================================================================
            // Check if user already exist. If already exist, then redirect
            // to DisplayError action with errorCode 10001.
            // ===================================================================
            var user = _context.Users.Find(newUser.Username);
            if (user != null) return RedirectToAction("DisplayError", "Error", new {@errorCode = 10001});

            // TODO: Maybe we need to check if the private key exist already.

            // Execute register.
            _context.Users.Add(newUser);
            _context.SaveChanges();

            LoginUsingToken(newUser.PrivateToken);

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