-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
1,003 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/OrchardCore.Modules/OrchardCore.Sms.Azure/Activities/SmsTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.Workflows.Abstractions.Models; | ||
using OrchardCore.Workflows.Activities; | ||
using OrchardCore.Workflows.Models; | ||
using OrchardCore.Workflows.Services; | ||
|
||
namespace OrchardCore.Sms.Azure.Activities; | ||
|
||
public class SmsTask : TaskActivity<SmsTask> | ||
{ | ||
private readonly ISmsService _smsService; | ||
private readonly IWorkflowExpressionEvaluator _expressionEvaluator; | ||
protected readonly IStringLocalizer S; | ||
|
||
public SmsTask( | ||
ISmsService smsService, | ||
IWorkflowExpressionEvaluator expressionEvaluator, | ||
IStringLocalizer<SmsTask> stringLocalizer | ||
) | ||
{ | ||
_smsService = smsService; | ||
_expressionEvaluator = expressionEvaluator; | ||
S = stringLocalizer; | ||
} | ||
|
||
public override LocalizedString DisplayText => S["SMS Task"]; | ||
|
||
public override LocalizedString Category => S["Messaging"]; | ||
|
||
public WorkflowExpression<string> PhoneNumber | ||
{ | ||
get => GetProperty(() => new WorkflowExpression<string>()); | ||
set => SetProperty(value); | ||
} | ||
|
||
public WorkflowExpression<string> Body | ||
{ | ||
get => GetProperty(() => new WorkflowExpression<string>()); | ||
set => SetProperty(value); | ||
} | ||
|
||
public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext) | ||
{ | ||
return Outcomes(S["Done"], S["Failed"]); | ||
} | ||
|
||
public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext) | ||
{ | ||
var message = new SmsMessage | ||
{ | ||
To = await _expressionEvaluator.EvaluateAsync(PhoneNumber, workflowContext, null), | ||
Body = await _expressionEvaluator.EvaluateAsync(Body, workflowContext, null), | ||
}; | ||
|
||
var result = await _smsService.SendAsync(message); | ||
|
||
workflowContext.LastResult = result; | ||
|
||
if (result.Succeeded) | ||
{ | ||
return Outcomes("Done"); | ||
} | ||
|
||
return Outcomes("Failed"); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/OrchardCore.Modules/OrchardCore.Sms.Azure/AdminMenu.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.Mvc.Core.Utilities; | ||
using OrchardCore.Navigation; | ||
using OrchardCore.Sms.Azure.Controllers; | ||
|
||
namespace OrchardCore.Sms.Azure; | ||
|
||
public class AdminMenu : INavigationProvider | ||
{ | ||
private static readonly RouteValueDictionary _routeValues = new() | ||
{ | ||
{ "area", "OrchardCore.Settings" }, | ||
{ "groupId", SmsSettings.GroupId }, | ||
}; | ||
|
||
protected readonly IStringLocalizer S; | ||
|
||
public AdminMenu(IStringLocalizer<AdminMenu> stringLocalizer) | ||
{ | ||
S = stringLocalizer; | ||
} | ||
|
||
public Task BuildNavigationAsync(string name, NavigationBuilder builder) | ||
{ | ||
if (!NavigationHelper.IsAdminMenu(name)) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
builder | ||
.Add(S["Configuration"], configuration => configuration | ||
.Add(S["Settings"], settings => settings | ||
.Add(S["SMS"], S["SMS"].PrefixPosition(), sms => sms | ||
.AddClass("sms") | ||
.Id("sms") | ||
.Action("Index", "Admin", _routeValues) | ||
.Permission(SmsPermissions.ManageSmsSettings) | ||
.LocalNav() | ||
) | ||
.Add(S["SMS Test"], S["SMS Test"].PrefixPosition(), sms => sms | ||
.AddClass("smstest") | ||
.Id("smstest") | ||
.Action(nameof(AdminController.Test), typeof(AdminController).ControllerName(), "OrchardCore.Sms") | ||
.Permission(SmsPermissions.ManageSmsSettings) | ||
.LocalNav() | ||
) | ||
) | ||
); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/OrchardCore.Modules/OrchardCore.Sms.Azure/Controllers/AdminController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Localization; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.Extensions.Localization; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.Admin; | ||
using OrchardCore.DisplayManagement.Notify; | ||
using OrchardCore.Sms.Azure.ViewModels; | ||
|
||
namespace OrchardCore.Sms.Azure.Controllers; | ||
|
||
public class AdminController : Controller | ||
{ | ||
private readonly SmsProviderOptions _smsProviderOptions; | ||
private readonly IPhoneFormatValidator _phoneFormatValidator; | ||
private readonly INotifier _notifier; | ||
private readonly IAuthorizationService _authorizationService; | ||
private readonly ISmsProviderResolver _smsProviderResolver; | ||
|
||
protected readonly IHtmlLocalizer H; | ||
protected readonly IStringLocalizer S; | ||
|
||
public AdminController( | ||
IOptions<SmsProviderOptions> smsProviderOptions, | ||
IPhoneFormatValidator phoneFormatValidator, | ||
ISmsProviderResolver smsProviderResolver, | ||
INotifier notifier, | ||
IAuthorizationService authorizationService, | ||
IHtmlLocalizer<AdminController> htmlLocalizer, | ||
IStringLocalizer<AdminController> stringLocalizer) | ||
{ | ||
_smsProviderOptions = smsProviderOptions.Value; | ||
_phoneFormatValidator = phoneFormatValidator; | ||
_smsProviderResolver = smsProviderResolver; | ||
_notifier = notifier; | ||
_authorizationService = authorizationService; | ||
H = htmlLocalizer; | ||
S = stringLocalizer; | ||
} | ||
|
||
[Admin("sms/test", "SmsProviderTest")] | ||
public async Task<IActionResult> Test() | ||
{ | ||
if (!await _authorizationService.AuthorizeAsync(User, SmsPermissions.ManageSmsSettings)) | ||
{ | ||
return Forbid(); | ||
} | ||
|
||
var model = new SmsTestViewModel(); | ||
|
||
PopulateModel(model); | ||
|
||
return View(model); | ||
} | ||
|
||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public async Task<IActionResult> Test(SmsTestViewModel model) | ||
{ | ||
if (!await _authorizationService.AuthorizeAsync(User, SmsPermissions.ManageSmsSettings)) | ||
{ | ||
return Forbid(); | ||
} | ||
|
||
if (ModelState.IsValid) | ||
{ | ||
var provider = await _smsProviderResolver.GetAsync(model.Provider); | ||
|
||
if (provider is null) | ||
{ | ||
ModelState.AddModelError(nameof(model.Provider), S["Please select a valid provider."]); | ||
} | ||
else if (!_phoneFormatValidator.IsValid(model.PhoneNumber)) | ||
{ | ||
ModelState.AddModelError(nameof(model.PhoneNumber), S["Please provide a valid phone number."]); | ||
} | ||
else | ||
{ | ||
var result = await provider.SendAsync(new SmsMessage() | ||
{ | ||
To = model.PhoneNumber, | ||
Body = S["This is a test SMS message."] | ||
}); | ||
|
||
if (result.Succeeded) | ||
{ | ||
await _notifier.SuccessAsync(H["The test SMS message has been successfully sent."]); | ||
|
||
return RedirectToAction(nameof(Test)); | ||
} | ||
else | ||
{ | ||
await _notifier.ErrorAsync(H["The test SMS message failed to send."]); | ||
} | ||
} | ||
} | ||
|
||
PopulateModel(model); | ||
|
||
return View(model); | ||
} | ||
|
||
private void PopulateModel(SmsTestViewModel model) | ||
{ | ||
model.Providers = _smsProviderOptions.Providers | ||
.Where(entry => entry.Value.IsEnabled) | ||
.Select(entry => new SelectListItem(entry.Key, entry.Key)) | ||
.OrderBy(item => item.Text) | ||
.ToArray(); | ||
} | ||
} |
Oops, something went wrong.