Ejemplo n.º 1
0
        public async Task <IActionResult> CreatePost(LayerEditViewModel model)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageLayers))
            {
                return(Unauthorized());
            }

            var layers = await _layerService.LoadLayersAsync();

            ValidateViewModel(model, layers, isNew: true);

            if (ModelState.IsValid)
            {
                layers.Layers.Add(new Layer
                {
                    Name        = model.Name,
                    Rule        = model.Rule,
                    Description = model.Description
                });

                await _layerService.UpdateAsync(layers);

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreatePost(LayerEditViewModel model)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageLayers))
            {
                return(Forbid());
            }

            var layers = await _layerService.LoadLayersAsync();

            ValidateViewModel(model, layers, isNew: true);

            if (ModelState.IsValid)
            {
                var layer = new Layer
                {
                    Name        = model.Name,
                    Description = model.Description
                };

                layer.LayerRule = new Rule();
                _conditionIdGenerator.GenerateUniqueId(layer.LayerRule);

                layers.Layers.Add(layer);

                await _layerService.UpdateAsync(layers);

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create(LayerRuleCreateViewModel model)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageLayers))
            {
                return(Forbid());
            }

            var layers = await _layerService.LoadLayersAsync();

            var layer = layers.Layers.FirstOrDefault(x => String.Equals(x.Name, model.Name));

            if (layer == null)
            {
                return(NotFound());
            }

            var conditionGroup = FindConditionGroup(layer.LayerRule, model.ConditionGroupId);

            if (conditionGroup == null)
            {
                return(NotFound());
            }

            var condition = _factories.FirstOrDefault(x => x.Name == model.ConditionType)?.Create();

            if (condition == null)
            {
                return(NotFound());
            }

            var editor = await _displayManager.UpdateEditorAsync(condition, updater : _updateModelAccessor.ModelUpdater, isNew : true);

            if (ModelState.IsValid)
            {
                _conditionIdGenerator.GenerateUniqueId(condition);
                conditionGroup.Conditions.Add(condition);
                await _layerService.UpdateAsync(layers);

                await _notifier.SuccessAsync(H["Condition added successfully."]);

                return(RedirectToAction(nameof(Edit), "Admin", new { name = model.Name }));
            }

            model.Editor = editor;

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 4
0
        public async Task <int> UpdateFrom2Async()
        {
            var layers = await _layerService.LoadLayersAsync();

            foreach (var layer in layers.Layers)
            {
                layer.LayerRule = new Rule();
                _conditionIdGenerator.GenerateUniqueId(layer.LayerRule);

#pragma warning disable 0618
                _ruleMigrator.Migrate(layer.Rule, layer.LayerRule);

                layer.Rule = String.Empty;
#pragma warning restore 0618
            }

            await _layerService.UpdateAsync(layers);

            return(3);
        }
Ejemplo n.º 5
0
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!String.Equals(context.Name, "Layers", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var model = context.Step.ToObject <LayerStepModel>();

            var allLayers = await _layerService.LoadLayersAsync();

            foreach (Layer layer in model.Layers)
            {
                var existing = allLayers.Layers.FirstOrDefault(x => String.Equals(x.Name, layer.Name, StringComparison.OrdinalIgnoreCase));

                if (existing != null)
                {
                    // Replace any property that is set in the recipe step
                    if (!String.IsNullOrEmpty(layer.Rule))
                    {
                        existing.Rule = layer.Rule;
                    }

                    if (!String.IsNullOrEmpty(layer.Description))
                    {
                        existing.Description = layer.Description;
                    }
                }
                else
                {
                    allLayers.Layers.Add(layer);
                }
            }

            await _layerService.UpdateAsync(allLayers);
        }
Ejemplo n.º 6
0
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!String.Equals(context.Name, "Layers", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var model = context.Step.ToObject <LayersStepModel>();

            var allLayers = await _layerService.LoadLayersAsync();

            var unknownTypes = new List <string>();
            var factories    = _factories.ToDictionary(x => x.Name);

            foreach (var layerStep in model.Layers)
            {
                var layer = allLayers.Layers.FirstOrDefault(x => String.Equals(x.Name, layerStep.Name, StringComparison.OrdinalIgnoreCase));

                if (layer == null)
                {
                    layer = new Layer();
                    allLayers.Layers.Add(layer);
                }

                // Backwards compatability check.
                if (layer.LayerRule == null)
                {
                    layer.LayerRule = new Rule();
                    _conditionIdGenerator.GenerateUniqueId(layer.LayerRule);
                }

                // Replace any property that is set in the recipe step
                if (!String.IsNullOrEmpty(layerStep.Name))
                {
                    layer.Name = layerStep.Name;
                }
                else
                {
                    throw new ArgumentNullException($"{nameof(layer.Name)} is required");
                }

                if (layerStep.LayerRule != null)
                {
                    if (!String.IsNullOrEmpty(layerStep.LayerRule.ConditionId))
                    {
                        layer.LayerRule.ConditionId = layerStep.LayerRule.ConditionId;
                    }

                    // The conditions list is cleared, because we cannot logically merge conditions.
                    layer.LayerRule.Conditions.Clear();
                    foreach (var jCondition in layerStep.LayerRule.Conditions)
                    {
                        var name = jCondition["Name"].ToString();
                        if (factories.TryGetValue(name, out var factory))
                        {
                            var factoryCondition = (Condition)jCondition.ToObject(factory.Create().GetType(), JsonSerializer);

                            layer.LayerRule.Conditions.Add(factoryCondition);
                        }
                        else
                        {
                            unknownTypes.Add(name);
                        }
                    }
                }

#pragma warning disable 0618
                // Migrate any old rule in a recipe to the new rule format.
                // Do not import the old rule.
                if (!String.IsNullOrEmpty(layerStep.Rule))
                {
                    _ruleMigrator.Migrate(layerStep.Rule, layer.LayerRule);
                }
#pragma warning restore 0618

                if (!String.IsNullOrEmpty(layerStep.Description))
                {
                    layer.Description = layerStep.Description;
                }
            }

            if (unknownTypes.Count != 0)
            {
                var prefix = "No changes have been made. The following types of conditions cannot be added:";
                var suffix = "Please ensure that the related features are enabled to add these types of conditions.";

                throw new InvalidOperationException($"{prefix} {String.Join(", ", unknownTypes)}. {suffix}");
            }

            await _layerService.UpdateAsync(allLayers);
        }