Ejemplo n.º 1
0
        public async Task <UserDTO> GrantPermission([FromBody] GrantUserPermissionDTO input)
        {
            var command = new GrantUserPermissionCommand(input);
            var result  = await _commandDispatcher.Execute(command);

            return(result);
        }
Ejemplo n.º 2
0
        public void user_permission_list_should_not_have_granted_permission_after_UserPermissionGrantedEvent_with_inactive_permission()
        {
            // Assemble
            var agg           = UserAggregateMockAggregate.SetupAdminUser();
            var permissionAgg = UserAggregateMockAggregate.SetupTestPermission();

            permissionAgg.DisablePermission(agg);

            GrantUserPermissionDTO input = new GrantUserPermissionDTO
            {
                ForId = agg.Id,
                ById  = agg.Id,
                PermissionsToGrant = new Dictionary <Guid, PermissionDetails>
                {
                    {
                        permissionAgg.Id, new PermissionDetails
                        {
                            Reason = "test Reason"
                        }
                    }
                }
            };

            // Apply
            agg.GrantPermission(agg, new List <PermissionAggregate> {
                permissionAgg
            }, input);

            // Assert
            var changes = agg.FlushUncommitedChanges();

            Assert.Single(changes);
            Assert.False(agg.PermissionList.Any());
        }
Ejemplo n.º 3
0
        public void given_GrantUserPermissionCommand_handler_should_call_session_Get_and_Commit_on_requested_permission()
        {
            // Assemble
            var mockAgg           = new GrantUserPermissionCommandHandlerMockAggregate();
            var requestHandler    = mockAgg.UserPermissionsRequestedHandlerFactory();
            var granthandler      = mockAgg.GrantUserPermissionHandlerFactory();
            var testAgg           = mockAgg.SetupAdminUser();
            var testPermissionAgg = mockAgg.SetupTestPermission();

            mockAgg.setup_session_to_return_correct_aggregate(testAgg, testPermissionAgg);

            var grantInput = new GrantUserPermissionDTO
            {
                ForId = testAgg.Id,
                ById  = testAgg.Id,
                PermissionsToGrant = new Dictionary <Guid, PermissionDetails>
                {
                    {
                        testPermissionAgg.Id, new PermissionDetails
                        {
                            Reason = "testGrantReason"
                        }
                    }
                }
            };

            var grantCommand = new GrantUserPermissionCommand(grantInput);

            var requestInput = new RequestUserPermissionsDTO
            {
                ForId    = testAgg.Id,
                ById     = testAgg.Id,
                Requests = new Dictionary <Guid, PermissionDetails>
                {
                    {
                        testPermissionAgg.Id, new PermissionDetails
                        {
                            Reason = "testRequestReason"
                        }
                    }
                }
            };

            var requestCommand = new UserPermissionsRequestedCommand(requestInput);

            var requestResult = requestHandler.Handle(requestCommand);

            // Apply
            var grantResult = granthandler.Handle(grantCommand);

            // Assert
            Assert.True(mockAgg.SessionGetWasCalled);
            Assert.True(mockAgg.SessionCommitWasCalled);
            Assert.True(mockAgg.SessionGetPermisisonWasCalled);
        }
