1- using System . Transactions ;
2- using Shuttle . Access . Messages . v1 ;
3- using Shuttle . Contract ;
1+ using Shuttle . Access . Messages . v1 ;
42using Shuttle . Mediator ;
53using Shuttle . Hopper ;
64
75namespace Shuttle . Access . Server . v1 . MessageHandlers ;
86
9- public class RegisterTenantHandler ( ITenantQuery tenantQuery , IRoleQuery roleQuery , IIdentityQuery identityQuery , IMediator mediator ) : IMessageHandler < RegisterTenant >
7+ public class RegisterTenantHandler ( ITenantQuery tenantQuery , IRoleQuery roleQuery , IPermissionQuery permissionQuery , IIdentityQuery identityQuery , IMediator mediator , IBus bus ) : IMessageHandler < RegisterTenant >
108{
11- private readonly IIdentityQuery _identityQuery = Guard . AgainstNull ( identityQuery ) ;
12- private readonly IMediator _mediator = Guard . AgainstNull ( mediator ) ;
13- private readonly IRoleQuery _roleQuery = Guard . AgainstNull ( roleQuery ) ;
14- private readonly ITenantQuery _tenantQuery = Guard . AgainstNull ( tenantQuery ) ;
15-
169 public async Task HandleAsync ( RegisterTenant message , CancellationToken cancellationToken = default )
1710 {
18- Guard . AgainstNull ( message ) ;
11+ ArgumentNullException . ThrowIfNull ( tenantQuery ) ;
12+ ArgumentNullException . ThrowIfNull ( roleQuery ) ;
13+ ArgumentNullException . ThrowIfNull ( permissionQuery ) ;
14+ ArgumentNullException . ThrowIfNull ( identityQuery ) ;
15+ ArgumentNullException . ThrowIfNull ( mediator ) ;
16+ ArgumentNullException . ThrowIfNull ( bus ) ;
17+ ArgumentNullException . ThrowIfNull ( message ) ;
1918
20- var identity = ( await _identityQuery . SearchAsync ( new Query . Identity . Specification ( ) . WithName ( message . AdministratorIdentityName ) , cancellationToken ) ) . FirstOrDefault ( ) ;
19+ var identity = ( await identityQuery . SearchAsync ( new Query . Identity . Specification ( ) . WithName ( message . AdministratorIdentityName ) , cancellationToken ) ) . FirstOrDefault ( ) ;
2120
2221 if ( identity == null )
2322 {
24- return ;
23+ throw new ApplicationException ( $ "Could not find the administrator identity with name ' { message . AdministratorIdentityName } '." ) ;
2524 }
2625
27- using ( new TransactionScope ( TransactionScopeOption . Suppress , TransactionScopeAsyncFlowOption . Enabled ) )
26+ var accessAdministratorPermission = ( await permissionQuery . SearchAsync ( new Query . Permission . Specification ( ) . AddName ( AccessPermissions . Administrator ) , cancellationToken ) ) . FirstOrDefault ( ) ;
27+
28+ if ( accessAdministratorPermission == null )
2829 {
29- await mediator . SendAsync ( message , cancellationToken ) ;
30+ throw new ApplicationException ( $ "Could not find the Access administrator permission ' { AccessPermissions . Administrator } '." ) ;
3031 }
3132
33+ var registerTenant = new Application . RegisterTenant ( message . Id , message . Name , ( TenantStatus ) message . Status , message . AuditTenantId , message . AuditIdentityName )
34+ {
35+ LogoUrl = message . LogoUrl ,
36+ LogoSvg = message . LogoSvg
37+ } ;
38+
39+ await mediator . SendAsync ( registerTenant , cancellationToken ) ;
40+
3241 Query . Tenant ? tenant ;
3342
3443 var timeout = DateTime . UtcNow . AddSeconds ( 15 ) ;
44+
3545 do
3646 {
37- tenant = ( await _tenantQuery . SearchAsync ( new Query . Tenant . Specification ( ) . AddId ( message . Id ) , cancellationToken ) ) . FirstOrDefault ( ) ;
47+ tenant = ( await tenantQuery . SearchAsync ( new Query . Tenant . Specification ( ) . AddId ( message . Id ) , cancellationToken ) ) . FirstOrDefault ( ) ;
3848
3949 if ( tenant == null )
4050 {
@@ -49,19 +59,18 @@ public async Task HandleAsync(RegisterTenant message, CancellationToken cancella
4959
5060 var auditInformation = new AuditInformation ( message . Id , "system" ) ;
5161
52- var registerRoleMessage = new Application . RegisterRole ( Guid . NewGuid ( ) , message . Id , "Access Administrator" , message . AuditTenantId , message . AuditIdentityName ) ;
62+ var registerRoleMessage = new Application . RegisterRole ( message . AccessAdministratorRoleId , message . Id , "Access Administrator" , message . AuditTenantId , message . AuditIdentityName ) ;
5363
54- using ( new TransactionScope ( TransactionScopeOption . Suppress , TransactionScopeAsyncFlowOption . Enabled ) )
55- {
56- await _mediator . SendAsync ( registerRoleMessage , cancellationToken ) ;
57- }
64+ registerRoleMessage . AddPermissionId ( accessAdministratorPermission . Id ) ;
65+
66+ await mediator . SendAsync ( registerRoleMessage , cancellationToken ) ;
5867
5968 Query . Role ? role ;
6069
6170 timeout = DateTime . UtcNow . AddSeconds ( 15 ) ;
6271 do
6372 {
64- role = ( await _roleQuery . SearchAsync ( new Query . Role . Specification ( ) . AddName ( "Access Administrator" ) . WithTenantId ( message . Id ) , cancellationToken ) ) . FirstOrDefault ( ) ;
73+ role = ( await roleQuery . SearchAsync ( new Query . Role . Specification ( ) . AddId ( message . AccessAdministratorRoleId ) , cancellationToken ) ) . FirstOrDefault ( ) ;
6574
6675 if ( role == null )
6776 {
@@ -83,7 +92,7 @@ public async Task HandleAsync(RegisterTenant message, CancellationToken cancella
8392 AuditTenantId = auditInformation . AuditTenantId
8493 } ;
8594
86- await _mediator . SendAsync ( registerAdministratorMessage , cancellationToken ) ;
95+ await bus . SendAsync ( registerAdministratorMessage , builder => builder . ToSelf ( ) , cancellationToken ) ;
8796
8897 var setIdentityRoleMessage = new SetIdentityRoleStatus
8998 {
@@ -94,6 +103,6 @@ public async Task HandleAsync(RegisterTenant message, CancellationToken cancella
94103 AuditTenantId = auditInformation . AuditTenantId
95104 } ;
96105
97- await _mediator . SendAsync ( setIdentityRoleMessage , cancellationToken ) ;
106+ await bus . SendAsync ( setIdentityRoleMessage , builder => builder . ToSelf ( ) , cancellationToken ) ;
98107 }
99108}
0 commit comments