Skip to content

Commit 2a9f375

Browse files
committed
Merge branch 'dev-db-per-tenant'
2 parents fadfc91 + 3f1f0bf commit 2a9f375

File tree

81 files changed

+4238
-332
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+4238
-332
lines changed

src/AbpCompanyName.AbpProjectName.Application/AbpCompanyName.AbpProjectName.Application.csproj

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
3434
</PropertyGroup>
3535
<ItemGroup>
3636
<Reference Include="Abp, Version=0.8.3.0, Culture=neutral, processorArchitecture=MSIL">
37-
<HintPath>..\packages\Abp.0.8.3.0\lib\net452\Abp.dll</HintPath>
38-
<Private>True</Private>
37+
<SpecificVersion>False</SpecificVersion>
38+
<HintPath>..\..\..\aspnetboilerplate\src\Abp\bin\Debug\Abp.dll</HintPath>
3939
</Reference>
4040
<Reference Include="Abp.AutoMapper, Version=0.8.3.0, Culture=neutral, processorArchitecture=MSIL">
41-
<HintPath>..\packages\Abp.AutoMapper.0.8.3.0\lib\net452\Abp.AutoMapper.dll</HintPath>
42-
<Private>True</Private>
41+
<SpecificVersion>False</SpecificVersion>
42+
<HintPath>..\..\..\aspnetboilerplate\src\Abp.AutoMapper\bin\Debug\Abp.AutoMapper.dll</HintPath>
4343
</Reference>
4444
<Reference Include="Abp.Zero, Version=0.8.3.0, Culture=neutral, processorArchitecture=MSIL">
45-
<HintPath>..\packages\Abp.Zero.0.8.3.0\lib\net452\Abp.Zero.dll</HintPath>
46-
<Private>True</Private>
45+
<SpecificVersion>False</SpecificVersion>
46+
<HintPath>..\..\..\module-zero\src\Abp.Zero\bin\Debug\Abp.Zero.dll</HintPath>
4747
</Reference>
4848
<Reference Include="AutoMapper, Version=4.1.1.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
4949
<HintPath>..\packages\AutoMapper.4.1.1\lib\net45\AutoMapper.dll</HintPath>
@@ -104,6 +104,7 @@
104104
<Compile Include="Properties\AssemblyInfo.cs" />
105105
<Compile Include="Roles\IRoleAppService.cs" />
106106
<Compile Include="Roles\Dto\UpdateRolePermissionsInput.cs" />
107+
<Compile Include="Users\Dto\CreateUserInput.cs" />
107108
<Compile Include="Users\IUserAppService.cs" />
108109
<Compile Include="Roles\RoleAppService.cs" />
109110
<Compile Include="Sessions\Dto\GetCurrentLoginInformationsOutput.cs" />
@@ -113,6 +114,7 @@
113114
<Compile Include="Sessions\SessionAppService.cs" />
114115
<Compile Include="Users\Dto\ProhibitPermissionInput.cs" />
115116
<Compile Include="Users\UserAppService.cs" />
117+
<Compile Include="Users\Dto\UserListDto.cs" />
116118
</ItemGroup>
117119
<ItemGroup>
118120
<None Include="app.config" />

src/AbpCompanyName.AbpProjectName.Application/MultiTenancy/Dto/CreateTenantInput.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using System.ComponentModel.DataAnnotations;
22
using Abp.Application.Services.Dto;
3+
using Abp.AutoMapper;
4+
using Abp.MultiTenancy;
35
using AbpCompanyName.AbpProjectName.Users;
46

57
namespace AbpCompanyName.AbpProjectName.MultiTenancy.Dto
68
{
9+
[AutoMapTo(typeof(Tenant))]
710
public class CreateTenantInput : IInputDto
811
{
912
[Required]
10-
[StringLength(Tenant.MaxTenancyNameLength)]
13+
[StringLength(AbpTenantBase.MaxTenancyNameLength)]
1114
[RegularExpression(Tenant.TenancyNameRegex)]
1215
public string TenancyName { get; set; }
1316

@@ -18,5 +21,8 @@ public class CreateTenantInput : IInputDto
1821
[Required]
1922
[StringLength(User.MaxEmailAddressLength)]
2023
public string AdminEmailAddress { get; set; }
24+
25+
[MaxLength(AbpTenantBase.MaxConnectionStringLength)]
26+
public string ConnectionString { get; set; }
2127
}
2228
}

src/AbpCompanyName.AbpProjectName.Application/MultiTenancy/TenantAppService.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,37 @@
22
using System.Linq;
33
using System.Threading.Tasks;
44
using Abp.Application.Services.Dto;
5+
using Abp.Authorization;
56
using Abp.AutoMapper;
6-
using Abp.Domain.Uow;
7+
using Abp.Extensions;
8+
using Abp.MultiTenancy;
9+
using Abp.Runtime.Security;
10+
using AbpCompanyName.AbpProjectName.Authorization;
711
using AbpCompanyName.AbpProjectName.Authorization.Roles;
812
using AbpCompanyName.AbpProjectName.Editions;
913
using AbpCompanyName.AbpProjectName.MultiTenancy.Dto;
1014
using AbpCompanyName.AbpProjectName.Users;
1115

