Skip to content

Commit 1ba1a70

Browse files
committed
Forms integration
1 parent 28fe2de commit 1ba1a70

File tree

8 files changed

+278
-4
lines changed

8 files changed

+278
-4
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
using Umbraco.Cms.Integrations.Automation.Zapier.Configuration;
5+
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
6+
using Umbraco.Cms.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.Cms.Integrations.Automation.Zapier.Controllers
19+
{
20+
public class FormController : UmbracoApiController
21+
{
22+
private readonly ZapierSettings Options;
23+
24+
private readonly IUserValidationService _userValidationService;
25+
26+
private readonly ZapierFormService _zapierFormService;
27+
28+
#if NETCOREAPP
29+
public FormController(IOptions<ZapierSettings> options, IUserValidationService userValidationService, ZapierFormService zapierFormService)
30+
#else
31+
public FormController(ZapierFormService zapierFormService, IUserValidationService userValidationService)
32+
#endif
33+
{
34+
#if NETCOREAPP
35+
Options = options.Value;
36+
#else
37+
Options = new ZapierSettings(ConfigurationManager.AppSettings);
38+
#endif
39+
40+
_zapierFormService = zapierFormService;
41+
42+
_userValidationService = userValidationService;
43+
}
44+
45+
public IEnumerable<FormDto> GetForms()
46+
{
47+
string username = string.Empty;
48+
string password = string.Empty;
49+
50+
#if NETCOREAPP
51+
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.UsernameHeaderKey,
52+
out var usernameValues))
53+
username = usernameValues.First();
54+
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.PasswordHeaderKey,
55+
out var passwordValues))
56+
password = passwordValues.First();
57+
#else
58+
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.UsernameHeaderKey,
59+
out var usernameValues))
60+
username = usernameValues.First();
61+
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.PasswordHeaderKey,
62+
out var passwordValues))
63+
password = passwordValues.First();
64+
#endif
65+
66+
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return Enumerable.Empty<FormDto>();
67+
68+
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter().GetResult();
69+
if (!isAuthorized) return Enumerable.Empty<FormDto>();
70+
71+
return _zapierFormService.GetAll();
72+
}
73+
74+
}
75+
}

src/Umbraco.Cms.Integrations.Automation.Zapier/Controllers/PollingController.cs

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

34
using System.Linq;
45

@@ -26,12 +27,14 @@ public class PollingController : UmbracoApiController
2627

2728
private IContentService _contentService;
2829

30+
private ZapierFormService _zapierFormService;
31+
2932
private readonly IUserValidationService _userValidationService;
3033

3134
#if NETCOREAPP
32-
public PollingController(IOptions<ZapierSettings> options, IContentService contentService, IUserValidationService userValidationService)
35+
public PollingController(IOptions<ZapierSettings> options, IContentService contentService, ZapierFormService zapierFormService, IUserValidationService userValidationService)
3336
#else
34-
public PollingController(IContentService contentService, IUserValidationService userValidationService)
37+
public PollingController(IContentService contentService, ZapierFormService zapierFormService, IUserValidationService userValidationService)
3538
#endif
3639
{
3740
#if NETCOREAPP
@@ -42,6 +45,8 @@ public PollingController(IContentService contentService, IUserValidationService
4245

4346
_contentService = contentService;
4447

48+
_zapierFormService = zapierFormService;
49+
4550
_userValidationService = userValidationService;
4651
}
4752

@@ -68,7 +73,8 @@ public IEnumerable<PublishedContentDto> GetSampleContent()
6873

6974
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return null;
7075

71-
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter().GetResult();
76+
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter()
77+
.GetResult();
7278
if (!isAuthorized) return null;
7379

7480
var rootNodes = _contentService.GetRootContent().Where(p => p.Published)
@@ -82,5 +88,9 @@ public IEnumerable<PublishedContentDto> GetSampleContent()
8288
});
8389
}
8490

91+
public IEnumerable<FormDto> GetSampleForm()
92+
{
93+
return _zapierFormService.GetAll();
94+
}
8595
}
8696
}

src/Umbraco.Cms.Integrations.Automation.Zapier/Controllers/SubscriptionController.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,37 @@ public bool UpdatePreferences([FromBody] SubscriptionDto dto)
7777