Ejemplo n.º 4
0
        public void user_permission_list_should_have_granted_permission_after_UserPermissionGrantedEvent()
        {
            // Assemble
            var agg           = UserAggregateMockAggregate.SetupAdminUser();
            var permissionAgg = UserAggregateMockAggregate.SetupTestPermission();

            GrantUserPermissionDTO input = new GrantUserPermissionDTO
            {
                ForId = agg.Id,
                ById  = agg.Id,
                PermissionsToGrant = new Dictionary <Guid, PermissionDetails>
                {
                    {
                        permissionAgg.Id, new PermissionDetails
                        {
                            Reason = "test Reason"
                        }
                    }
                }
            };

            // Apply
            agg.GrantPermission(agg, new List <PermissionAggregate> {
                permissionAgg
            }, input);

            // Assert
            var changes = agg.FlushUncommitedChanges();

            Assert.Equal(2, changes.Length);
            Assert.Collection(changes,
                              (e) =>
            {
                Assert.IsType <UserCreatedEvent>(e);
            },
                              (e) =>
            {
                Assert.IsType <UserPermissionGrantedEvent>(e);
                var @event = (UserPermissionGrantedEvent)e;
                Assert.NotEqual(Guid.Empty, @event.Id);
                Assert.Equal(2, @event.Version);

                Assert.Equal(agg.Id, @event.ForId);
                Assert.Equal(agg.Id, @event.ById);
                Assert.True(@event.PermissionsToGrant.ContainsKey(permissionAgg.Id));
                Assert.Equal(@event.PermissionsToGrant[permissionAgg.Id].EventType, JsonConvert.SerializeObject(typeof(UserPermissionGrantedEvent).FullName));
            }
                              );

            Assert.True(agg.PermissionList.ContainsKey(permissionAgg.Id));
            Assert.Equal(agg.PermissionList[permissionAgg.Id].EventType, JsonConvert.SerializeObject(typeof(UserPermissionGrantedEvent).FullName));
        }
Ejemplo n.º 5
0
        public async Task <UserDTO> Handle(GrantRevokePermissionsCommand command)
        {
            var agg = await _session.Get <UserAggregate>(command.Input.ForId);

            var byAgg = await _session.Get <UserAggregate>(command.Input.ById);

            var permissionsToGrant  = GetPermissionsToGrant(agg.PermissionList, command.Input.Permissions);
            var permissionsToRevoke = GetPermissionsToRevoke(agg.PermissionList, command.Input.Permissions);
            var grantPermAggs       = new List <PermissionAggregate>();
            var revokePermAggs      = new List <PermissionAggregate>();

            if (byAgg.IsAdmin)
            {
                if (permissionsToGrant.Any())
                {
                    foreach (var permission in permissionsToGrant)
                    {
                        grantPermAggs.Add(await _session.Get <PermissionAggregate>(permission.Key));
                    }

                    var grantUserPermissionDTO = new GrantUserPermissionDTO
                    {
                        ForId = agg.Id,
                        ById  = byAgg.Id,
                        PermissionsToGrant = permissionsToGrant
                    };
                    agg.GrantPermission(byAgg, grantPermAggs, grantUserPermissionDTO);
                }

                if (permissionsToRevoke.Any())
                {
                    foreach (var permission in permissionsToRevoke)
                    {
                        revokePermAggs.Add(await _session.Get <PermissionAggregate>(permission.Key));
                    }

                    var revokeUserPermissionDTO = new RevokeUserPermissionDTO
                    {
                        ForId = agg.Id,
                        ById  = byAgg.Id,
                        PermissionsToRevoke = permissionsToRevoke
                    };
                    agg.RevokePermission(byAgg, revokeUserPermissionDTO);
                }

                _email.SendPermissionsUpdatedMessage(agg, revokePermAggs, grantPermAggs);
                await _session.Commit();
            }

            return(_mapper.Map <UserAggregate, UserDTO>(await _session.Get <UserAggregate>(agg.Id)));
        }
Ejemplo n.º 6
0
        public async void given_grantuserpermissionscommand_command_dispatcher_should_get_same_command_created_in_controller()
        {
            //Assemble
            var mockAgg = new UserControllerMockAggregate();

            var id       = new Guid();
            var userId   = new Guid();
            var requests = new Dictionary <Guid, PermissionDetails>
            {
                {
                    new Guid(), new PermissionDetails
                    {
                        EventType    = "testEvent",
                        IsPending    = true,
                        Reason       = "testReason",
                        RequestedBy  = id,
                        RequestedFor = userId,
                        RequestDate  = new DateTime()
                    }
                }
            };

            var input = new GrantUserPermissionDTO()
            {
                ById  = id,
                ForId = userId,
                PermissionsToGrant = requests
            };

            var command = new GrantUserPermissionCommand(input);

            mockAgg.setup_dispatcher_to_verify_grantUserPermissionRequestCommands_are_the_same(command);

            var controller = mockAgg.CreateUserController();

            //Apply
            var result = await controller.GrantPermission(input);

            //Assert
            Assert.IsType <UserDTO>(result);
            Assert.Equal(result.Id, input.ForId);
            Assert.Equal(result.PermissionList, input.PermissionsToGrant);
        }
