private async Task ImportPlatformEntriesInternalAsync(ZipArchive zipArchive, PlatformExportManifest manifest, Action <ExportImportProgressInfo> progressCallback, ICancellationToken cancellationToken)
        {
            var progressInfo   = new ExportImportProgressInfo();
            var jsonSerializer = GetJsonSerializer();
            var batchSize      = 20;

            var platformZipEntries = zipArchive.GetEntry(PlatformZipEntryName);

            if (platformZipEntries != null)
            {
                using (var stream = platformZipEntries.Open())
                {
                    using (var streamReader = new StreamReader(stream))
                        using (var reader = new JsonTextReader(streamReader))
                        {
                            while (reader.Read())
                            {
                                if (reader.TokenType == JsonToken.PropertyName)
                                {
                                    if (manifest.HandleSecurity && reader.Value.ToString().EqualsInvariant("Roles"))
                                    {
                                        await reader.DeserializeJsonArrayWithPagingAsync <Role>(jsonSerializer, batchSize, async items =>
                                        {
                                            foreach (var role in items)
                                            {
                                                if (await _roleManager.RoleExistsAsync(role.Name))
                                                {
                                                    await _roleManager.UpdateAsync(role);
                                                }
                                                else
                                                {
                                                    await _roleManager.CreateAsync(role);
                                                }
                                            }
                                        }, processedCount =>
                                        {
                                            progressInfo.Description = $"{ processedCount } roles have been imported";
                                            progressCallback(progressInfo);
                                        }, cancellationToken);
                                    }
                                    else if (manifest.HandleSecurity && reader.Value.ToString().EqualsInvariant("Users"))
                                    {
                                        await reader.DeserializeJsonArrayWithPagingAsync <ApplicationUser>(jsonSerializer, batchSize, async items =>
                                        {
                                            foreach (var user in items)
                                            {
                                                var userExist = await _userManager.FindByIdAsync(user.Id);
                                                if (userExist != null)
                                                {
                                                    await _userManager.UpdateAsync(user);
                                                }
                                                else
                                                {
                                                    await _userManager.CreateAsync(user);
                                                }
                                            }
                                        }, processedCount =>
                                        {
                                            progressInfo.Description = $"{ processedCount } roles have been imported";
                                            progressCallback(progressInfo);
                                        }, cancellationToken);
                                    }
                                    else if (manifest.HandleSettings && reader.Value.ToString() == "Settings")
                                    {
                                        await reader.DeserializeJsonArrayWithPagingAsync <ObjectSettingEntry>(jsonSerializer, int.MaxValue,
                                                                                                              async items =>
                                        {
                                            var arrayItems = items.ToArray();
                                            foreach (var module in manifest.Modules)
                                            {
                                                await _settingsManager.SaveObjectSettingsAsync(arrayItems.Where(x => x.ModuleId == module.Id).ToArray());
                                            }
                                        }, processedCount =>
                                        {
                                            progressInfo.Description = $"{ processedCount } coupons have been imported";
                                            progressCallback(progressInfo);
                                        }, cancellationToken);
                                    }
                                    else if (manifest.HandleSettings && reader.Value.ToString() == "DynamicProperties")
                                    {
                                        await reader.DeserializeJsonArrayWithPagingAsync <DynamicProperty>(jsonSerializer, batchSize,
                                                                                                           items => _dynamicPropertyService.SaveDynamicPropertiesAsync(items.ToArray()), processedCount =>
                                        {
                                            progressInfo.Description = $"{ processedCount } coupons have been imported";
                                            progressCallback(progressInfo);
                                        }, cancellationToken);
                                    }
                                    else if (manifest.HandleSettings && reader.Value.ToString() == "DynamicPropertyDictionaryItems")
                                    {
                                        await reader.DeserializeJsonArrayWithPagingAsync <DynamicPropertyDictionaryItem>(jsonSerializer, batchSize,
                                                                                                                         items => _dynamicPropertyService.SaveDictionaryItemsAsync(items.ToArray()), processedCount =>
                                        {
                                            progressInfo.Description = $"{ processedCount } coupons have been imported";
                                            progressCallback(progressInfo);
                                        }, cancellationToken);
                                    }
                                }
                            }
                        }
                }
            }
        }
Пример #2
0
        public async Task <IActionResult> SaveDictionaryItemsAsync([FromBody] DynamicPropertyDictionaryItem[] items)
        {
            await _dynamicPropertyService.SaveDictionaryItemsAsync(items);

            return(Ok());
        }
        private async Task ImportPlatformEntriesInternalAsync(ZipArchive zipArchive, PlatformExportManifest manifest, Action <ExportImportProgressInfo> progressCallback, ICancellationToken cancellationToken)
        {
            var progressInfo = new ExportImportProgressInfo();

            var platformZipEntries = zipArchive.GetEntry(_platformZipEntryName);

            if (platformZipEntries != null)
            {
                PlatformExportEntries platformEntries;
                using (var stream = platformZipEntries.Open())
                {
                    platformEntries = stream.DeserializeJson <PlatformExportEntries>(GetJsonSerializer());
                }

                //Import security objects
                if (manifest.HandleSecurity)
                {
                    progressInfo.Description = $"Import { platformEntries.Users.Count()} users with roles...";
                    progressCallback(progressInfo);

                    foreach (var role in platformEntries.Roles)
                    {
                        //TODO: Test with new and already exist
                        await _roleManager.UpdateAsync(role);

                        foreach (var permission in role.Permissions)
                        {
                            //TODO: Test with new and already exist
                            await _roleManager.AddClaimAsync(role, new Claim(PlatformConstants.Security.Claims.PermissionClaimType, permission.Name));
                        }
                    }

                    //Next create or update users
                    foreach (var user in platformEntries.Users)
                    {
                        if (_userManager.FindByIdAsync(user.Id).Result != null)
                        {
                            await _userManager.UpdateAsync(user);
                        }
                        else
                        {
                            await _userManager.CreateAsync(user);
                        }
                    }
                }

                //Import modules settings
                if (manifest.HandleSettings)
                {
                    //Import dynamic properties
                    await _dynamicPropertyService.SaveDynamicPropertiesAsync(platformEntries.DynamicProperties.ToArray());

                    await _dynamicPropertyService.SaveDictionaryItemsAsync(platformEntries.DynamicPropertyDictionaryItems.ToArray());

                    foreach (var module in manifest.Modules)
                    {
                        await _settingsManager.SaveObjectSettingsAsync(platformEntries.Settings.Where(x => x.ModuleId == module.Id).ToArray());
                    }
                }
            }
        }