/// <summary> /// 注册租户信息 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task <TenantUpdateInput> RegisterTenantAsync(TenantCreateInput input) { //创建租户信息 Tenant tenant = new Tenant(input.TenancyName, input.TenancyName) { IsActive = true }; tenant.ConnectionString = input.ConnectionString.IsNullOrEmpty() ? null : SimpleStringCipher.Instance.Encrypt(input.ConnectionString); Abp.Application.Editions.Edition defaultEdition = await _editionManager.FindByNameAsync(EditionManager.DefaultEditionName); if (defaultEdition != null) { tenant.EditionId = defaultEdition.Id; } await TenantManager.CreateAsync(tenant); // 保存以获取新租户的Id await CurrentUnitOfWork.SaveChangesAsync(); // 创建租户数据库 _abpZeroDbMigrator.CreateOrMigrateForTenant(tenant); //创建成功后,需要设置当前工作单元为当前登录后的租户信息 using (CurrentUnitOfWork.SetTenantId(tenant.Id)) { // 给新租户创建角色 CheckErrors(await _roleManager.CreateStaticRoles(tenant.Id)); // 保存,获取角色id await CurrentUnitOfWork.SaveChangesAsync(); // 分配权限 Role adminRole = _roleManager.Roles.Single(r => r.Name == StaticRoleNames.Tenants.Admin); await _roleManager.GrantAllPermissionsAsync(adminRole); // 创建此租户的管理员用户 var adminUser = User.CreateTenantAdminUser(tenant.Id, input.AdminEmailAddress); // 如果没有提交密码,那么走的是默认密码 123qwe adminUser.Password = _passwordHasher.HashPassword(adminUser, input.TenantAdminPassword.IsNullOrWhiteSpace() ? User.DefaultPassword : input.TenantAdminPassword); CheckErrors(await UserManager.CreateAsync(adminUser)); // 保存,获取角色id await CurrentUnitOfWork.SaveChangesAsync(); // 授权 CheckErrors(await UserManager.AddToRoleAsync(adminUser, adminRole.Name)); await CurrentUnitOfWork.SaveChangesAsync(); } return(tenant.MapTo <TenantUpdateInput>()); }
public async Task CreateAsync_Test(string tenancyName, string name, string adminEmailAddress, string connectionString, bool isActive) { LoginAsHost("admin"); var createInput = new TenantCreateInput() { Name = name, ConnectionString = connectionString, TenancyName = tenancyName, AdminEmailAddress = adminEmailAddress, IsActive = isActive }; var result = await tenantAppService.CreateAsync(createInput); result.ShouldBeNull(); Assert.True(result == null); }
public virtual Task <TenantResult> CreateAsync(TenantCreateInput input) { return(_service.CreateAsync(input)); }