Example #1
0
        public async Task <IActionResult> Edit(EmailTemplateViewModel model, string submit, string returnUrl = null)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageEmailTemplates))
            {
                return(Forbid());
            }

            var templatesDocument = await _templatesManager.LoadEmailTemplatesDocumentAsync();

            if (ModelState.IsValid && templatesDocument.Templates.Any(x => x.Key != model.Id && x.Value.Name.Equals(model.Name, StringComparison.OrdinalIgnoreCase)))
            {
                ModelState.AddModelError(nameof(EmailTemplateViewModel.Name), S["An email template with the same name already exists."]);
            }

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

            if (ModelState.IsValid)
            {
                var template = new EmailTemplate
                {
                    Name                 = model.Name,
                    Description          = model.Description,
                    AuthorExpression     = model.AuthorExpression,
                    ReplyToExpression    = model.ReplyToExpression,
                    SenderExpression     = model.SenderExpression,
                    RecipientsExpression = model.RecipientsExpression,
                    CCExpression         = model.CCExpression,
                    BCCExpression        = model.BCCExpression,
                    SubjectExpression    = model.SubjectExpression,
                    Body                 = model.Body,
                    IsBodyHtml           = model.IsBodyHtml,
                };

                await _templatesManager.RemoveTemplateAsync(model.Id);

                await _templatesManager.UpdateTemplateAsync(model.Id, template);

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

            // If we got this far, something failed, redisplay form
            ViewData["ReturnUrl"] = returnUrl;
            return(View(model));
        }
Example #2
0
        public async Task <int> UpdateFrom1Async()
        {
            var templateDocuments = _templatesManager.GetEmailTemplatesDocumentAsync().Result;

            // if any EmailTemplates has a value on the name property it was already updated
            if (templateDocuments.Templates.Any(x => x.Value.Name != null))
            {
                return(2);
            }

            foreach (var templateDocument in templateDocuments.Templates)
            {
                // Assign all value to a new template, generate an Id and delete the old one.
                var template = new EmailTemplate
                {
                    Name                 = templateDocument.Key,
                    Description          = templateDocument.Value.Description,
                    AuthorExpression     = templateDocument.Value.AuthorExpression,
                    SenderExpression     = templateDocument.Value.SenderExpression,
                    ReplyToExpression    = templateDocument.Value.ReplyToExpression,
                    RecipientsExpression = templateDocument.Value.RecipientsExpression,
                    SubjectExpression    = templateDocument.Value.SubjectExpression,
                    Body                 = templateDocument.Value.Body,
                    IsBodyHtml           = templateDocument.Value.IsBodyHtml,
                };

                await _templatesManager.UpdateTemplateAsync(IdGenerator.GenerateId(), template);

                await _templatesManager.RemoveTemplateAsync(templateDocument.Key);
            }

            return(2);
        }
        public async Task <IActionResult> Edit(string sourceName, EmailTemplateViewModel model, string submit, string returnUrl = null)
        {
            if (!await _authorizationService.AuthorizeAsync(User, EmailTemplatesPermissions.ManageEmailTemplates))
            {
                return(Unauthorized());
            }

            var templatesDocument = await _emailTemplatesManager.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 _emailTemplatesManager.RemoveTemplateAsync(sourceName);

                await _emailTemplatesManager.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));
        }