Exemple #1
0
        public async Task <IActionResult> Edit(string sourceName, TemplateViewModel model, string submit, string returnUrl = null)
        {
            if (!model.AdminTemplates && !await _authorizationService.AuthorizeAsync(User, Permissions.ManageTemplates))
            {
                return(Unauthorized());
            }

            if (model.AdminTemplates && !await _authorizationService.AuthorizeAsync(User, AdminTemplatesPermissions.ManageAdminTemplates))
            {
                return(Unauthorized());
            }

            var templatesDocument = model.AdminTemplates
                ? await _adminTemplatesManager.LoadTemplatesDocumentAsync()
                : await _templatesManager.LoadTemplatesDocumentAsync()
            ;

            if (ModelState.IsValid)
            {
                if (String.IsNullOrWhiteSpace(model.Name))
                {
                    ModelState.AddModelError(nameof(TemplateViewModel.Name), S["The name is mandatory."]);
                }
                else if (!model.Name.Equals(sourceName, StringComparison.OrdinalIgnoreCase) && templatesDocument.Templates.ContainsKey(model.Name))
                {
                    ModelState.AddModelError(nameof(TemplateViewModel.Name), S["A template with the same name already exists."]);
                }
            }

            if (!templatesDocument.Templates.ContainsKey(sourceName))
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                var template = new Template {
                    Content = model.Content, Description = model.Description
                };

                await(model.AdminTemplates
                    ? _adminTemplatesManager.RemoveTemplateAsync(sourceName)
                    : _templatesManager.RemoveTemplateAsync(sourceName)
                      );

                await(model.AdminTemplates
                    ? _adminTemplatesManager.UpdateTemplateAsync(model.Name, template)
                    : _templatesManager.UpdateTemplateAsync(model.Name, template)
                      );

                if (submit != "SaveAndContinue")
                {
                    return(RedirectToReturnUrlOrIndex(returnUrl));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["ReturnUrl"] = returnUrl;
            return(View(model));
        }