public SimpleMembershipInitializer()
            {
                Database.SetInitializer<PIMContext>(null);

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

                    if (!WebSecurity.Initialized)
                        WebSecurity.InitializeDatabaseConnection("PIMdb", "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);
                }
            }
Exemple #2
0
        public ActionResult Index(PIMModel model, HttpPostedFileBase file)
        {
            model.Brands = GetBrands();

            if (ModelState.IsValid)
            {
                int added = 0;
                int updated = 0;

                if (file != null && file.ContentLength > 0 && file.FileName.Contains("xls"))
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Files/upload"), fileName);
                    file.SaveAs(path);
                    var dt = ReadSpreadsheetIntoDataTable(path, "ProductInfo");
                    System.IO.File.Delete(path);

                    using (var db = new PIMContext())
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            if (string.IsNullOrEmpty(row["Style Number"].ToString().Trim()))
                            {
                                ModelState.AddModelError("StyleError", "A style number is missing. Please correct and re-upload.");
                                return View(model);
                            }
                            string patternName = row["Pattern Name"].ToString().Trim();
                            var pattern = db.Patterns.SingleOrDefault(p => p.PatternName == patternName);
                            if (pattern == null) pattern = new Pattern();
                            pattern.BrandId = model.BrandId;
                            pattern.PatternName = patternName;
                            pattern.LastUpdated = DateTime.Now;
                            // Add if it doesn't exist otherwise just updates the existing pattern that was loaded
                            db.Entry(pattern).State = pattern.PatternId == 0 ? EntityState.Added : EntityState.Modified;
                            db.SaveChanges(); // Need to save the pattern first to get an id

                            string styleNumber = row["Style Number"].ToString().Trim();
                            var style = db.Styles.SingleOrDefault(s => s.StockNumber == styleNumber);
                            if (style == null) style = new Style();
                            style.PatternId = pattern.PatternId;
                            style.StockNumber = styleNumber;
                            style.MarketingDescription = row["Marketing Description"].ToString();
                            style.TechBullets = !string.IsNullOrEmpty(row["Tech Bullets"].ToString()) ?
                                row["Tech Bullets"].ToString() : null;
                            style.LastUpdated = DateTime.Now;
                            style.LastUpdatedBy = User.Identity.Name;

                            db.Entry(style).State = style.StyleId == 0 ? EntityState.Added : EntityState.Modified;
                            if (style.StyleId == 0)
                                added++;
                            else
                                updated++;
                            db.SaveChanges();
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("XlsError", "Please upload a valid Excel file.");
                    return View(model);
                }

                return RedirectToAction("ViewProducts", new { b = model.BrandId, a = added, u = updated });
            }

            return View(model);
        }
Exemple #3
0
        private SelectList GetBrands()
        {
            using (var db = new PIMContext())
            {
                var query = from b in db.Brands
                            select b;

                return new SelectList(query.ToList(), "BrandId", "BrandName");
            }
        }
Exemple #4
0
        public ActionResult Purge()
        {
            using (var db = new PIMContext())
            {
                var ctx = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db).ObjectContext;
                ctx.ExecuteStoreCommand("TRUNCATE TABLE [Style]");
                ctx.ExecuteStoreCommand("DELETE FROM [Pattern]");
            }

            ViewBag.Purged = "The [Style] and [Pattern] tables have been purged.";
            return View("Admin");
        }
Exemple #5
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 (var db = new PIMContext())
                {
                    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);
        }