public async Task DispatchAllAsync(CancellationToken cancellationToken = default)
        {
            // Ez a metódus a bg service által kerül meghívásra, tenant nem elérhető
            // => Dispatchelünk minden tenant outboxából
            var tenants = await masterDbContext.Tenants.ToListAsync(cancellationToken);

            foreach (var tenant in tenants)
            {
                await using var context = tenantOutboxDbContextFactory.CreateContext(tenant);
                var dispatcher = new EfCoreMessageDispatcher <TContext>(
                    context,
                    serviceProvider.GetRequiredService <IOptions <OutboxConfiguration> >(),
                    serviceProvider.GetRequiredService <IPublishEndpoint>(),
                    serviceProvider.GetRequiredService <ILogger <EfCoreMessageDispatcher <TContext> > >());

                await dispatcher.DispatchAllAsync(cancellationToken);
            }
        }
        public async Task Consume(ConsumeContext <TenantDeletedEvent> context)
        {
            var tenant = await masterDbContext.Tenants.FindOrThrowAsync(context.Message.TenantId);

            await using var tenantDbContext = tenantDbContextFactory.CreateContext(tenant);

            await tenantDbContext.Database.EnsureDeletedAsync();

            masterDbContext.Tenants.Remove(tenant);

            await masterDbContext.SaveChangesAsync();
        }
        public async Task Consume(ConsumeContext <TenantUserDeletedEvent> context)
        {
            var tenant = await masterDbContext.Tenants.FindAsync(context.Message.TenantId);

            await using var dbContext = tenantDbContextFactory.CreateContext(tenant);

            var userWithRoles = await dbContext.Users.Include(x => x.UserRoles)
                                .SingleAsync(x => x.Id == context.Message.UserId);

            dbContext.Users.Remove(userWithRoles);
            await dbContext.SaveChangesAsync();
        }
        public async Task Consume(ConsumeContext <TenantUserCreatedOrEditedEvent> context)
        {
            var tenant = await masterDbContext.Tenants.FindAsync(context.Message.TenantId);

            // Global admins can also create users => No tenant Id will be set in the Identity Service.
            await using var dbContext = tenantDbContextFactory.CreateContext(tenant);

            var existingUser = await dbContext.Users.IgnoreQueryFilters()
                               .Include(x => x.UserRoles)
                               .SingleOrDefaultAsync(x => x.Id == context.Message.UserId);

            if (existingUser == null)
            {
                existingUser = new MessagingUser
                {
                    Id        = context.Message.UserId,
                    Name      = context.Message.Name,
                    UserRoles = new List <MessagingUserRole>()
                };
            }
            else
            {
                dbContext.UserRoles.RemoveRange(existingUser.UserRoles);

                if (existingUser.IsDeleted)
                {
                    existingUser.IsDeleted = false;
                }
            }

            var userRoles = context.Message.TenantRoles.Select(x => new MessagingUserRole
            {
                User           = existingUser,
                TenantRoleType = x,
            });

            dbContext.UserRoles.AddRange(userRoles);
            await dbContext.SaveChangesAsync();
        }
Exemple #5
0
        public async Task Consume(ConsumeContext <TenantUserDeletedEvent> context)
        {
            var tenant = await masterDbContext.Tenants.FindAsync(context.Message.TenantId);

            await using var dbContext = tenantDbContextFactory.CreateContext(tenant);

            var userWithRoles = await dbContext.Users.Include(x => x.UserRoles)
                                .SingleAsync(x => x.Id == context.Message.UserId);

            eventPublisher.Setup(dbContext);
            foreach (var @event in userWithRoles.UserRoles.Select(mapper.Map <TenantUserRoleDeletedEvent>))
            {
                await eventPublisher.PublishAsync(@event, publishContext =>
                {
                    publishContext.Headers.Add("TenantId", context.Message.TenantId.ToString());
                    return(Task.CompletedTask);
                });
            }

            dbContext.Users.Remove(userWithRoles);
            dbContext.UserRoles.RemoveRange(userWithRoles.UserRoles);
            await dbContext.SaveChangesAsync();
        }
