Skip to content

Commit 51f900f

Browse files
committed
PR updates
1 parent 45d0462 commit 51f900f

File tree

11 files changed

+341
-36
lines changed

11 files changed

+341
-36
lines changed

src/Umbraco.Forms.Integrations.Automation.Zapier/Components/NewFormSubmittedComponent.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#if NETFRAMEWORK
22
using System;
33
using System.Collections.Generic;
4-
using System.Linq;
54

65
using Umbraco.Core;
76
using Umbraco.Core.Composing;
87
using Umbraco.Core.Logging;
98
using Umbraco.Core.Models.PublishedContent;
9+
using Umbraco.Core.Scoping;
1010
using Umbraco.Forms.Core.Data.Storage;
11+
using Umbraco.Forms.Integrations.Automation.Zapier.Extensions;
1112
using Umbraco.Forms.Integrations.Automation.Zapier.Helpers;
12-
using Umbraco.Forms.Integrations.Automation.Zapier.Models.Dtos;
1313
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
1414
using Umbraco.Web;
1515

@@ -28,7 +28,7 @@ public class NewFormSubmittedComponent : IComponent
2828
private readonly ZapierService _zapierService;
2929

3030
private readonly ZapierFormSubscriptionHookService _zapierFormSubscriptionHookService;
31-
31+
3232
private readonly ILogger _logger;
3333

