Browse Source

Change the code style and architecture.

pull/340/head
seamys 8 years ago
parent
commit
ae2ea6bc18
  1. 37
      extensions/Squidex.Extensions/Actions/Email/EmailAction.cs
  2. 33
      extensions/Squidex.Extensions/Actions/Email/EmailActionHandler.cs
  3. 22
      extensions/Squidex.Extensions/Actions/Email/EmailOptions.cs
  4. 2
      src/Squidex/AppServices.cs
  5. 76
      src/Squidex/app/features/rules/pages/rules/actions/email-action.component.html
  6. 1
      src/Squidex/app/features/rules/pages/rules/actions/email-action.component.scss
  7. 27
      src/Squidex/app/features/rules/pages/rules/actions/email-action.component.ts
  8. 10
      src/Squidex/appsettings.json

37
extensions/Squidex.Extensions/Actions/Email/EmailAction.cs

@ -12,16 +12,35 @@ namespace Squidex.Extensions.Actions.Email
{
[RuleActionHandler(typeof(EmailActionHandler))]
[RuleAction(
IconImage = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><path d='M28 5h-24c-2.209 0-4 1.792-4 4v13c0 2.209 1.791 4 4 4h24c2.209 0 4-1.791 4-4v-13c0-2.208-1.791-4-4-4zM2 10.25l6.999 5.25-6.999 5.25v-10.5zM30 22c0 1.104-0.898 2-2 2h-24c-1.103 0-2-0.896-2-2l7.832-5.875 4.368 3.277c0.533 0.398 1.166 0.6 1.8 0.6 0.633 0 1.266-0.201 1.799-0.6l4.369-3.277 7.832 5.875zM30 20.75l-7-5.25 7-5.25v10.5zM17.199 18.602c-0.349 0.262-0.763 0.4-1.199 0.4s-0.851-0.139-1.2-0.4l-12.8-9.602c0-1.103 0.897-2 2-2h24c1.102 0 2 0.897 2 2l-12.801 9.602z'/></svg>",
IconColor = "#333300",
Display = "Send an email",
Description = "Send an email, when some contents has changed",
ReadMore = "https://en.wikipedia.org/wiki/Email")]
IconImage = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><path d='M28 5h-24c-2.209 0-4 1.792-4 4v13c0 2.209 1.791 4 4 4h24c2.209 0 4-1.791 4-4v-13c0-2.208-1.791-4-4-4zM2 10.25l6.999 5.25-6.999 5.25v-10.5zM30 22c0 1.104-0.898 2-2 2h-24c-1.103 0-2-0.896-2-2l7.832-5.875 4.368 3.277c0.533 0.398 1.166 0.6 1.8 0.6 0.633 0 1.266-0.201 1.799-0.6l4.369-3.277 7.832 5.875zM30 20.75l-7-5.25 7-5.25v10.5zM17.199 18.602c-0.349 0.262-0.763 0.4-1.199 0.4s-0.851-0.139-1.2-0.4l-12.8-9.602c0-1.103 0.897-2 2-2h24c1.102 0 2 0.897 2 2l-12.801 9.602z'/></svg>",
IconColor = "#333300",
Display = "Send an email",
Description = "Send an email",
ReadMore = "https://en.wikipedia.org/wiki/Email")]
public class EmailAction : RuleAction
{
[Required]
[EmailAddress]
[Display(Name = "From", Description = "The mail is sent from?")]
[Display(Name = "Host", Description = "The Name or IP address of the host used for SMTP transactions.")]
public string Host { get; set; }
[Required]
[Display(Name = "Port", Description = "The port to be used on host.")]
public int Port { get; set; }
[Required]
[Display(Name = "EnableSsl", Description = "Specify whether the smtp client uses Secure Sockets Layer (SSL) to encrypt the connection.")]
public bool EnableSsl { get; set; }
[Required]
[Display(Name = "Username", Description = "The username used to authenticate the sender.")]
public string Username { get; set; }
[Required]
[Display(Name = "Password", Description = "The password used to authenticate the sender.")]
public string Password { get; set; }
[Required]
[Display(Name = "From", Description = "The email is sent from?")]
public string From { get; set; }
[Required]
@ -29,11 +48,11 @@ namespace Squidex.Extensions.Actions.Email
public string To { get; set; }
[Required]
[Display(Name = "Subject", Description = "The email Subject")]
[Display(Name = "Subject", Description = "The subject line for this e-mail message.")]
public string Subject { get; set; }
[Required]
[Display(Name = "Body", Description = "The email body")]
[Display(Name = "Body", Description = "The message body.")]
public string Body { get; set; }
}
}

33
extensions/Squidex.Extensions/Actions/Email/EmailActionHandler.cs