Exemple #6
0
        public async Task Consume(ConsumeContext <TenantCreatedOrUpdatedEvent> context)
        {
            var tenant = await masterDbContext.Tenants.FindAsync(context.Message.TenantId);

            if (tenant == null)
            {
                tenant = new Tenant
                {
                    Id = context.Message.TenantId,
                    DbConnectionString = string.Format(configuration.GetConnectionString("ConnectionStringTemplate"),
                                                       context.Message.TenantId)
                };
                masterDbContext.Tenants.Add(tenant);

                await using var tenantDbContext = tenantDbContextFactory.CreateContext(tenant);
                await tenantDbContext.Database.MigrateAsync();
            }

            tenant.Name         = context.Message.Name;
            tenant.OmIdentifier = context.Message.OmIdentifier;

            memoryCache.Set(tenant.Id, tenant);
            await masterDbContext.SaveChangesAsync();
        }
        public async Task Consume(ConsumeContext <TenantUserCreatedOrEditedEvent> context)
        {
            var tenant = await masterDbContext.Tenants.FindAsync(context.Message.TenantId);

            // Global admins can also create users => No tenant Id will be set in the Identity Service.
            await using var dbContext = tenantDbContextFactory.CreateContext(tenant);
            publisher.Setup(dbContext);

            var tenantUserTypes = typeof(ClassRegisterUserRole).Assembly
                                  .GetTypes()
                                  .Where(x => x.BaseType == typeof(ClassRegisterUserRole) && x.GetCustomAttribute <TenantUserAttribute>() != null)
                                  .ToList();

            var existingUser = await dbContext.Users.IgnoreQueryFilters()
                               .Include(x => x.UserRoles)
                               .SingleOrDefaultAsync(x => x.Id == context.Message.UserId);

            if (existingUser == null)
            {
                existingUser = new ClassRegisterUser
                {
                    Id        = context.Message.UserId,
                    Name      = context.Message.Name,
                    Email     = context.Message.Email,
                    UserRoles = new List <ClassRegisterUserRole>()
                };
                dbContext.Users.Add(existingUser);
            }
            else
            {
                existingUser.Name  = context.Message.Name;
                existingUser.Email = context.Message.Email;
            }

            // Delete or update userBases
            foreach (var userBase in existingUser.UserRoles)
            {
                if (context.Message.TenantRoles.All(x => x != userBase.GetType().GetCustomAttribute <TenantUserAttribute>() !.TenantRoleType))
                {
                    dbContext.UserRoles.Remove(userBase);
                    if (mapper.TryMap <TenantUserRoleDeletedEvent>(userBase, out var @event))
                    {
                        await publisher.PublishAsync(@event, context =>
                        {
                            context.Headers.Add("TenantId", tenant.Id.ToString());
                            return(Task.CompletedTask);
                        });
                    }
                }
            }

            foreach (var roleType in context.Message.TenantRoles)
            {
                var existingUserRole = existingUser.UserRoles.SingleOrDefault(x =>
                                                                              x.GetType().GetCustomAttribute <TenantUserAttribute>()?.TenantRoleType == roleType);

                if (existingUserRole == null)
                {
                    var userType = tenantUserTypes.Single(x =>
                                                          x.GetCustomAttribute <TenantUserAttribute>() !.TenantRoleType == roleType);

                    var userRole = (ClassRegisterUserRole)Activator.CreateInstance(userType);
                    userRole.Id   = Guid.NewGuid();
                    userRole.User = existingUser;
                    dbContext.UserRoles.Add(userRole);
                    if (mapper.TryMap <TenantUserRoleCreatedEvent>(userRole, out var @event))
                    {
                        await publisher.PublishAsync(@event, context =>
                        {
                            context.Headers.Add("TenantId", tenant.Id.ToString());
                            return(Task.CompletedTask);
                        });
                    }
                }
                else
                {
                    // Undelete the existing user
                    existingUserRole.IsDeleted = false;
                    if (mapper.TryMap <TenantUserRoleCreatedEvent>(existingUserRole, out var @event))
                    {
                        await publisher.PublishAsync(@event, context =>
                        {
                            context.Headers.Add("TenantId", tenant.Id.ToString());
                            return(Task.CompletedTask);
                        });
                    }
                }
            }

            await dbContext.SaveChangesAsync();
        }