示例#1
0
        public async Task ExportAsync(ISyncService sync, SyncOptions options, ISession session)
        {
            var model = new AppModel
            {
                Contributors = new Dictionary <string, AppContributorModel>()
            };

            await log.DoSafeAsync("Exporting clients", async() =>
            {
                var clients = await session.Apps.GetClientsAsync(session.App);

                model.Clients = new Dictionary <string, AppClientModel>();

                foreach (var client in clients.Items)
                {
                    model.Clients[client.Name] = client.ToModel();
                }
            });

            await log.DoSafeAsync("Exporting languages", async() =>
            {
                var languages = await session.Apps.GetLanguagesAsync(session.App);

                model.Languages = new Dictionary <string, UpdateLanguageDto>();

                foreach (var language in languages.Items)
                {
                    model.Languages[language.Iso2Code] = language.ToModel();
                }
            });

            await log.DoSafeAsync("Exporting Roles", async() =>
            {
                var roles = await session.Apps.GetRolesAsync(session.App);

                model.Roles = new Dictionary <string, AppRoleModel>();

                foreach (var role in roles.Items.Where(x => !x.IsDefaultRole))
                {
                    model.Roles[role.Name] = role.ToModel();
                }
            });

            await sync.WriteWithSchema(new FilePath("app.json"), model, Ref);
        }
        public async Task GenerateSchemaAsync(ISyncService sync)
        {
            await sync.WriteJsonSchemaAsync <AppModel>(new FilePath("app.json"));

            var sample = new AppModel
            {
                Roles = new Dictionary <string, AppRoleModel>
                {
                    ["custom"] = new AppRoleModel
                    {
                        Permissions = new[]
                        {
                            "schemas.*"
                        }
                    }
                },
                Clients = new Dictionary <string, AppClientModel>
                {
                    ["test"] = new AppClientModel
                    {
                        Role = "Owner"
                    }
                },
                Languages = new Dictionary <string, UpdateLanguageDto>
                {
                    ["en"] = new UpdateLanguageDto
                    {
                        IsMaster = true
                    }
                },
                Contributors = new Dictionary <string, AppContributorModel>
                {
                    ["*****@*****.**"] = new AppContributorModel
                    {
                        Role = "Owner"
                    }
                }
            };

            await sync.WriteWithSchema(new FilePath("__app.json"), sample, Ref);
        }
示例#3
0
        public async Task GenerateSchemaAsync(DirectoryInfo directoryInfo, JsonHelper jsonHelper)
        {
            await jsonHelper.WriteJsonSchemaAsync <AppModel>(directoryInfo, "app.json");

            var sample = new AppModel
            {
                Roles = new Dictionary <string, AppRoleModel>
                {
                    ["custom"] = new AppRoleModel
                    {
                        Permissions = new string[]
                        {
                            "schemas.*"
                        }
                    }
                },
                Clients = new Dictionary <string, AppClientModel>
                {
                    ["test"] = new AppClientModel
                    {
                        Role = "Owner"
                    }
                },
                Languages = new Dictionary <string, UpdateLanguageDto>
                {
                    ["en"] = new UpdateLanguageDto
                    {
                        IsMaster = true
                    }
                },
                Contributors = new Dictionary <string, AppContributorModel>
                {
                    ["*****@*****.**"] = new AppContributorModel
                    {
                        Role = "Owner"
                    }
                }
            };

            await jsonHelper.WriteWithSchema(directoryInfo, "__app.json", sample, "__json/app");
        }
        private async Task SynchronizeRolesAsync(AppModel model, SyncOptions options, ISession session)
        {
            var current = await session.Apps.GetRolesAsync(session.App);

            if (options.Delete)
            {
                foreach (var role in current.Items)
                {
                    if (model.Roles.ContainsKey(role.Name) ||
                        role.IsDefaultRole ||
                        role.NumClients > 0 ||
                        role.NumContributors > 0)
                    {
                        continue;
                    }

                    await log.DoSafeAsync($"Role '{role.Name}' deleting", async() =>
                    {
                        await session.Apps.DeleteRoleAsync(session.App, role.Name);
                    });
                }
            }

            foreach (var(roleName, _) in model.Roles)
            {
                var existing = current.Items.FirstOrDefault(x => x.Name == roleName);

                if (existing != null)
                {
                    continue;
                }

                await log.DoSafeAsync($"Role '{roleName}' creating", async() =>
                {
                    var request = new AddRoleDto {
                        Name = roleName
                    };

                    current = await session.Apps.PostRoleAsync(session.App, request);
                });
            }

            foreach (var(roleName, value) in model.Roles)
            {
                var existing = current.Items.FirstOrDefault(x => x.Name == roleName);

                if (existing == null || value.JsonEquals(existing))
                {
                    continue;
                }

                await log.DoSafeAsync($"Role '{roleName}' updating", async() =>
                {
                    var request = new UpdateRoleDto {
                        Permissions = value.Permissions
                    };

                    await session.Apps.PutRoleAsync(session.App, roleName, request);
                });
            }
        }
示例#5
0
        private async Task SynchronizeClientsAsync(AppModel model, SyncOptions options, ISession session)
        {
            var current = await session.Apps.GetClientsAsync(session.App);

            if (options.Delete)
            {
                foreach (var client in current.Items)
                {
                    var generatedClientId = $"{session.App}:{client.Id}";

                    if (model.Clients.ContainsKey(client.Id) || session.ClientId.Equals(generatedClientId, StringComparison.Ordinal))
                    {
                        continue;
                    }

                    await log.DoSafeAsync($"Client '{client.Id}' deleting", async() =>
                    {
                        await session.Apps.DeleteClientAsync(session.App, client.Id);
                    });
                }
            }

            foreach (var(clientId, _) in model.Clients)
            {
                var existing = current.Items.Find(x => x.Id == clientId);

                if (existing != null)
                {
                    continue;
                }

                await log.DoSafeAsync($"Client '{clientId}' creating", async() =>
                {
                    var request = new CreateClientDto {
                        Id = clientId
                    };

                    current = await session.Apps.PostClientAsync(session.App, request);
                });
            }

            foreach (var(clientId, client) in model.Clients)
            {
                var existing = current.Items.Find(x => x.Id == clientId);

                if (existing == null || client.JsonEquals(existing))
                {
                    continue;
                }

                if (!options.UpdateCurrentClient && session.ClientId.Equals(clientId, StringComparison.Ordinal))
                {
                    continue;
                }

                await log.DoSafeAsync($"Client '{clientId}' updating", async() =>
                {
                    var request = client.ToUpdate();

                    await session.Apps.PutClientAsync(session.App, clientId, request);
                });
            }
        }