public IActionResult Index([FromServices] ApplicationDbContext applicationDbContext,
                                   [FromServices] MultiTenantDbContext extraAuthorizeDbContext)
        {
            var userLister = new ListUsers(applicationDbContext, extraAuthorizeDbContext);

            return(View(userLister.ListUserWithRolesAndModules()));
        }
 private static void SetupStockInShops(this MultiTenantDbContext context, IEnumerable <Shop> shops)
 {
     foreach (var shop in shops)
     {
         var stock1 = new StockInfo {
             Name = $"{shop.Name} nice stuff", NumInStock = 10, AtShop = shop, DistrictManagerId = shop.DistrictManagerId
         };
         var stock2 = new StockInfo {
             Name = $"{shop.Name} other stuff", NumInStock = 22, AtShop = shop, DistrictManagerId = shop.DistrictManagerId
         };
         var stock3 = new StockInfo {
             Name = $"{shop.Name} thingy", NumInStock = 3, AtShop = shop, DistrictManagerId = shop.DistrictManagerId
         };
         context.AddRange(stock1, stock2, stock3);
     }
 }
        public void TestShopHierarchicalFilterWithIncludeOk(int shopKey, string districtManagerId, string stockName)
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <MultiTenantDbContext>();

            using (var context =
                       new MultiTenantDbContext(options, new MockGetClaimsProvider("user-id", 0, "manager-id")))
            {
                context.Database.EnsureCreated();

                var mUser = new MultiTenantUser
                {
                    UserId            = "manager-id",
                    IsDistrictManager = true
                };
                var shop1 = new Shop {
                    Name = "shop1", DistrictManager = mUser
                };
                var shop2 = new Shop {
                    Name = "shop2"
                };
                context.AddRange(shop1, shop2);
                context.SaveChanges();
                var stock1 = new StockInfo
                {
                    Name = shop1.Name, NumInStock = 10, AtShop = shop1, DistrictManagerId = shop1.DistrictManagerId
                };
                var stock2 = new StockInfo
                {
                    Name = shop2.Name, NumInStock = 10, AtShop = shop2, DistrictManagerId = shop2.DistrictManagerId
                };
                context.AddRange(stock1, stock2);
                context.SaveChanges();
            }
            using (var context = new MultiTenantDbContext(options, new MockGetClaimsProvider("user-id", shopKey, districtManagerId)))
            {
                //ATTEMPT
                var filtered = context.CurrentStock.Include(x => x.AtShop).ToList();

                //VERIFY
                filtered.Count.ShouldEqual(1);
                filtered.Single().Name.ShouldEqual(stockName);
            }
        }
        public async Task ValidateAsync(CookieValidatePrincipalContext context)
        {
            //NOTE: To make easier to see the data authorization code I have removed
            //all the feature authorization code described in the article
            //https://www.thereformedprogrammer.net/a-better-way-to-handle-authorization-in-asp-net-core/
            //BUT in real life this method with have both the feature authorization and data authorization code in it


            if (context.Principal.Claims.Any(x => x.Type == GetClaimsFromUser.ShopKeyClaimName))
            {
                return;
            }

            //No ShopKey in the claims, so we need to add it. This is only happens once after the user has logged in
            var claims = new List <Claim>();

            claims.AddRange(context.Principal.Claims); //Copy over existing claims

            //now we lookup the user to find what shop they are linked to
            using (var multiContext = new MultiTenantDbContext(_multiTenantOptions, new DummyClaimsFromUser()))
            {
                var userId = context.Principal.Claims.Single(x => x.Type == ClaimTypes.NameIdentifier).Value;
                var mTUser = await multiContext.MultiTenantUsers.IgnoreQueryFilters()
                             .SingleOrDefaultAsync(x => x.UserId == userId);

                if (mTUser == null)
                {
                    throw new InvalidOperationException($"The user {context.Principal.Claims.Single(x => x.Type == ClaimTypes.Name).Value} was not linked to a multi-tenant user.");
                }
                claims.Add(new Claim(GetClaimsFromUser.ShopKeyClaimName, mTUser.ShopKey.ToString()));
                if (mTUser.IsDistrictManager)
                {
                    claims.Add(new Claim(GetClaimsFromUser.DistrictManagerIdClaimName, mTUser.UserId));
                }
            }

            //Build a new ClaimsPrincipal and use it to replace the current ClaimsPrincipal
            var identity     = new ClaimsIdentity(claims, "Cookie");
            var newPrincipal = new ClaimsPrincipal(identity);

            context.ReplacePrincipal(newPrincipal);
            //THIS IS IMPORTANT: This updates the cookie, otherwise this calc will be done every HTTP request
            context.ShouldRenew = true;
        }
Exemple #5
0
        //---------------------------------------------------------------------------
        //private methods

        private static IEnumerable <Shop> SetupMultiTenantUsers(this MultiTenantDbContext context, List <UserInfoJson> userInfos,
                                                                IdentityUser[] users)
        {
            var shopsDict = new Dictionary <string, Shop>();

            IEnumerable <Shop> AddOrFindShops(string[] shopNames)
            {
                foreach (var shopName in shopNames)
                {
                    if (!shopsDict.ContainsKey(shopName))
                    {
                        var shop = new Shop {
                            Name = shopName
                        };
                        shop.SetShopKey(shopsDict.Values.Count + 1);  //set the shopKey, as used later
                        shopsDict[shopName] = shop;
                    }

                    yield return(shopsDict[shopName]);
                }
            }

            foreach (var userInfo in userInfos)
            {
                if (userInfo.ShopNames != null)
                {
                    var shops = AddOrFindShops(userInfo.ShopNames.Split(',')).ToList();
                    var mUser = shops.Count == 1
                        ? new MultiTenantUser
                    {
                        UserId = users.Single(x => x.Email == userInfo.Email).Id, ShopKey = shops.Single().ShopKey
                    }
                        : new MultiTenantUser
                    {
                        UserId = users.Single(x => x.Email == userInfo.Email).Id,
                        //ShopKey is not set, i.e. it defaults to zero
                        IsDistrictManager = true,
                        AccessTo          = shops
                    };
                    context.Add(mUser);
                }
            }
            return(shopsDict.Values);
        }
        public void TestCreateValidDatabaseOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <MultiTenantDbContext>();

            using (var context = new MultiTenantDbContext(options, new MockGetClaimsProvider("user-id", 0, null)))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                var shop1 = new Shop {
                    Name = "Test1"
                };
                var shop2 = new Shop {
                    Name = "Test2"
                };
                context.AddRange(shop1, shop2);
                context.SaveChanges();

                //VERIFY
                context.Shops.IgnoreQueryFilters().Count().ShouldEqual(2);
            }
        }
Exemple #7
0
 public ListUsers(ApplicationDbContext applicationDbContext, MultiTenantDbContext multiTenantDbContext)
 {
     _applicationDbContext = applicationDbContext ?? throw new ArgumentNullException(nameof(applicationDbContext));
     _multiTenantDbContext = multiTenantDbContext ?? throw new ArgumentNullException(nameof(multiTenantDbContext));
 }
 public StockController(MultiTenantDbContext context)
 {
     _context = context;
 }
Exemple #9
0
 public UsersController(ApplicationDbContext applicationDbContext, ExtraAuthorizeDbContext extraAuthorizeDbContext, MultiTenantDbContext multiTenantDbContext)
 {
     _applicationDbContext    = applicationDbContext;
     _extraAuthorizeDbContext = extraAuthorizeDbContext;
     _multiTenantDbContext    = multiTenantDbContext;
 }