Skip to content

Commit bd780b7

Browse files
committed
PR feedback updates.
1 parent 51f900f commit bd780b7

File tree

9 files changed

+127
-68
lines changed

9 files changed

+127
-68
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#if NETFRAMEWORK
2-
using System;
3-
using System.Collections.Generic;
4-
52
using Umbraco.Core;
63
using Umbraco.Core.Composing;
74
using Umbraco.Core.Logging;
85
using Umbraco.Core.Models.PublishedContent;
9-
using Umbraco.Core.Scoping;
106
using Umbraco.Forms.Core.Data.Storage;
117
using Umbraco.Forms.Integrations.Automation.Zapier.Extensions;
128
using Umbraco.Forms.Integrations.Automation.Zapier.Helpers;
@@ -64,7 +60,7 @@ private void RecordStorage_RecordInserting(object sender, Core.RecordEventArgs e
6460
var umbracoPageId = e.Record.UmbracoPageId;
6561
var pageUrl = umbracoContext.UrlProvider.GetUrl(umbracoPageId, UrlMode.Absolute);
6662

67-
if (_zapierFormSubscriptionHookService.TryGetByName(e.Form.Name, out var zapFormConfigList))
63+
if (_zapierFormSubscriptionHookService.TryGetById(e.Form.Id.ToString(), out var zapFormConfigList))
6864
{
6965
var content = e.Form.ToFormDictionary(e.Record, pageUrl);
7066

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void Handle(RecordCreatingNotification notification)
5656
{
5757
var form = _formService.Get(notificationSavedEntity.Form);
5858

59-
if (_zapierFormSubscriptionHookService.TryGetByName(form.Name, out var zapFormConfigList))
59+
if (_zapierFormSubscriptionHookService.TryGetById(form.Id.ToString(), out var zapFormConfigList))
6060
{
6161
string pageUrl = string.Empty;
6262
if (_umbracoHelperAccessor.TryGetUmbracoHelper(out UmbracoHelper umbracoHelper))

src/Umbraco.Forms.Integrations.Automation.Zapier/Controllers/FormController.cs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111
using Umbraco.Cms.Web.Common.Controllers;
1212
#else
1313
using System.Configuration;
14-
15-
using Umbraco.Web.WebApi;
1614
#endif
1715

1816
namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
1917
{
2018
/// <summary>
2119
/// When a Zapier user creates a new "New Form Submitted" trigger, the API is used to provide him with the list of forms.
2220
/// </summary>
23-
public class FormController : UmbracoApiController
21+
public class FormController : ZapierFormAuthorizedApiController
2422
{
2523
private readonly ZapierSettings Options;
2624

@@ -30,8 +28,10 @@ public class FormController : UmbracoApiController
3028

3129
#if NETCOREAPP
3230
public FormController(IOptions<ZapierSettings> options, IUserValidationService userValidationService, ZapierFormService zapierFormService)
31+
: base(options, userValidationService)
3332
#else
3433
public FormController(ZapierFormService zapierFormService, IUserValidationService userValidationService)
34+
: base(userValidationService)
3535
#endif
3636
{
3737
#if NETCOREAPP
@@ -47,29 +47,7 @@ public FormController(ZapierFormService zapierFormService, IUserValidationServic
4747

4848
public IEnumerable<FormDto> GetForms()
4949
{
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>();
50+
if (!IsUserValid()) return Enumerable.Empty<FormDto>();
7351

7452
return _zapierFormService.GetAll();
7553
}
Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34

45
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
56

67
using Umbraco.Forms.Integrations.Automation.Zapier.Configuration;
8+
using Umbraco.Forms.Integrations.Automation.Zapier.Extensions;
79
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
810

911
#if NETCOREAPP
1012
using Microsoft.Extensions.Options;
11-
12-
using Umbraco.Cms.Web.Common.Controllers;
1313
#else
1414
using System.Configuration;
15-
16-
using Umbraco.Web.WebApi;
1715
#endif
1816

1917
namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
@@ -22,7 +20,7 @@ namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
2220
/// 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
2321
/// structure of the selected form.
2422
/// </summary>
25-
public class FormPollingController : UmbracoApiController
23+
public class FormPollingController : ZapierFormAuthorizedApiController
2624
{
2725
private readonly ZapierSettings Options;
2826

@@ -32,8 +30,10 @@ public class FormPollingController : UmbracoApiController
3230

3331
#if NETCOREAPP
3432
public FormPollingController(IOptions<ZapierSettings> options, ZapierFormService zapierFormService, IUserValidationService userValidationService)
33+
: base(options, userValidationService)
3534
#else
3635
public FormPollingController(ZapierFormService zapierFormService, IUserValidationService userValidationService)
36+
: base(userValidationService)
3737
#endif
3838
{
3939
#if NETCOREAPP
@@ -47,34 +47,22 @@ public FormPollingController(ZapierFormService zapierFormService, IUserValidatio
4747
_userValidationService = userValidationService;
4848
}
4949

50+
[Obsolete("Used only for Umbraco Zapier app v1.0.0. For updated versions use GetFormById")]
5051
public IEnumerable<FormDto> GetSampleForm()
5152
{
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
53+
if (!IsUserValid()) return null;
7054

71-
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return null;
55+
return _zapierFormService.GetAll().Take(1);
56+
}
7257

73-
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter()
74-
.GetResult();
75-
if (!isAuthorized) return null;
58+
public List<Dictionary<string, string>> GetFormPropertiesById(string id)
59+
{
60+
if (!IsUserValid()) return new List<Dictionary<string, string>>();
7661

77-
return _zapierFormService.GetAll().Take(1);
62+
var form = _zapierFormService.GetById(id);
63+
return form != null
64+
? new List<Dictionary<string, string>> { form.ToFormDictionary() }
65+
: new List<Dictionary<string, string>>();
7866
}
7967
}
8068
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Linq;
2+
3+
using Umbraco.Forms.Integrations.Automation.Zapier.Configuration;
4+
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
5+
6+
#if NETCOREAPP
7+
using Microsoft.Extensions.Options;
8+
9+
using Umbraco.Cms.Web.Common.Controllers;
10+
#else
11+
using System.Configuration;
12+
13+
using Umbraco.Web.WebApi;
14+
#endif
15+
16+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
17+
{
18+
public class ZapierFormAuthorizedApiController : UmbracoApiController
19+
{
20+
private readonly ZapierSettings Options;
21+
22+
private readonly IUserValidationService _userValidationService;
23+
24+
#if NETCOREAPP
25+
public ZapierFormAuthorizedApiController(IOptions<ZapierSettings> options, IUserValidationService userValidationService)
26+
#else
27+
public ZapierFormAuthorizedApiController(IUserValidationService userValidationService)
28+
#endif
29+
{
30+
#if NETCOREAPP
31+
Options = options.Value;
32+
#else
33+
Options = new ZapierSettings(ConfigurationManager.AppSettings);
34+
#endif
35+
36+
_userValidationService = userValidationService;
37+
}
38+
39+
public bool IsUserValid()
40+
{
41+
string username = string.Empty;
42+
string password = string.Empty;
43+
44+
#if NETCOREAPP
45+
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.UsernameHeaderKey,
46+
out var usernameValues))
47+
username = usernameValues.First();
48+
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.PasswordHeaderKey,
49+
out var passwordValues))
50+
password = passwordValues.First();
51+
#else
52+
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.UsernameHeaderKey,
53+
out var usernameValues))
54+
username = usernameValues.First();
55+
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.PasswordHeaderKey,
56+
out var passwordValues))
57+
password = passwordValues.First();
58+
#endif
59+
60+
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return false;
61+
62+
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter()
63+
.GetResult();
64+
if (!isAuthorized) return false;
65+
66+
return true;
67+
}
68+
}
69+
}

src/Umbraco.Forms.Integrations.Automation.Zapier/Extensions/FormExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,23 @@ public static Dictionary<string, string> ToFormDictionary(this Form form, Record
2525

2626
return contentDict;
2727
}
28+
29+
public static Dictionary<string, string> ToFormDictionary(this Form form)
30+
{
31+
var contentDict = new Dictionary<string, string>
32+
{
33+
{ Constants.Form.Id, form.Id.ToString() },
34+
{ Constants.Form.Name, form.Name },
35+
{ Constants.Form.SubmissionDate, DateTime.UtcNow.ToString("s") },
36+
{ Constants.Form.PageUrl, string.Empty }
37+
};
38+
39+
foreach (var field in form.AllFields)
40+
{
41+
contentDict.Add(field.Alias, string.Empty);
42+
}
43+
44+
return contentDict;
45+
}
2846
}
2947
}