1216
namespace AbpCompanyName.AbpProjectName.MultiTenancy
1317
{
18+
[AbpAuthorize(PermissionNames.Pages_Tenants)]
1419
public class TenantAppService : AbpProjectNameAppServiceBase, ITenantAppService
1520
{
1621
private readonly TenantManager _tenantManager;
1722
private readonly RoleManager _roleManager;
1823
private readonly EditionManager _editionManager;
24+
private readonly IAbpZeroDbMigrator _abpZeroDbMigrator;
1925

20-
public TenantAppService(TenantManager tenantManager, RoleManager roleManager, EditionManager editionManager)
26+
public TenantAppService(
27+
TenantManager tenantManager,
28+
RoleManager roleManager,
29+
EditionManager editionManager,
30+
IAbpZeroDbMigrator abpZeroDbMigrator)
2131
{
2232
_tenantManager = tenantManager;
2333
_roleManager = roleManager;
2434
_editionManager = editionManager;
35+
_abpZeroDbMigrator = abpZeroDbMigrator;
2536
}
2637

2738
public ListResultOutput<TenantListDto> GetTenants()
@@ -37,7 +48,11 @@ public ListResultOutput<TenantListDto> GetTenants()
3748
public async Task CreateTenant(CreateTenantInput input)
3849
{
3950
//Create tenant
40-
var tenant = new Tenant(input.TenancyName, input.Name);
51+
var tenant = input.MapTo<Tenant>();
52+
tenant.ConnectionString = input.ConnectionString.IsNullOrEmpty()
53+
? null
54+
: SimpleStringCipher.Instance.Encrypt(input.ConnectionString);
55+
4156
var defaultEdition = await _editionManager.FindByNameAsync(EditionManager.DefaultEditionName);
4257
if (defaultEdition != null)
4358
{
@@ -47,8 +62,11 @@ public async Task CreateTenant(CreateTenantInput input)
4762
CheckErrors(await TenantManager.CreateAsync(tenant));
4863
await CurrentUnitOfWork.SaveChangesAsync(); //To get new tenant's id.
4964

65+
//Create tenant database
66+
_abpZeroDbMigrator.CreateOrMigrateForTenant(tenant);
67+
5068
//We are working entities of new tenant, so changing tenant filter
51-
using (CurrentUnitOfWork.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, tenant.Id))
69+
using (CurrentUnitOfWork.SetTenantId(tenant.Id))
5270
{
5371
//Create static roles for new tenant
5472
CheckErrors(await _roleManager.CreateStaticRoles(tenant.Id));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Abp.Application.Services.Dto;
3+
using Abp.Authorization.Users;
4+
using Abp.AutoMapper;
5+
6+
namespace AbpCompanyName.AbpProjectName.Users.Dto
7+
{
8+
[AutoMap(typeof(User))]
9+
public class CreateUserInput : IInputDto
10+
{
11+
[Required]
12+
[StringLength(AbpUserBase.MaxUserNameLength)]
13+
public string UserName { get; set; }
14+
15+
[Required]
16+
[StringLength(User.MaxNameLength)]
17+
public string Name { get; set; }
18+
19+
[Required]
20+
[StringLength(User.MaxSurnameLength)]
21+
public string Surname { get; set; }
22+
23+
[Required]
24+
[EmailAddress]
25+
[StringLength(AbpUserBase.MaxEmailAddressLength)]
26+
public string EmailAddress { get; set; }
27+
28+
[Required]
29+
[StringLength(User.MaxPlainPasswordLength)]
30+
public string Password { get; set; }
31+
32+
public bool IsActive { get; set; }
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using Abp.Application.Services.Dto;
3+
using Abp.AutoMapper;
4+
5+
namespace AbpCompanyName.AbpProjectName.Users.Dto
6+
{
7+
[AutoMapFrom(typeof(User))]
8+
public class UserListDto : EntityDto<long>
9+
{
10+
public string Name { get; set; }
11+
12+
public string Surname { get; set; }
13+
14+
public string UserName { get; set; }
15+
16+
public string FullName { get; set; }
17+
18+
public string EmailAddress { get; set; }
19+
20+
public bool IsEmailConfirmed { get; set; }
21+
22+
public DateTime? LastLoginTime { get; set; }
23+
24+
public bool IsActive { get; set; }
25+
26+
public DateTime CreationTime { get; set; }
27+
}
28+
}

src/AbpCompanyName.AbpProjectName.Application/Users/IUserAppService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Threading.Tasks;
22
using Abp.Application.Services;
3+
using Abp.Application.Services.Dto;
34
using AbpCompanyName.AbpProjectName.Users.Dto;
45

56
namespace AbpCompanyName.AbpProjectName.Users
@@ -9,5 +10,9 @@ public interface IUserAppService : IApplicationService
910
Task ProhibitPermission(ProhibitPermissionInput input);
1011

1112
Task RemoveFromRole(long userId, string roleName);
13+
14+
Task<ListResultOutput<UserListDto>> GetUsers();
15+
16+
Task CreateUser(CreateUserInput input);
1217
}
1318
}
Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,60 @@
1+
using System.Collections.Generic;
12
using System.Threading.Tasks;
3+
using Abp.Application.Services.Dto;
24
using Abp.Authorization;
5+
using Abp.AutoMapper;
6+
using Abp.Domain.Repositories;
7+
using AbpCompanyName.AbpProjectName.Authorization;
38
using AbpCompanyName.AbpProjectName.Users.Dto;
9+
using Microsoft.AspNet.Identity;
410

511
namespace AbpCompanyName.AbpProjectName.Users
612
{
713
/* THIS IS JUST A SAMPLE. */
14+
[AbpAuthorize(PermissionNames.Pages_Users)]
815
public class UserAppService : AbpProjectNameAppServiceBase, IUserAppService
916
{
10-
private readonly UserManager _userManager;
17+
private readonly IRepository<User, long> _userRepository;
1118
private readonly IPermissionManager _permissionManager;
1219

13-
public UserAppService(UserManager userManager, IPermissionManager permissionManager)
20+
public UserAppService(IRepository<User, long> userRepository, IPermissionManager permissionManager)
1421
{
15-
_userManager = userManager;
22+
_userRepository = userRepository;
1623
_permissionManager = permissionManager;
1724
}
1825

1926
public async Task ProhibitPermission(ProhibitPermissionInput input)
2027
{
21-
var user = await _userManager.GetUserByIdAsync(input.UserId);
28+
var user = await UserManager.GetUserByIdAsync(input.UserId);
2229
var permission = _permissionManager.GetPermission(input.PermissionName);
2330

24-
await _userManager.ProhibitPermissionAsync(user, permission);
31+
await UserManager.ProhibitPermissionAsync(user, permission);
2532
}
2633

2734
//Example for primitive method parameters.
2835
public async Task RemoveFromRole(long userId, string roleName)
2936
{
30-
CheckErrors(await _userManager.RemoveFromRoleAsync(userId, roleName));
37+
CheckErrors(await UserManager.RemoveFromRoleAsync(userId, roleName));
38+
}
39+
40+
public async Task<ListResultOutput<UserListDto>> GetUsers()
41+
{
42+
var users = await _userRepository.GetAllListAsync();
43+
44+
return new ListResultOutput<UserListDto>(
45+
users.MapTo<List<UserListDto>>()
46+
);
47+
}
48+
49+
public async Task CreateUser(CreateUserInput input)
50+
{
51+
var user = input.MapTo<User>();
52+
53+
user.TenantId = AbpSession.TenantId;
54+
user.Password = new PasswordHasher().HashPassword(input.Password);
55+
user.IsEmailConfirmed = true;
56+
57+
await UserManager.CreateAsync(user);
3158
}
3259
}
3360
}

src/AbpCompanyName.AbpProjectName.Core/AbpCompanyName.AbpProjectName.Core.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
</PropertyGroup>
3535
<ItemGroup>
3636
<Reference Include="Abp, Version=0.8.3.0, Culture=neutral, processorArchitecture=MSIL">
37-
<HintPath>..\packages\Abp.0.8.3.0\lib\net452\Abp.dll</HintPath>
38-
<Private>True</Private>
37+
<SpecificVersion>False</SpecificVersion>
38+
<HintPath>..\..\..\aspnetboilerplate\src\Abp\bin\Debug\Abp.dll</HintPath>
3939
</Reference>
4040
<Reference Include="Abp.Zero, Version=0.8.3.0, Culture=neutral, processorArchitecture=MSIL">
41-
<HintPath>..\packages\Abp.Zero.0.8.3.0\lib\net452\Abp.Zero.dll</HintPath>
42-
<Private>True</Private>
41+
<SpecificVersion>False</SpecificVersion>
42+
<HintPath>..\..\..\module-zero\src\Abp.Zero\bin\Debug\Abp.Zero.dll</HintPath>
4343
</Reference>
4444
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
4545
<SpecificVersion>False</SpecificVersion>

src/AbpCompanyName.AbpProjectName.Core/Authorization/AbpProjectNameAuthorizationProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public override void SetPermissions(IPermissionDefinitionContext context)
1515
pages = context.CreatePermission(PermissionNames.Pages, L("Pages"));
1616
}
1717

18+
var users = pages.CreateChildPermission(PermissionNames.Pages_Users, L("Users"));
19+
1820
//Host permissions
1921
var tenants = pages.CreateChildPermission(PermissionNames.Pages_Tenants, L("Tenants"), multiTenancySides: MultiTenancySides.Host);
2022
}

src/AbpCompanyName.AbpProjectName.Core/Authorization/PermissionNames.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ public static class PermissionNames
55
public const string Pages = "Pages";
66

77
public const string Pages_Tenants = "Pages.Tenants";
8+
9+
public const string Pages_Users = "Pages.Users";
810
}
911
}

0 commit comments

Comments
 (0)