private void CreateRolesAndUsers()
        {
            //Admin role

            var adminRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Admin);

            if (adminRole == null)
            {
                adminRole = _context.Roles.Add(new SysRole(_tenantId, StaticRoleNames.Tenants.Admin, StaticRoleNames.Tenants.Admin)
                {
                    IsStatic = true
                });
                _context.SaveChanges();

                //Grant all permissions to admin role
                var permissions = PermissionFinder
                                  .GetAllPermissions(new AppAuthorizationProvider())
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    _context.Permissions.Add(
                        new RolePermissionSetting
                    {
                        TenantId  = _tenantId,
                        Name      = permission.Name,
                        IsGranted = true,
                        RoleId    = adminRole.Id
                    });
                }

                _context.SaveChanges();
            }

            //admin user

            var adminUser = _context.Users.FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == SysUser.AdminUserName);

            if (adminUser == null)
            {
                adminUser = SysUser.CreateTenantAdminUser(_tenantId, "*****@*****.**", SysUser.DefaultPassword);
                adminUser.IsEmailConfirmed = true;
                adminUser.IsActive         = true;

                _context.Users.Add(adminUser);
                _context.SaveChanges();

                //Assign Admin role to admin user
                _context.UserRoles.Add(new UserRole(_tenantId, adminUser.Id, adminRole.Id));
                _context.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建租户
        /// </summary>
        public async Task CreateTenant(CreateTenantInput input)
        {
            //Create tenant
            var tenant = input.MapTo <SysTenant>();

            tenant.ConnectionString = null; //所有租户共享数据库
            //SimpleStringCipher.Instance.Encrypt(input.ConnectionString)

            var defaultEdition = await _editionManager.FindByNameAsync(EditionManager.DefaultEditionName);

            if (defaultEdition != null)
            {
                tenant.EditionId = defaultEdition.Id;
            }

            CheckErrors(await TenantManager.CreateAsync(tenant));
            await CurrentUnitOfWork.SaveChangesAsync(); //To get new tenant's id.

            //Create tenant database
            //_abpZeroDbMigrator.CreateOrMigrateForTenant(tenant);

            //We are working entities of new tenant, so changing tenant filter
            using (CurrentUnitOfWork.SetTenantId(tenant.Id))
            {
                //Create static roles for new tenant
                CheckErrors(await _roleManager.CreateStaticRoles(tenant.Id));

                await CurrentUnitOfWork.SaveChangesAsync(); //To get static role ids

                //grant all permissions to admin role
                var adminRole = _roleManager.Roles.Single(r => r.Name == StaticRoleNames.Tenants.Admin);
                await _roleManager.GrantAllPermissionsAsync(adminRole);

                //Create admin user for the tenant
                var adminUser = SysUser.CreateTenantAdminUser(tenant.Id, input.AdminEmailAddress, SysUser.DefaultPassword);
                CheckErrors(await UserManager.CreateAsync(adminUser));
                await CurrentUnitOfWork.SaveChangesAsync(); //To get admin user's id

                //Assign admin user to role!
                CheckErrors(await UserManager.AddToRoleAsync(adminUser.Id, adminRole.Name));
                await CurrentUnitOfWork.SaveChangesAsync();
            }
        }
        private void CreateSeedFunction()
        {
            var adminUser = _context.SysUserDbSet.IgnoreQueryFilters().FirstOrDefault(u => u.UserName == SysUser.AdminUserName);

            if (adminUser == null)
            {
                adminUser          = SysUser.CreateTenantAdminUser("*****@*****.**");
                adminUser.Password = StringHelper.MD5Encrypt64(SysUser.DefaultPassword);
                _context.SysUserDbSet.Add(adminUser);
                _context.SaveChanges();
            }
            var testUser = _context.SysUserDbSet.IgnoreQueryFilters().FirstOrDefault(u => u.UserName == "test");

            if (testUser == null)
            {
                testUser          = SysUser.CreateTenantAdminUser("*****@*****.**", "test");
                testUser.Password = StringHelper.MD5Encrypt64(SysUser.DefaultPassword);
                _context.SysUserDbSet.Add(testUser);
                _context.SaveChanges();
            }
        }