public async Task SeedAsync(IServiceProvider serviceProvider)
        {
            if (await _tenants.IgnoreQueryFilters().AnyAsync())
            {
                return;
            }

            var t1 = new Tenant {
                Id = "T20210602000001", Name = "租户一",
            };

            t1.AddConnectionStrings("Default", "Server=127.0.0.1,1433;Database=Pnct_T20210602000001;User Id=sa;Password=970307lBX;Trusted_Connection = False;");
            var t2 = new Tenant {
                Id = "T20210602000002", Name = "租户二",
            };

            t2.AddConnectionStrings("Default", "Server=127.0.0.1,1433;Database=Pnct_T20210602000002;User Id=sa;Password=970307lBX;Trusted_Connection = False;");
            await _tenants.InsertAsync(t1, true);

            await _tenants.InsertAsync(t2, true);
        }
Example #2
0
        /// <inheritdoc />
        public async Task SeedAsync(IServiceProvider serviceProvider)
        {
            string[] tenantIds   = new[] { "T20210602000001", "T20210602000002" };
            var      permissions = new Dictionary <string, List <string> >
            {
                { "ProductManager", new List <string>
                  {
                      "ProductManager.Products",
                      "ProductManager.Products.Create",
                      "ProductManager.Products.Edit",
                      "ProductManager.Products.Delete",

                      "ProductManager.Devices",
                      "ProductManager.Devices.Create",
                      "ProductManager.Devices.Edit",
                      "ProductManager.Devices.Delete",
                  } },
                { "TenantManager", new List <string>
                  {
                      "TenantManager.Tenant",
                      "TenantManager.Tenant.Create",
                      "TenantManager.Tenant.Edit",
                      "TenantManager.Tenant.Delete",
                  } },
                { "PermissionManager", new List <string>
                  {
                      "PermissionManager.PermissionGroup",
                      "PermissionManager.PermissionGroup.Create",
                      "PermissionManager.PermissionGroup.Edit",
                      "PermissionManager.PermissionGroup.Delete",

                      "PermissionManager.Permission",
                      "PermissionManager.Permission.Create",
                      "PermissionManager.Permission.Edit",
                      "PermissionManager.Permission.Delete",
                  } }
            };

            if (!(await _permissionGroup.AnyAsync()))
            {
                var group = new List <PermissionGroupDefinition>();
                foreach (var item in permissions)
                {
                    var permission = new PermissionGroupDefinition
                    {
                        Name        = item.Key,
                        DisplayName = item.Key,
                        CreateTime  = DateTime.Now,
                    };
                    permission.Permissions = new List <PermissionDefinition>();
                    foreach (var p in item.Value)
                    {
                        permission.Permissions.Add(new PermissionDefinition(p, p));
                    }
                    group.Add(permission);
                }
                await _permissionGroup.InsertAsync(group, true);
            }



            foreach (var tenantId in tenantIds)
            {
                using (_currentTenant.Change(tenantId, "租户一", out var scope))
                {
                    var productRepository = scope.ServiceProvider.GetService <IPermissionGrantRepository>();
                    if (await productRepository.AnyAsync())
                    {
                        continue;
                    }

                    foreach (var item in permissions)
                    {
                        foreach (var value in item.Value)
                        {
                            await productRepository.InsertAsync(new PermissionGrant
                            {
                                Name         = value,
                                ProviderName = "role",
                                ProviderKey  = "admin",
                            });
                        }
                    }
                    await productRepository.Uow.SaveChangesAsync();
                }
            }
        }