7878
return string.IsNullOrEmpty(result);
7979
}
80+
81+
[HttpPost]
82+
public bool UpdateFormPreferences([FromBody] FormSubscriptionDto dto)
83+
{
84+
string username = string.Empty;
85+
string password = string.Empty;
86+
87+
#if NETCOREAPP
88+
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.UsernameHeaderKey,
89+
out var usernameValues))
90+
username = usernameValues.First();
91+
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.PasswordHeaderKey,
92+
out var passwordValues))
93+
password = passwordValues.First();
94+
#else
95+
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.UsernameHeaderKey,
96+
out var usernameValues))
97+
username = usernameValues.First();
98+
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.PasswordHeaderKey,
99+
out var passwordValues))
100+
password = passwordValues.First();
101+
#endif
102+
103+
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return false;
104+
105+
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter().GetResult();
106+
if (!isAuthorized) return false;
107+
108+
if (dto == null) return false;
109+
110+
return true;
111+
}
80112
}
81113
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
namespace Umbraco.Cms.Integrations.Automation.Zapier.Helpers
7+
{
8+
public class FormHelper
9+
{
10+
public static IEnumerable<MethodInfo> GetMethodsForType(Type type)
11+
{
12+
foreach (var method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
13+
{
14+
yield return method;
15+
}
16+
17+
if (type.IsInterface)
18+
{
19+
foreach (var iface in type.GetInterfaces())
20+
{
21+
foreach (var method in GetMethodsForType(iface))
22+
{
23+
yield return method;
24+
}
25+
}
26+
}
27+
}
28+
29+
public static MethodInfo GetMethodForTypeByName(Type type, string name, object[] parameters) =>
30+
GetMethodsForType(type).First(p => p.Name == name && p.GetParameters().Length == parameters.Length);
31+
}
32+
}
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos
4+
{
5+
public class FormSubscriptionDto
6+
{
7+
[JsonProperty("hookUrl")]
8+
public string HookUrl { get; set; }
9+
10+
[JsonProperty("formId")]
11+
public string FormId { get; set; }
12+
13+
[JsonProperty("subscribeHook")]
14+
public bool SubscribeHook { get; set; }
15+
}
16+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
using Umbraco.Cms.Integrations.Automation.Zapier.Helpers;
6+
7+
#if NETCOREAPP
8+
#else
9+
using System.Web.Mvc;
10+
#endif
11+
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
12+
13+
namespace Umbraco.Cms.Integrations.Automation.Zapier.Services
14+
{
15+
public class ZapierFormService
16+
{
17+
#if NETCOREAPP
18+
private readonly IServiceProvider _serviceProvider;
19+
20+
public ZapierFormService(IServiceProvider serviceProvider)
21+
{
22+
_serviceProvider = serviceProvider;
23+
}
24+
#endif
25+
26+
private Type FormServiceType => Type.GetType("Umbraco.Forms.Core.Services.IFormService, Umbraco.Forms.Core");
27+
28+
private Type FormStorageType =>
29+
Type.GetType("Umbraco.Forms.Core.Data.Storage.IFormStorage, Umbraco.Forms.Core");
30+
31+
public bool TryResolveFormService(out object formService)
32+
{
33+
#if NETCOREAPP
34+
formService = _serviceProvider.GetService(FormServiceType);
35+
#else
36+
37+
formService = DependencyResolver.Current.GetService(FormServiceType);
38+
#endif
39+
40+
return formService != null;
41+
}
42+
43+
#if NETCOREAPP
44+
public IEnumerable<FormDto> GetAll()
45+
{
46+
if (!TryResolveFormService(out var formService)) return Enumerable.Empty<FormDto>();
47+
48+
var getAllFormsMethod = FormHelper.GetMethodForTypeByName(FormServiceType, "Get", new object[] {});
49+
50+
var forms = (IEnumerable<object>) getAllFormsMethod.Invoke(formService, null);
51+
52+
return Enumerable.Empty<FormDto>();
53+
}
54+
#else
55+
/// <summary>
56+
/// for V8 use IFormStorage to retrieve forms saved on disk also
57+
/// </summary>
58+
/// <param name="formStorage"></param>
59+
/// <returns></returns>
60+
public bool TryResolveFormStorage(out object formStorage)
61+
{
62+
formStorage = DependencyResolver.Current.GetService(FormStorageType);
63+
64+
return formStorage != null;
65+
}
66+
67+
public IEnumerable<FormDto> GetAll()
68+
{
69+
if (!TryResolveFormStorage(out var formStorage)) return Enumerable.Empty<FormDto>();
70+
71+
try
72+
{
73+
var getAllFormsMethod = FormHelper.GetMethodForTypeByName(FormStorageType, "GetAll", new object[] { });
74+
75+
var forms = (IEnumerable<object>)getAllFormsMethod.Invoke(formStorage, null);
76+
77+
return forms.Select(p => new FormDto
78+
{
79+
Id = p.GetType().GetProperty("Id").GetValue(p, null).ToString(),
80+
Name = p.GetType().GetProperty("Name").GetValue(p, null).ToString()
81+
});
82+
}
83+
catch (Exception e)
84+
{
85+
return Enumerable.Empty<FormDto>();
86+
}
87+
88+
}
89+
#endif
90+
}
91+
}

src/Umbraco.Cms.Integrations.Automation.Zapier/ZapierComposer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public void Compose(IUmbracoBuilder builder)
3232

3333
builder.Services.AddSingleton<ZapierService>();
3434

35+
builder.Services.AddSingleton<ZapierFormService>();
36+
3537
builder.Services.AddScoped<IUserValidationService, UserValidationService>();
3638
}
3739
#else
@@ -41,6 +43,8 @@ public void Compose(Composition composition)
4143

4244
composition.Register<ZapierService>(Lifetime.Singleton);
4345

46+
composition.Register<ZapierFormService>(Lifetime.Singleton);
47+
4448
composition.Register<IUserValidationService, UserValidationService>(Lifetime.Scope);
4549
}
4650
#endif

0 commit comments

Comments
 (0)