Exemplo n.º 1
0
        public async Task <IActionResult> Edit(string name)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageDynamicResources))
            {
                return(Forbid());
            }

            var layers = await _layerService.GetDynamicResourcesAsync();

            var layer = layers.DynamicResources.FirstOrDefault(x => String.Equals(x.Name, name));

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

            var model = new DynamicResourceEditViewModel
            {
                Name        = layer.Name,
                Rule        = layer.Rule,
                Description = layer.Description,
                Resources   = layer.Resources,
                Enabled     = layer.Enabled
            };

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> EditPost(DynamicResourceEditViewModel model)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageDynamicResources))
            {
                return(Forbid());
            }

            var layers = await _layerService.LoadDynamicResourcesAsync();

            ValidateViewModel(model, layers, isNew: false);

            if (ModelState.IsValid)
            {
                var layer = layers.DynamicResources.FirstOrDefault(x => String.Equals(x.Name, model.Name));

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

                layer.Name        = model.Name;
                layer.Rule        = model.Rule;
                layer.Description = model.Description;
                layer.Resources   = model.Resources;
                layer.Enabled     = model.Enabled;

                await _layerService.UpdateAsync(layers);

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> CreatePost(DynamicResourceEditViewModel model)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageDynamicResources))
            {
                return(Forbid());
            }

            var layers = await _layerService.LoadDynamicResourcesAsync();

            ValidateViewModel(model, layers, isNew: true);

            if (ModelState.IsValid)
            {
                layers.DynamicResources.Add(new DynamicResource
                {
                    Name        = model.Name,
                    Rule        = model.Rule,
                    Description = model.Description,
                    Resources   = model.Resources,
                    Enabled     = model.Enabled
                });

                await _layerService.UpdateAsync(layers);

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
Exemplo n.º 4
0
        // [HttpPost]
        // public async Task<IActionResult> UpdatePosition(string contentItemId, double position, string zone)
        // {
        //     if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageDynamicResources))
        //     {
        //         return StatusCode(401);
        //     }
        //
        //     // Load the latest version first if any
        //     var contentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Latest);
        //
        //     if (contentItem == null)
        //     {
        //         return StatusCode(404);
        //     }
        //
        //     var layerMetadata = contentItem.As<LayerMetadata>();
        //
        //     if (layerMetadata == null)
        //     {
        //         return StatusCode(403);
        //     }
        //
        //     layerMetadata.Position = position;
        //     layerMetadata.Zone = zone;
        //
        //     contentItem.Apply(layerMetadata);
        //
        //     _session.Save(contentItem);
        //
        //     // In case the moved contentItem is the draft for a published contentItem we update it's position too.
        //     // We do that because we want the position of published and draft version to be the same.
        //     if (contentItem.IsPublished() == false)
        //     {
        //         var publishedContentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Published);
        //         if (publishedContentItem != null)
        //         {
        //             layerMetadata = contentItem.As<LayerMetadata>();
        //
        //             if (layerMetadata == null)
        //             {
        //                 return StatusCode(403);
        //             }
        //
        //             layerMetadata.Position = position;
        //             layerMetadata.Zone = zone;
        //
        //             publishedContentItem.Apply(layerMetadata);
        //
        //             _session.Save(publishedContentItem);
        //         }
        //     }
        //
        //     // The state will be updated once the ambient session is committed.
        //     await _layerStateManager.UpdateAsync(new LayerState());
        //
        //     if (Request.Headers != null && Request.Headers["X-Requested-With"] == "XMLHttpRequest")
        //     {
        //         return StatusCode(200);
        //     }
        //     else
        //     {
        //         return RedirectToAction("Index");
        //     }
        // }

        private void ValidateViewModel(DynamicResourceEditViewModel model, DynamicResourcesDocument layers, bool isNew)
        {
            if (String.IsNullOrWhiteSpace(model.Name))
            {
                ModelState.AddModelError(nameof(DynamicResourceEditViewModel.Name), S["The dynamic resource name is required."]);
            }
            else if (isNew && layers.DynamicResources.Any(x => String.Equals(x.Name, model.Name, StringComparison.OrdinalIgnoreCase)))
            {
                ModelState.AddModelError(nameof(DynamicResourceEditViewModel.Name), S["The dynamic resource name already exists."]);
            }

            if (String.IsNullOrWhiteSpace(model.Rule))
            {
                ModelState.AddModelError(nameof(DynamicResourceEditViewModel.Rule), S["The rule is required."]);
            }
        }