3434
public NewFormSubmittedComponent(IUmbracoContextAccessor umbracoContextAccessor, IRecordStorage recordStorage,
@@ -66,18 +66,7 @@ private void RecordStorage_RecordInserting(object sender, Core.RecordEventArgs e
6666

6767
if (_zapierFormSubscriptionHookService.TryGetByName(e.Form.Name, out var zapFormConfigList))
6868
{
69-
var content = new Dictionary<string, string>
70-
{
71-
{ Constants.Form.Id, e.Form.Id.ToString() },
72-
{ Constants.Form.Name, e.Form.Name },
73-
{ Constants.Form.SubmissionDate, DateTime.UtcNow.ToString("s") },
74-
{ Constants.Form.PageUrl, pageUrl }
75-
};
76-
77-
foreach (var recordField in e.Record.RecordFields)
78-
{
79-
content.Add(recordField.Value.Alias, recordField.Value.ValuesAsString());
80-
}
69+
var content = e.Form.ToFormDictionary(e.Record, pageUrl);
8170

8271
foreach (var zapFormConfig in zapFormConfigList)
8372
{

src/Umbraco.Forms.Integrations.Automation.Zapier/Components/NewFormSubmittedNotification.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
#if NETCOREAPP
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
62
using Microsoft.Extensions.Logging;
73

84
using Umbraco.Cms.Core.Events;
@@ -12,8 +8,8 @@
128
using Umbraco.Extensions;
139
using Umbraco.Forms.Core.Services;
1410
using Umbraco.Forms.Core.Services.Notifications;
11+
using Umbraco.Forms.Integrations.Automation.Zapier.Extensions;
1512
using Umbraco.Forms.Integrations.Automation.Zapier.Helpers;
16-
using Umbraco.Forms.Integrations.Automation.Zapier.Models.Dtos;
1713
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
1814

1915
namespace Umbraco.Forms.Integrations.Automation.Zapier.Components
@@ -72,18 +68,7 @@ public void Handle(RecordCreatingNotification notification)
7268
}
7369
}
7470

75-
var content = new Dictionary<string, string>
76-
{
77-
{ Constants.Form.Id, form.Id.ToString() },
78-
{ Constants.Form.Name, form.Name },
79-
{ Constants.Form.SubmissionDate, DateTime.UtcNow.ToString("s") },
80-
{ Constants.Form.PageUrl, pageUrl }
81-
};
82-
83-
foreach (var recordField in notificationSavedEntity.RecordFields)
84-
{
85-
content.Add(recordField.Value.Alias, recordField.Value.ValuesAsString());
86-
}
71+
var content = form.ToFormDictionary(notificationSavedEntity, pageUrl);
8772

8873
foreach (var formConfigDto in zapFormConfigList)
8974
{

src/Umbraco.Forms.Integrations.Automation.Zapier/Constants.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ public class Constants
55
{
66
public const string UmbracoFormsIntegrationsAutomationZapierUserGroup = "Umbraco.Forms.Integrations.Automation.Zapier.UserGroup";
77

8+
public static class ZapierAppConfiguration
9+
{
10+
public const string UsernameHeaderKey = "X-USERNAME";
11+
12+
public const string PasswordHeaderKey = "X-PASSWORD";
13+
}
14+
815
public static class Configuration
916
{
1017
public const string Settings = "Umbraco:Forms:Integrations:Automation:Zapier:Settings";
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
5+
using Umbraco.Forms.Integrations.Automation.Zapier.Configuration;
6+
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
7+
8+
#if NETCOREAPP
9+
using Microsoft.Extensions.Options;
10+
11+
using Umbraco.Cms.Web.Common.Controllers;
12+
#else
13+
using System.Configuration;
14+
15+
using Umbraco.Web.WebApi;
16+
#endif
17+
18+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
19+
{
20+
/// <summary>
21+
/// When a Zapier user creates a new "New Form Submitted" trigger, the API is used to provide him with the list of forms.
22+
/// </summary>
23+
public class FormController : UmbracoApiController
24+
{
25+
private readonly ZapierSettings Options;
26+
27+
private readonly IUserValidationService _userValidationService;
28+
29+
private readonly ZapierFormService _zapierFormService;
30+
31+
#if NETCOREAPP
32+
public FormController(IOptions<ZapierSettings> options, IUserValidationService userValidationService, ZapierFormService zapierFormService)
33+
#else
34+
public FormController(ZapierFormService zapierFormService, IUserValidationService userValidationService)
35+
#endif
36+
{
37+
#if NETCOREAPP
38+
Options = options.Value;
39+
#else
40+
Options = new ZapierSettings(ConfigurationManager.AppSettings);
41+
#endif
42+
43+
_zapierFormService = zapierFormService;
44+
45+
_userValidationService = userValidationService;
46+
}
47+
48+
public IEnumerable<FormDto> GetForms()
49+
{
50+
string username = string.Empty;
51+
string password = string.Empty;
52+
53+
#if NETCOREAPP
54+
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.UsernameHeaderKey,
55+
out var usernameValues))
56+
username = usernameValues.First();
57+
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.PasswordHeaderKey,
58+
out var passwordValues))
59+
password = passwordValues.First();
60+
#else
61+
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.UsernameHeaderKey,
62+
out var usernameValues))
63+
username = usernameValues.First();
64+
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.PasswordHeaderKey,
65+
out var passwordValues))
66+
password = passwordValues.First();
67+
#endif
68+
69+
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return Enumerable.Empty<FormDto>();
70+
71+
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter().GetResult();
72+
if (!isAuthorized) return Enumerable.Empty<FormDto>();
73+
74+
return _zapierFormService.GetAll();
75+
}
76+
77+
}
78+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
5+
6+
using Umbraco.Forms.Integrations.Automation.Zapier.Configuration;
7+
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
8+
9+
#if NETCOREAPP
10+
using Microsoft.Extensions.Options;
11+
12+
using Umbraco.Cms.Web.Common.Controllers;
13+
#else
14+
using System.Configuration;
15+
16+
using Umbraco.Web.WebApi;
17+
#endif
18+
19+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
20+
{
21+
/// <summary>
22+
/// When a Zapier user creates a "New Form Submitted" trigger, he is authenticated, then selects a form, the API provides an output json with the
23+
/// structure of the selected form.
24+
/// </summary>
25+
public class FormPollingController : UmbracoApiController
26+
{
27+
private readonly ZapierSettings Options;
28+
29+
private readonly ZapierFormService _zapierFormService;
30+
31+
private readonly IUserValidationService _userValidationService;
32+
33+
#if NETCOREAPP
34+
public FormPollingController(IOptions<ZapierSettings> options, ZapierFormService zapierFormService, IUserValidationService userValidationService)
35+
#else
36+
public FormPollingController(ZapierFormService zapierFormService, IUserValidationService userValidationService)
37+
#endif
38+
{
39+
#if NETCOREAPP
40+
Options = options.Value;
41+
#else
42+
Options = new ZapierSettings(ConfigurationManager.AppSettings);
43+
#endif
44+
45+
_zapierFormService = zapierFormService;
46+
47+
_userValidationService = userValidationService;
48+
}
49+
50+
public IEnumerable<FormDto> GetSampleForm()
51+
{
52+
string username = string.Empty;
53+
string password = string.Empty;
54+
55+
#if NETCOREAPP
56+
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.UsernameHeaderKey,
57+
out var usernameValues))
58+
username = usernameValues.First();
59+
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.PasswordHeaderKey,
60+
out var passwordValues))
61+
password = passwordValues.First();
62+
#else
63+
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.UsernameHeaderKey,
64+
out var usernameValues))
65+
username = usernameValues.First();
66+
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.PasswordHeaderKey,
67+
out var passwordValues))
68+
password = passwordValues.First();
69+
#endif
70+
71+
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return null;
72+
73+
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter()
74+
.GetResult();
75+
if (!isAuthorized) return null;
76+
77+
return _zapierFormService.GetAll().Take(1);
78+
}
79+
}
80+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using Umbraco.Forms.Core.Models;
5+
using Umbraco.Forms.Core.Persistence.Dtos;
6+
7+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Extensions
8+
{
9+
public static class FormExtensions
10+
{
11+
public static Dictionary<string, string> ToFormDictionary(this Form form, Record record, string pageUrl)
12+
{
13+
var contentDict = new Dictionary<string, string>
14+
{
15+
{ Constants.Form.Id, form.Id.ToString() },
16+
{ Constants.Form.Name, form.Name },
17+
{ Constants.Form.SubmissionDate, DateTime.UtcNow.ToString("s") },
18+
{ Constants.Form.PageUrl, pageUrl }
19+
};
20+
21+
foreach (var recordField in record.RecordFields)
22+
{
23+
contentDict.Add(recordField.Value.Alias, recordField.Value.ValuesAsString());
24+
}
25+
26+
return contentDict;
27+
}
28+
}
29+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+

2+
using Newtonsoft.Json;
3+
4+
namespace Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos
5+
{
6+
public class FormDto
7+
{
8+
[JsonProperty("id")]
9+
public string Id { get; set; }
10+
11+
[JsonProperty("name")]
12+
public string Name { get; set; }
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Threading.Tasks;
2+
3+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Services
4+
{
5+
public interface IUserValidationService
6+
{
7+
Task<bool> Validate(string username, string password, string userGroup);
8+
}
9+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
4+
#if NETCOREAPP
5+
using Umbraco.Cms.Core.Security;
6+
using Umbraco.Cms.Core.Services;
7+
#else
8+
using Umbraco.Core.Services;
9+
using Umbraco.Web;
10+
#endif
11+
12+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Services
13+
{
14+
public class UserValidationService : IUserValidationService
15+
{
16+
private readonly IUserService _userService;
17+
18+
19+
#if NETCOREAPP
20+
private readonly IBackOfficeUserManager _backOfficeUserManager;
21+
22+
public UserValidationService(IBackOfficeUserManager backOfficeUserManager, IUserService userService)
23+
{
24+
_backOfficeUserManager = backOfficeUserManager;
25+
}
26+
#else
27+
public UserValidationService(IUserService userService)
28+
{
29+
_userService = userService;
30+
}
31+
#endif
32+
33+
public async Task<bool> Validate(string username, string password, string userGroup)
34+
{
35+
#if NETCOREAPP
36+
var isUserValid =
37+
await _backOfficeUserManager.ValidateCredentialsAsync(username, password);
38+
#else
39+
var isUserValid = Umbraco.Web.Composing.Current.UmbracoContext.Security.ValidateBackOfficeCredentials(username, password);
40+
#endif
41+
42+
if (!isUserValid) return false;
43+
44+
if (!string.IsNullOrEmpty(userGroup))
45+
{
46+
var user = _userService.GetByUsername(username);
47+
48+
return user != null && user.Groups.Any(p => p.Name == userGroup);
49+
}
50+
51+
return true;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)