Ejemplo n.º 7
0
        public UserPermissionGrantedEvent(GrantUserPermissionDTO dto)
        {
            ForId = dto.ForId;
            ById  = dto.ById;
            foreach (var permission in dto.PermissionsToGrant)
            {
                var reason = String.IsNullOrWhiteSpace(permission.Value.Reason) ? "Reason Not Specified" : permission.Value.Reason;

                var p = new PermissionDetails
                {
                    EventType    = JsonConvert.SerializeObject(GetType().FullName),
                    IsPending    = false,
                    Reason       = reason,
                    RequestDate  = TimeStamp,
                    RequestedBy  = dto.ById,
                    RequestedFor = dto.ForId
                };
                PermissionsToGrant[permission.Key] = p;
            }
        }
Ejemplo n.º 8
0
        public void user_permission_list_should_not_have_revoked_permission_after_UserPermissionRevokedEvent_when_byAgg_is_not_an_admin()
        {
            // Assemble
            var agg           = UserAggregateMockAggregate.SetupAdminUser();
            var nonAdminAgg   = UserAggregateMockAggregate.SetupNonAdminUser();
            var permissionAgg = UserAggregateMockAggregate.SetupTestPermission();

            GrantUserPermissionDTO input = new GrantUserPermissionDTO
            {
                ForId = agg.Id,
                ById  = agg.Id,
                PermissionsToGrant = new Dictionary <Guid, PermissionDetails>
                {
                    {
                        permissionAgg.Id, new PermissionDetails
                        {
                            Reason = "test grant Reason"
                        }
                    }
                }
            };

            agg.GrantPermission(agg, new List <PermissionAggregate> {
                permissionAgg
            }, input);

            RevokeUserPermissionDTO revokeInput = new RevokeUserPermissionDTO()
            {
                ForId = agg.Id,
                ById  = nonAdminAgg.Id,
                PermissionsToRevoke = new Dictionary <Guid, PermissionDetails>
                {
                    {
                        permissionAgg.Id, new PermissionDetails
                        {
                            Reason = "test revoke reason"
                        }
                    }
                }
            };

            // Apply
            agg.RevokePermission(nonAdminAgg, revokeInput);

            // Assert
            var changes = agg.FlushUncommitedChanges();

            Assert.Equal(2, changes.Length);
            Assert.Collection(changes,
                              (e) =>
            {
                Assert.IsType <UserCreatedEvent>(e);
            },
                              (e) =>
            {
                Assert.IsType <UserPermissionGrantedEvent>(e);
            }
                              );

            Assert.True(agg.PermissionList.ContainsKey(permissionAgg.Id));
            Assert.Equal(agg.PermissionList[permissionAgg.Id].EventType, JsonConvert.SerializeObject(typeof(UserPermissionGrantedEvent).FullName));
        }
Ejemplo n.º 9
0
        public void GrantPermission(UserAggregate byAgg, List <PermissionAggregate> permissions, GrantUserPermissionDTO dto)
        {
            //business Logic here!
            if (byAgg.IsAdmin)
            {
                foreach (var permission in permissions)
                {
                    if (!permission.IsActive)
                    {
                        dto.PermissionsToGrant.Remove(permission.Id);
                    }
                }

                if (dto.PermissionsToGrant.Any())
                {
                    ApplyChange(new UserPermissionGrantedEvent(dto));
                }
            }
        }
Ejemplo n.º 10
0
 public GrantUserPermissionCommand(GrantUserPermissionDTO input)
 {
     Input = input;
 }