public void TestDatabase() { using (var context = new SportsStoreContext()) { DbSet <Product> products = context.Products; Assert.AreEqual(9, products.Count(), "Number of products wasn't 10"); } }
protected override bool AuthorizeCore(HttpContextBase httpContext) { User user = null; using (var dbEntry = new SportsStoreContext()) { user = dbEntry.Users .Include(u => u.Roles) .FirstOrDefault(u => u.Email == httpContext.User.Identity.Name); } if (user != null) { foreach (UserRole role in user.Roles) { foreach (var permission in role.Permissions) { if (permission.Name == Name) { return(true); } } } } return(false); }
public void Test_Categories() { using (var context = new SportsStoreContext()) { DbSet <Category> categories = context.Category; Assert.AreEqual(3, categories.Count(), "Number of categores wasn't 3"); } }
public ActionResult Create(Product product) { try { using (var context = new SportsStoreContext()) { context.Products.Add(product); context.SaveChanges(); } return(RedirectToAction("List")); } catch { return(View()); } }
public static void UpdateListPermissions() { SportsStoreContext db = new SportsStoreContext(); Assembly asm = Assembly.GetExecutingAssembly(); var permissions = (from type in asm.GetTypes() where typeof(Controller).IsAssignableFrom(type) from method in type.GetMethods() where method.IsDefined(typeof(AuthorizePermissionAttribute)) select new Permission() { Name = type.ToString() + method.Name, Description = method.GetCustomAttribute <AuthorizePermissionAttribute>().Description }).ToList(); foreach (var dbPermission in db.Permissions) { var newPermission = permissions.FirstOrDefault(attributePermission => dbPermission.Name.Contains(attributePermission.Name) && attributePermission.Description == dbPermission.Description); if (newPermission == null) { //Update the current permissions dbPermission.Description = newPermission.Description; permissions.Remove(newPermission); } else { //Removes unused permission db.Permissions.Remove(dbPermission); } } foreach (var permission in permissions) { db.Permissions.Add(permission); } db.SaveChanges(); }
public static bool HadPermission(this IPrincipal user, string permissionName) { User defaultUser = null; using (var db = new SportsStoreContext()) { defaultUser = db.Users.Include(u => u.Roles).FirstOrDefault(u => u.Email == user.Identity.Name); } if (defaultUser == null) { throw new UserNotFoundException(); } foreach (UserRole role in defaultUser.Roles) { foreach (var permission in role.Permissions) { if (permission.Name == permissionName) { return(true); } } } return(false); }
public RoleStore(SportsStoreContext context) : base(context) { }
public UserStore(SportsStoreContext context) : base(context) { }
public static void EnsurePopulated(IApplicationBuilder app) { SportsStoreContext context = app.ApplicationServices .GetRequiredService <SportsStoreContext>(); var userManager = app.ApplicationServices .GetRequiredService <UserManager <IdentityUser> >(); if (!context.Products.Any()) { context.Products.AddRange( new Product { Name = "Kayak", Description = "A boat for one person", Category = "Watersports", Price = 275 }, new Product { Name = "Lifejacket", Description = "Protective and fashionable", Category = "Watersports", Price = 48.95m }, new Product { Name = "Soccer Ball", Description = "FIFA-approved size and weight", Category = "Soccer", Price = 19.50m }, new Product { Name = "Corner Flags", Description = "Give your playing field a professional touch", Category = "Soccer", Price = 34.95m }, new Product { Name = "Stadium", Description = "Flat-packed 35,000-seat stadium", Category = "Soccer", Price = 79500 }, new Product { Name = "Thinking Cap", Description = "Improve brain efficiency by 75%", Category = "Chess", Price = 16 }, new Product { Name = "Unsteady Chair", Description = "Secretly give your opponent a disadvantage", Category = "Chess", Price = 29.95m }, new Product { Name = "Human Chess Board", Description = "A fun game for the family", Category = "Chess", Price = 75 }, new Product { Name = "Bling-Bling King", Description = "Gold-plated, diamond-studded King", Category = "Chess", Price = 1200 } ); context.SaveChanges(); } if (userManager.FindByEmailAsync("*****@*****.**").GetAwaiter().GetResult() == null) { var user = new IdentityUser { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, LockoutEnabled = false }; userManager.CreateAsync(user, "Password@123").GetAwaiter().GetResult(); } }
public Repository(SportsStoreContext dbContext) { dbSet = dbContext.Set <T>(); }
public UnitOfWork(SportsStoreContext dbContext) { this.dbContext = dbContext; }
public OrderService(SportsStoreContext context) { _context = context; }
public ProductService(SportsStoreContext context, IMapper mapper) { _context = context; _mapper = mapper; }