@ -22,33 +22,36 @@ namespace Squidex.Extensions.Actions.Email
{
private const string Description = "Send an Email";
private readonly EmailOptions emailOptions;
public EmailActionHandler(RuleEventFormatter formatter, IOptions<EmailOptions> emailOptions)
public EmailActionHandler(RuleEventFormatter formatter)
: base(formatter)
{
Guard.NotNull(emailOptions, nameof(emailOptions));
this.emailOptions = emailOptions.Value;
}
protected override (string Description, EmailJob Data) CreateJob(EnrichedEvent @event, EmailAction action)
{
var ruleJob = new EmailJob
{
From = action.From,
To = action.To,
Host = action.Host,
EnableSsl = action.EnableSsl,
Password = action.Password,
Port = action.Port,
Username = Format(action.Username, @event),
From = Format(action.From, @event),
To = Format(action.To, @event),
Subject = Format(action.Subject, @event),
Body = Format(action.Body, @event)
};
return (Description, ruleJob);
}
protected override async Task<(string Dump, Exception Exception)> ExecuteJobAsync(EmailJob job)
{
using (SmtpClient client = new SmtpClient(emailOptions.Host, emailOptions.Port))
using (var client = new SmtpClient(job.Host, job.Port))
{
client.EnableSsl = emailOptions.EnableSsl;
client.Credentials = new NetworkCredential(emailOptions.Username, emailOptions.Password);
client.EnableSsl = job.EnableSsl;
client.Credentials = new NetworkCredential(job.Username, job.Password);
using (var message = new MailMessage(job.From, job.To))
{
message.Subject = job.Subject;
@ -63,6 +66,16 @@ namespace Squidex.Extensions.Actions.Email
public class EmailJob
{
public string Host { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public bool EnableSsl { get; set; }
public string From { get; set; }
public string To { get; set; }

22
extensions/Squidex.Extensions/Actions/Email/EmailOptions.cs

@ -1,22 +0,0 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
namespace Squidex.Extensions.Actions.Email
{
public sealed class EmailOptions
{
public string Host { get; set; }
public int Port { get; set; }
public bool EnableSsl { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}

2
src/Squidex/AppServices.cs

@ -58,8 +58,6 @@ namespace Squidex
config.GetSection("mode"));
services.Configure<TwitterOptions>(
config.GetSection("twitter"));
services.Configure<EmailOptions>(
config.GetSection("smtpServer"));
services.Configure<RobotsTxtOptions>(
config.GetSection("robots"));
services.Configure<GCHealthCheckOptions>(

76
src/Squidex/app/features/rules/pages/rules/actions/email-action.component.html

@ -1,4 +1,74 @@
<div [formGroup]="actionForm" class="form-horizontal">
<div class="form-group row">
<label class="col-3 col-form-label" for="host">Host</label>
<div class="col-9">
<sqx-control-errors for="from" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="text" class="form-control" id="host" formControlName="host" />
<small class="form-text text-muted">
The Name or IP address of the host used for SMTP transactions.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="port">Port</label>
<div class="col-9">
<sqx-control-errors for="port" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="text" class="form-control" id="port" formControlName="port" />
<small class="form-text text-muted">
The port to be used on host.
</small>
</div>
</div>
<div class="form-group row">
<div class="col-9 offset-3">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="enableSsl" formControlName="enableSsl" />
<label class="form-check-label" for="enableSsl">
EnableSsl
</label>
</div>
<small class="form-text text-muted">
Specify whether the smtp client uses Secure Sockets Layer (SSL) to encrypt the connection.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="username">Username</label>
<div class="col-9">
<sqx-control-errors for="port" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="text" class="form-control" id="username" formControlName="username" />
<small class="form-text text-muted">
The username used to authenticate the sender.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="password">Password</label>
<div class="col-9">
<sqx-control-errors for="password" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="password" class="form-control" id="password" formControlName="password" />
<small class="form-text text-muted">
The password used to authenticate the sender.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="from">From</label>
@ -8,7 +78,7 @@
<input type="text" class="form-control" id="from" formControlName="from" />
<small class="form-text text-muted">
The mail is sent from?
The email is sent from?
</small>
</div>
</div>
@ -36,7 +106,7 @@
<input type="text" class="form-control" id="subject" formControlName="subject" />
<small class="form-text text-muted">
The Email Subject.
The subject line for this e-mail message.
</small>
</div>
</div>
@ -51,7 +121,7 @@
<textarea class="form-control" id="body" formControlName="body"></textarea>
<small class="form-text text-muted">
The Email body
The message body
</small>
</div>
</div>

1
src/Squidex/app/features/rules/pages/rules/actions/email-action.component.scss

@ -1,5 +1,6 @@
@import '_vars';
@import '_mixins';
textarea {
height: 250px;
}

27
src/Squidex/app/features/rules/pages/rules/actions/email-action.component.ts

@ -24,10 +24,33 @@ export class EmailActionComponent implements OnInit {
public actionFormSubmitted = false;
public ngOnInit() {
this.actionForm.setControl('host',
new FormControl(this.action.host || 'smtp.gmail.com', [
Validators.required
]));
this.actionForm.setControl('port',
new FormControl(this.action.port || 465, [
Validators.required,
Validators.pattern(/\d{2,6}/)
]));
this.actionForm.setControl('enableSsl',
new FormControl(this.action.enableSsl || true));
this.actionForm.setControl('username',
new FormControl(this.action.username || '', [
Validators.required
]));
this.actionForm.setControl('password',
new FormControl(this.action.password || '', [
Validators.required
]));
this.actionForm.setControl('from',
new FormControl(this.action.from || '', [
Validators.required,
Validators.email
Validators.required
]));
this.actionForm.setControl('to',

10
src/Squidex/appsettings.json

@ -304,15 +304,5 @@
* The client secret for twitter.
*/
"clientSecret": "Pdu9wdN72T33KJRFdFy1w4urBKDRzIyuKpc0OItQC2E616DuZD"
},
"smtpServer": {
/*
* The hostname of the smtp server
*/
"host": "smtp.gmail.com",
"port": 465,
"enableSsl": true,
"username": "<username>@gmail.com",
"password": "<password>"
}
}

Loading…
Cancel
Save