Ejemplo n.º 1
0
        //
        // GET: /Booking/Create
        public ActionResult Create(int id)
        {
            //Get current user ID to get the first name/last name
            int userID = WebSecurity.GetUserId(User.Identity.Name);
            Complete bm = new Complete();

            using (UsersContext db2 = new UsersContext())
            {
                //get user profile
                UserProfile user = db2.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == User.Identity.Name.ToLower());
                if (user != null)
                {
                    bm.UP = user;
                }
            }
            ViewBag.Message = id.ToString();
            bm.DSM = db.DivingSpots.Find(id);

            return View(bm);
        }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<UsersContext>(null);

                try
                {
                    using (var context = new UsersContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
Ejemplo n.º 3
0
        //
        // GET: /Booking/
        public ActionResult Index()
        {
            Complete bm = new Complete();
            using (UsersContext db2 = new UsersContext())
            {
                //get user profile
                UserProfile user = db2.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == User.Identity.Name.ToLower());
                if (user != null)
                {
                    bm.UP = user;
                }
            }

            if (bm.UP.UserName.Contains("admin"))
            {
                List<Complete> blist = new List<Complete>();

                db.DivingSpots.ToList();

                foreach (BookingModel i in db.Bookings)
                {
                    Complete b = new Complete();
                    //Get booking
                    b.Book = i;
                    //Get Divingspot
                    b.DSM = db.DivingSpots.Find(i.DivingSpotID);

                    //Get user
                    using (UsersContext db2 = new UsersContext())
                    {
                        //get user profile
                        b.UP = db2.UserProfiles.Find(i.UserId);
                    }

                    blist.Add(b);
                }

                return View(blist.ToList());
            }
            else
            {
                //get current user id
                int userID = bm.UP.UserId;
                //Return all booking that has the same user ID

                List<Complete> blist = new List<Complete>();

                db.DivingSpots.ToList();

                foreach (BookingModel i in db.Bookings)
                {
                    if (i.UserId == userID)
                    {
                        Complete b = new Complete();
                        //Get booking
                        b.Book = i;
                        //Get Divingspot
                        b.DSM = db.DivingSpots.Find(i.DivingSpotID);
                        //Get user
                        using (UsersContext db2 = new UsersContext())
                        {
                            //get user profile
                            UserProfile user = db2.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == User.Identity.Name.ToLower());
                            if (user != null)
                            {
                                b.UP = user;
                            }
                        }

                        blist.Add(b);
                    }
                }

                return View(blist);
            }
        }
Ejemplo n.º 4
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (UsersContext db = new UsersContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }