Skip to content

Commit d6f8490

Browse files
committed
Resolved #14: Implement a basic tenant list & create page.
1 parent a83e998 commit d6f8490

File tree

29 files changed

+545
-31
lines changed

29 files changed

+545
-31
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@
9696
<ItemGroup>
9797
<Compile Include="AbpProjectNameApplicationModule.cs" />
9898
<Compile Include="AbpProjectNameAppServiceBase.cs" />
99+
<Compile Include="MultiTenancy\Dto\CreateTenantInput.cs" />
100+
<Compile Include="MultiTenancy\ITenantAppService.cs" />
101+
<Compile Include="MultiTenancy\Dto\TenantListDto.cs" />
102+
<Compile Include="MultiTenancy\TenantAppService.cs" />
99103
<Compile Include="Properties\AssemblyInfo.cs" />
100104
<Compile Include="Roles\IRoleAppService.cs" />
101105
<Compile Include="Roles\Dto\UpdateRolePermissionsInput.cs" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Abp.Application.Services.Dto;
3+
using AbpCompanyName.AbpProjectName.Users;
4+
5+
namespace AbpCompanyName.AbpProjectName.MultiTenancy.Dto
6+
{
7+
public class CreateTenantInput : IInputDto
8+
{
9+
[Required]
10+
[StringLength(Tenant.MaxTenancyNameLength)]
11+
[RegularExpression(Tenant.TenancyNameRegex)]
12+
public string TenancyName { get; set; }
13+
14+
[Required]
15+
[StringLength(Tenant.MaxNameLength)]
16+
public string Name { get; set; }
17+
18+
[Required]
19+
[StringLength(User.MaxEmailAddressLength)]
20+
public string AdminEmailAddress { get; set; }
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Abp.Application.Services.Dto;
2+
using Abp.AutoMapper;
3+
4+
namespace AbpCompanyName.AbpProjectName.MultiTenancy.Dto
5+
{
6+
[AutoMapFrom(typeof(Tenant))]
7+
public class TenantListDto : EntityDto
8+
{
9+
public string TenancyName { get; set; }
10+
11+
public string Name { get; set; }
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Threading.Tasks;
2+
using Abp.Application.Services;
3+
using Abp.Application.Services.Dto;
4+
using AbpCompanyName.AbpProjectName.MultiTenancy.Dto;
5+
6+
namespace AbpCompanyName.AbpProjectName.MultiTenancy
7+
{
8+
public interface ITenantAppService : IApplicationService
9+
{
10+
ListResultOutput<TenantListDto> GetTenants();
11+
12+
Task CreateTenant(CreateTenantInput input);
13+
}
14+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Abp.Application.Services.Dto;
5+
using Abp.AutoMapper;
6+
using Abp.Domain.Uow;
7+
using AbpCompanyName.AbpProjectName.Authorization.Roles;
8+
using AbpCompanyName.AbpProjectName.MultiTenancy.Dto;
9+
using AbpCompanyName.AbpProjectName.Users;
10+
11+
namespace AbpCompanyName.AbpProjectName.MultiTenancy
12+
{
13+
public class TenantAppService : AbpProjectNameAppServiceBase, ITenantAppService
14+
{
15+
private readonly TenantManager _tenantManager;
16+
private readonly RoleManager _roleManager;
17+
18+
public TenantAppService(TenantManager tenantManager, RoleManager roleManager)
19+
{
20+
_tenantManager = tenantManager;
21+
_roleManager = roleManager;
22+
}
23+
24+
public ListResultOutput<TenantListDto> GetTenants()
25+
{
26+
return new ListResultOutput<TenantListDto>(
27+
_tenantManager.Tenants
28+
.OrderBy(t => t.TenancyName)
29+
.ToList()
30+
.MapTo<List<TenantListDto>>()
31+
);
32+
}
33+
34+
public async Task CreateTenant(CreateTenantInput input)
35+
{
36+
//Create tenant
37+
var tenant = new Tenant(input.TenancyName, input.Name);
38+
CheckErrors(await TenantManager.CreateAsync(tenant));
39+
await CurrentUnitOfWork.SaveChangesAsync(); //To get new tenant's id.
40+
41+
//We are working entities of new tenant, so changing tenant filter
42+
using (CurrentUnitOfWork.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, tenant.Id))
43+
{
44+
//Create static roles for new tenant
45+
CheckErrors(await _roleManager.CreateStaticRoles(tenant.Id));
46+
47+
await CurrentUnitOfWork.SaveChangesAsync(); //To get static role ids
48+
49+
//grant all permissions to admin role
50+
var adminRole = _roleManager.Roles.Single(r => r.Name == StaticRoleNames.Tenants.Admin);
51+
await _roleManager.GrantAllPermissionsAsync(adminRole);
52+
53+
//Create admin user for the tenant
54+
var adminUser = User.CreateTenantAdminUser(tenant.Id, input.AdminEmailAddress, User.DefaultPassword);
55+
CheckErrors(await UserManager.CreateAsync(adminUser));
56+
await CurrentUnitOfWork.SaveChangesAsync(); //To get admin user's id
57+
58+
//Assign admin user to role!
59+
CheckErrors(await UserManager.AddToRoleAsync(adminUser.Id, adminRole.Name));
60+
await CurrentUnitOfWork.SaveChangesAsync();
61+
}
62+
}
63+
}
64+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@
8888
<ItemGroup>
8989
<Compile Include="AbpProjectNameConsts.cs" />
9090
<Compile Include="AbpProjectNameCoreModule.cs" />
91+
<Compile Include="Authorization\AbpProjectNameAuthorizationProvider.cs" />
9192
<Compile Include="Authorization\PermissionChecker.cs" />
93+
<Compile Include="Authorization\PermissionNames.cs" />
94+
<Compile Include="Authorization\Roles\AppRoleConfig.cs" />
95+
<Compile Include="Authorization\Roles\StaticRoleNames.cs" />
9296
<Compile Include="Editions\EditionManager.cs" />
9397
<Compile Include="Features\FeatureValueStore.cs" />
9498
<Compile Include="MultiTenancy\Tenant.cs" />

src/AbpCompanyName.AbpProjectName.Core/AbpProjectNameCoreModule.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Reflection;
22
using Abp.Localization.Dictionaries;
33
using Abp.Localization.Dictionaries.Xml;
4-
using Abp.Localization.Sources;
5-
using Abp.Localization.Sources.Xml;
64
using Abp.Modules;
75
using Abp.Zero;
6+
using Abp.Zero.Configuration;
7+
using AbpCompanyName.AbpProjectName.Authorization;
8+
using AbpCompanyName.AbpProjectName.Authorization.Roles;
89

910
namespace AbpCompanyName.AbpProjectName
1011
{
@@ -26,6 +27,10 @@ public override void PreInitialize()
2627
)
2728
)
2829
);
30+
31+
AppRoleConfig.Configure(Configuration.Modules.Zero().RoleManagement);
32+
33+
Configuration.Authorization.Providers.Add<AbpProjectNameAuthorizationProvider>();
2934
}
3035

3136
public override void Initialize()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Abp.Authorization;
2+
using Abp.Localization;
3+
using Abp.MultiTenancy;
4+
5+
namespace AbpCompanyName.AbpProjectName.Authorization
6+
{
7+
public class AbpProjectNameAuthorizationProvider : AuthorizationProvider
8+
{
9+
public override void SetPermissions(IPermissionDefinitionContext context)
10+
{
11+
//Common permissions
12+
var pages = context.GetPermissionOrNull(PermissionNames.Pages);
13+
if (pages == null)
14+
{
15+
pages = context.CreatePermission(PermissionNames.Pages, L("Pages"));
16+
}
17+
18+
//Host permissions
19+
var tenants = pages.CreateChildPermission(PermissionNames.Pages_Tenants, L("Tenants"), multiTenancySides: MultiTenancySides.Host);
20+
}
21+
22+
private static ILocalizableString L(string name)
23+
{
24+
return new LocalizableString(name, AbpProjectNameConsts.LocalizationSourceName);
25+
}
26+
}
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace AbpCompanyName.AbpProjectName.Authorization
2+
{
3+
public static class PermissionNames
4+
{
5+
public const string Pages = "Pages";
6+
7+
public const string Pages_Tenants = "Pages.Tenants";
8+
}
9+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Abp.MultiTenancy;
2+
using Abp.Zero.Configuration;
3+
4+
namespace AbpCompanyName.AbpProjectName.Authorization.Roles
5+
{
6+
public static class AppRoleConfig
7+
{
8+
public static void Configure(IRoleManagementConfig roleManagementConfig)
9+
{
10+
//Static host roles
11+
12+
roleManagementConfig.StaticRoles.Add(
13+
new StaticRoleDefinition(
14+
StaticRoleNames.Host.Admin,
15+
MultiTenancySides.Host)
16+
);
17+
18+
//Static tenant roles
19+
20+
roleManagementConfig.StaticRoles.Add(
21+
new StaticRoleDefinition(
22+
StaticRoleNames.Tenants.Admin,
23+
MultiTenancySides.Tenant)
24+
);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)