11using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
24using System . Threading . Tasks ;
5+ using Shuttle . Access . DataAccess ;
36using Shuttle . Access . Messages . v1 ;
47using Shuttle . Core . Contract ;
58using Shuttle . Core . Mediator ;
@@ -10,13 +13,15 @@ namespace Shuttle.Access.Application;
1013
1114public class RegisterRoleParticipant : IParticipant < RequestResponseMessage < RegisterRole , RoleRegistered > >
1215{
16+ private readonly IPermissionQuery _permissionQuery ;
1317 private readonly IEventStore _eventStore ;
1418 private readonly IIdKeyRepository _idKeyRepository ;
1519
16- public RegisterRoleParticipant ( IEventStore eventStore , IIdKeyRepository idKeyRepository )
20+ public RegisterRoleParticipant ( IEventStore eventStore , IIdKeyRepository idKeyRepository , IPermissionQuery permissionQuery )
1721 {
1822 _eventStore = Guard . AgainstNull ( eventStore ) ;
1923 _idKeyRepository = Guard . AgainstNull ( idKeyRepository ) ;
24+ _permissionQuery = Guard . AgainstNull ( permissionQuery ) ;
2025 }
2126
2227 public async Task ProcessMessageAsync ( IParticipantContext < RequestResponseMessage < RegisterRole , RoleRegistered > > context )
@@ -25,6 +30,24 @@ public async Task ProcessMessageAsync(IParticipantContext<RequestResponseMessage
2530
2631 var message = context . Message . Request ;
2732
33+ var permissionIds = new List < Guid > ( ) ;
34+
35+ foreach ( var permission in message . GetPermissions ( ) )
36+ {
37+ var permissionId = ( await _permissionQuery . SearchAsync ( new DataAccess . Permission . Specification ( ) . AddName ( permission ) ) ) . FirstOrDefault ( ) ? . Id ;
38+
39+ if ( permissionId . HasValue )
40+ {
41+ permissionIds . Add ( permissionId . Value ) ;
42+ }
43+ else
44+ {
45+
46+ message . MissingPermissions ( ) ;
47+ return ;
48+ }
49+ }
50+
2851 var key = Role . Key ( message . Name ) ;
2952
3053 if ( await _idKeyRepository . ContainsAsync ( key ) )
@@ -41,6 +64,14 @@ public async Task ProcessMessageAsync(IParticipantContext<RequestResponseMessage
4164
4265 stream . Add ( role . Register ( message . Name ) ) ;
4366
67+ foreach ( var permissionId in permissionIds )
68+ {
69+ if ( ! role . HasPermission ( permissionId ) )
70+ {
71+ stream . Add ( role . AddPermission ( permissionId ) ) ;
72+ }
73+ }
74+
4475 context . Message . WithResponse ( new ( )
4576 {
4677 Id = id ,
0 commit comments