Skip to content

Commit db02efa

Browse files
committed
All modules added. Project is building.
1 parent 7a03001 commit db02efa

File tree

135 files changed

+5528
-206
lines changed

Some content is hidden

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

135 files changed

+5528
-206
lines changed

AbpCompanyName.AbpProjectName.sln

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25123.0
4+
VisualStudioVersion = 14.0.25420.1
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}"
77
EndProject
@@ -16,14 +16,16 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectNa
1616
EndProject
1717
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.Application", "src\AbpCompanyName.AbpProjectName.Application\AbpCompanyName.AbpProjectName.Application.xproj", "{3870C648-4AEA-4B85-BA3F-F2F63B96136A}"
1818
EndProject
19-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.EntityFrameworkCore", "src\AbpCompanyName.AbpProjectName.EntityFrameworkCore\AbpCompanyName.AbpProjectName.EntityFrameworkCore.xproj", "{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}"
20-
EndProject
2119
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F10AA149-2626-486E-85BB-9CD5365F3016}"
2220
EndProject
2321
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.Tests", "test\AbpCompanyName.AbpProjectName.Tests\AbpCompanyName.AbpProjectName.Tests.xproj", "{0D4C5D00-C144-4213-A007-4B8944113AB1}"
2422
EndProject
2523
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.Web.Tests", "test\AbpCompanyName.AbpProjectName.Web.Tests\AbpCompanyName.AbpProjectName.Web.Tests.xproj", "{5D441612-29CA-4DAD-9945-B9DE11CE026C}"
2624
EndProject
25+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.EntityFramework", "src\AbpCompanyName.AbpProjectName.EntityFramework\AbpCompanyName.AbpProjectName.EntityFramework.xproj", "{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}"
26+
EndProject
27+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.Migrator", "src\AbpCompanyName.AbpProjectName.Migrator\AbpCompanyName.AbpProjectName.Migrator.xproj", "{880B3591-E057-46FE-B525-10BD83828B93}"
28+
EndProject
2729
Global
2830
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2931
Debug|Any CPU = Debug|Any CPU
@@ -42,10 +44,6 @@ Global
4244
{3870C648-4AEA-4B85-BA3F-F2F63B96136A}.Debug|Any CPU.Build.0 = Debug|Any CPU
4345
{3870C648-4AEA-4B85-BA3F-F2F63B96136A}.Release|Any CPU.ActiveCfg = Release|Any CPU
4446
{3870C648-4AEA-4B85-BA3F-F2F63B96136A}.Release|Any CPU.Build.0 = Release|Any CPU
45-
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
46-
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
47-
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
48-
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Release|Any CPU.Build.0 = Release|Any CPU
4947
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5048
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
5149
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -54,6 +52,14 @@ Global
5452
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Debug|Any CPU.Build.0 = Debug|Any CPU
5553
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Release|Any CPU.ActiveCfg = Release|Any CPU
5654
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Release|Any CPU.Build.0 = Release|Any CPU
55+
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
56+
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
57+
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
58+
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Release|Any CPU.Build.0 = Release|Any CPU
59+
{880B3591-E057-46FE-B525-10BD83828B93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
60+
{880B3591-E057-46FE-B525-10BD83828B93}.Debug|Any CPU.Build.0 = Debug|Any CPU
61+
{880B3591-E057-46FE-B525-10BD83828B93}.Release|Any CPU.ActiveCfg = Release|Any CPU
62+
{880B3591-E057-46FE-B525-10BD83828B93}.Release|Any CPU.Build.0 = Release|Any CPU
5763
EndGlobalSection
5864
GlobalSection(SolutionProperties) = preSolution
5965
HideSolutionNode = FALSE
@@ -62,8 +68,9 @@ Global
6268
{A2213374-BB48-48FD-BBD4-81E6A961D866} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
6369
{0FA75A5B-AB83-4FD0-B545-279774C01E87} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
6470
{3870C648-4AEA-4B85-BA3F-F2F63B96136A} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
65-
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
6671
{0D4C5D00-C144-4213-A007-4B8944113AB1} = {F10AA149-2626-486E-85BB-9CD5365F3016}
6772
{5D441612-29CA-4DAD-9945-B9DE11CE026C} = {F10AA149-2626-486E-85BB-9CD5365F3016}
73+
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
74+
{880B3591-E057-46FE-B525-10BD83828B93} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
6875
EndGlobalSection
6976
EndGlobal
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
using Abp.Application.Services;
1+
using System;
2+
using System.Threading.Tasks;
3+
using Abp.Application.Services;
4+
using AbpCompanyName.AbpProjectName.MultiTenancy;
5+
using AbpCompanyName.AbpProjectName.Users;
6+
using Microsoft.AspNet.Identity;
7+
using Abp.Runtime.Session;
8+
using Abp.IdentityFramework;
29

310
namespace AbpCompanyName.AbpProjectName
411
{
@@ -7,9 +14,34 @@ namespace AbpCompanyName.AbpProjectName
714
/// </summary>
815
public abstract class AbpProjectNameAppServiceBase : ApplicationService
916
{
17+
public TenantManager TenantManager { get; set; }
18+
19+
public UserManager UserManager { get; set; }
20+
1021
protected AbpProjectNameAppServiceBase()
1122
{
1223
LocalizationSourceName = AbpProjectNameConsts.LocalizationSourceName;
1324
}
25+
26+
protected virtual Task<User> GetCurrentUserAsync()
27+
{
28+
var user = UserManager.FindByIdAsync(AbpSession.GetUserId());
29+
if (user == null)
30+
{
31+
throw new ApplicationException("There is no current user!");
32+
}
33+
34+
return user;
35+
}
36+
37+
protected virtual Task<Tenant> GetCurrentTenantAsync()
38+
{
39+
return TenantManager.GetByIdAsync(AbpSession.GetTenantId());
40+
}
41+
42+
protected virtual void CheckErrors(IdentityResult identityResult)
43+
{
44+
identityResult.CheckErrors(LocalizationManager);
45+
}
1446
}
1547
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using Abp.Authorization.Users;
3+
using Abp.UI;
4+
5+
namespace AbpCompanyName.AbpProjectName.Authorization
6+
{
7+
public class AbpLoginResultTypeHelper : AbpProjectNameAppServiceBase
8+
{
9+
public Exception CreateExceptionForFailedLoginAttempt(AbpLoginResultType result, string usernameOrEmailAddress, string tenancyName)
10+
{
11+
switch (result)
12+
{
13+
case AbpLoginResultType.Success:
14+
return new ApplicationException("Don't call this method with a success result!");
15+
case AbpLoginResultType.InvalidUserNameOrEmailAddress:
16+
case AbpLoginResultType.InvalidPassword:
17+
return new UserFriendlyException(L("LoginFailed"), L("InvalidUserNameOrPassword"));
18+
case AbpLoginResultType.InvalidTenancyName:
19+
return new UserFriendlyException(L("LoginFailed"), L("ThereIsNoTenantDefinedWithName{0}", tenancyName));
20+
case AbpLoginResultType.TenantIsNotActive:
21+
return new UserFriendlyException(L("LoginFailed"), L("TenantIsNotActive", tenancyName));
22+
case AbpLoginResultType.UserIsNotActive:
23+
return new UserFriendlyException(L("LoginFailed"), L("UserIsNotActiveAndCanNotLogin", usernameOrEmailAddress));
24+
case AbpLoginResultType.UserEmailIsNotConfirmed:
25+
return new UserFriendlyException(L("LoginFailed"), L("UserEmailIsNotConfirmedAndCanNotLogin"));
26+
default: //Can not fall to default actually. But other result types can be added in the future and we may forget to handle it
27+
Logger.Warn("Unhandled login fail reason: " + result);
28+
return new UserFriendlyException(L("LoginFailed"));
29+
}
30+
}
31+
32+
public string CreateLocalizedMessageForFailedLoginAttempt(AbpLoginResultType result, string usernameOrEmailAddress, string tenancyName)
33+
{
34+
switch (result)
35+
{
36+
case AbpLoginResultType.Success:
37+
throw new ApplicationException("Don't call this method with a success result!");
38+
case AbpLoginResultType.InvalidUserNameOrEmailAddress:
39+
case AbpLoginResultType.InvalidPassword:
40+
return L("InvalidUserNameOrPassword");
41+
case AbpLoginResultType.InvalidTenancyName:
42+
return L("ThereIsNoTenantDefinedWithName{0}", tenancyName);
43+
case AbpLoginResultType.TenantIsNotActive:
44+
return L("TenantIsNotActive", tenancyName);
45+
case AbpLoginResultType.UserIsNotActive:
46+
return L("UserIsNotActiveAndCanNotLogin", usernameOrEmailAddress);
47+
case AbpLoginResultType.UserEmailIsNotConfirmed:
48+
return L("UserEmailIsNotConfirmedAndCanNotLogin");
49+
default: //Can not fall to default actually. But other result types can be added in the future and we may forget to handle it
50+
Logger.Warn("Unhandled login fail reason: " + result);
51+
return L("LoginFailed");
52+
}
53+
}
54+
}
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Abp.Application.Services.Dto;
3+
using Abp.AutoMapper;
4+
using Abp.MultiTenancy;
5+
using AbpCompanyName.AbpProjectName.Users;
6+
7+
namespace AbpCompanyName.AbpProjectName.MultiTenancy.Dto
8+
{
9+
[AutoMapTo(typeof(Tenant))]
10+
public class CreateTenantInput : IInputDto
11+
{
12+
[Required]
13+
[StringLength(AbpTenantBase.MaxTenancyNameLength)]
14+
[RegularExpression(Tenant.TenancyNameRegex)]
15+
public string TenancyName { get; set; }
16+
17+
[Required]
18+
[StringLength(Tenant.MaxNameLength)]
19+
public string Name { get; set; }
20+
21+
[Required]
22+
[StringLength(User.MaxEmailAddressLength)]
23+
public string AdminEmailAddress { get; set; }
24+
25+
[MaxLength(AbpTenantBase.MaxConnectionStringLength)]
26+
public string ConnectionString { get; set; }
27+
}
28+
}
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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Abp.Application.Services.Dto;
5+
using Abp.Authorization;
6+
using Abp.AutoMapper;
7+
using Abp.Extensions;
8+
using Abp.MultiTenancy;
9+
using Abp.Runtime.Security;
10+
using AbpCompanyName.AbpProjectName.Authorization;
11+
using AbpCompanyName.AbpProjectName.Authorization.Roles;
12+
using AbpCompanyName.AbpProjectName.Editions;
13+
using AbpCompanyName.AbpProjectName.MultiTenancy.Dto;
14+
using AbpCompanyName.AbpProjectName.Users;
15+
16+
namespace AbpCompanyName.AbpProjectName.MultiTenancy
17+
{
18+
[AbpAuthorize(PermissionNames.Pages_Tenants)]
19+
public class TenantAppService : AbpProjectNameAppServiceBase, ITenantAppService
20+
{
21+
private readonly TenantManager _tenantManager;
22+
private readonly RoleManager _roleManager;
23+
private readonly EditionManager _editionManager;
24+
private readonly IAbpZeroDbMigrator _abpZeroDbMigrator;
25+
26+
public TenantAppService(
27+
TenantManager tenantManager,
28+
RoleManager roleManager,
29+
EditionManager editionManager,
30+
IAbpZeroDbMigrator abpZeroDbMigrator)
31+
{
32+
_tenantManager = tenantManager;
33+
_roleManager = roleManager;
34+
_editionManager = editionManager;
35+
_abpZeroDbMigrator = abpZeroDbMigrator;
36+
}
37+
38+
public ListResultOutput<TenantListDto> GetTenants()
39+
{
40+
return new ListResultOutput<TenantListDto>(
41+
_tenantManager.Tenants
42+
.OrderBy(t => t.TenancyName)
43+
.ToList()
44+
.MapTo<List<TenantListDto>>()
45+
);
46+
}
47+
48+
public async Task CreateTenant(CreateTenantInput input)
49+
{
50+
//Create tenant
51+
var tenant = input.MapTo<Tenant>();
52+
tenant.ConnectionString = input.ConnectionString.IsNullOrEmpty()
53+
? null
54+
: SimpleStringCipher.Instance.Encrypt(input.ConnectionString);
55+
56+
var defaultEdition = await _editionManager.FindByNameAsync(EditionManager.DefaultEditionName);
57+
if (defaultEdition != null)
58+
{
59+
tenant.EditionId = defaultEdition.Id;
60+
}
61+
62+
CheckErrors(await TenantManager.CreateAsync(tenant));
63+
await CurrentUnitOfWork.SaveChangesAsync(); //To get new tenant's id.
64+
65+
//Create tenant database
66+
_abpZeroDbMigrator.CreateOrMigrateForTenant(tenant);
67+
68+
//We are working entities of new tenant, so changing tenant filter
69+
using (CurrentUnitOfWork.SetTenantId(tenant.Id))
70+
{
71+
//Create static roles for new tenant
72+
CheckErrors(await _roleManager.CreateStaticRoles(tenant.Id));
73+
74+
await CurrentUnitOfWork.SaveChangesAsync(); //To get static role ids
75+
76+
//grant all permissions to admin role
77+
var adminRole = _roleManager.Roles.Single(r => r.Name == StaticRoleNames.Tenants.Admin);
78+
await _roleManager.GrantAllPermissionsAsync(adminRole);
79+
80+
//Create admin user for the tenant
81+
var adminUser = User.CreateTenantAdminUser(tenant.Id, input.AdminEmailAddress, User.DefaultPassword);
82+
CheckErrors(await UserManager.CreateAsync(adminUser));
83+
await CurrentUnitOfWork.SaveChangesAsync(); //To get admin user's id
84+
85+
//Assign admin user to role!
86+
CheckErrors(await UserManager.AddToRoleAsync(adminUser.Id, adminRole.Name));
87+
await CurrentUnitOfWork.SaveChangesAsync();
88+
}
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)