src/Umbraco.Forms.Integrations.Automation.Zapier/Services/UserValidationService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Umbraco.Cms.Core.Services;
77
#else
88
using Umbraco.Core.Services;
9-
using Umbraco.Web;
109
#endif
1110

1211
namespace Umbraco.Forms.Integrations.Automation.Zapier.Services

src/Umbraco.Forms.Integrations.Automation.Zapier/Services/ZapierFormService.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34

4-
#if NETFRAMEWORK
5+
#if NETCOREAPP
6+
using Umbraco.Forms.Core.Services;
7+
#else
58
using Umbraco.Forms.Core.Data.Storage;
69
#endif
710

811
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
9-
10-
using Umbraco.Forms.Core.Services;
12+
using Umbraco.Forms.Core.Models;
1113

1214
namespace Umbraco.Forms.Integrations.Automation.Zapier.Services
1315
{
@@ -47,5 +49,14 @@ public IEnumerable<FormDto> GetAll()
4749
Name = p.Name
4850
});
4951
}
52+
53+
public Form GetById(string id)
54+
{
55+
#if NETCOREAPP
56+
return _formService.Get().FirstOrDefault(p => p.Id == Guid.Parse(id));
57+
#else
58+
return _formStorage.GetAll().FirstOrDefault(p => p.Id == Guid.Parse(id));
59+
#endif
60+
}
5061
}
5162
}

src/Umbraco.Forms.Integrations.Automation.Zapier/Services/ZapierFormSubscriptionHookService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public ZapierFormSubscriptionHookService(IScopeProvider scopeProvider, ILogger l
3838
}
3939
#endif
4040

41-
public bool TryGetByName(string formName, out IEnumerable<FormConfigDto> dto)
41+
public bool TryGetById(string id, out IEnumerable<FormConfigDto> dto)
4242
{
4343
using (var scope = _scopeProvider.CreateScope())
4444
{
4545
var entities =
4646
scope.Database
47-
.Query<FormConfigDto>( "SELECT * FROM zapierFormSubscriptionHook where FormName = @0", formName)
47+
.Query<FormConfigDto>( "SELECT * FROM zapierSubscriptionHook where EntityId = @0", id)
4848
.ToList();
4949

5050
dto = entities.Any()

0 commit comments

Comments
 (0)