Skip to content

Commit 63dedaf

Browse files
committed
PR feedback updates
1 parent a0d9bcf commit 63dedaf

File tree

9 files changed

+108
-192
lines changed

9 files changed

+108
-192
lines changed

src/Umbraco.Cms.Integrations.Automation.Zapier/Components/NewContentPublishedComponent.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ private void ContentServiceOnPublished(IContentService sender, ContentPublishedE
4949

5050
foreach (var node in e.PublishedEntities)
5151
{
52-
var x = node.ToContentDictionary();
53-
5452
if (_zapierSubscriptionHookService.TryGetByAlias(node.ContentType.Alias, out var zapContentConfigList))
5553
{
5654
var content = node.ToContentDictionary();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Umbraco.Cms.Integrations.Automation.Zapier.Models;
55
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
66

7-
87
#if NETCOREAPP
98
using Microsoft.AspNetCore.Mvc;
109
using Microsoft.Extensions.Options;
@@ -20,7 +19,7 @@
2019
namespace Umbraco.Cms.Integrations.Automation.Zapier.Controllers
2120
{
2221
/// <summary>
23-
/// When a Zapier user creates triggers using the Umbraco app from the Zapier App Directory, he needs to provide valid credentials for a backoffice account.
22+
/// When a Zapier user creates triggers using the Umbraco app from the Zapier App Directory, they need to provide valid credentials for a backoffice account.
2423
/// </summary>
2524
public class AuthController : UmbracoApiController
2625
{

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

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,41 @@
11
using System.Collections.Generic;
2-
32
using System.Linq;
43

5-
using Umbraco.Cms.Integrations.Automation.Zapier.Configuration;
64
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
75
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
86

97
#if NETCOREAPP
108
using Microsoft.Extensions.Options;
119

12-
using Umbraco.Cms.Web.Common.Controllers;
1310
using Umbraco.Cms.Core.Services;
11+
using Umbraco.Cms.Integrations.Automation.Zapier.Configuration;
1412
#else
15-
using System.Configuration;
16-
17-
using Umbraco.Web.WebApi;
1813
using Umbraco.Core.Services;
1914
#endif
2015

2116
namespace Umbraco.Cms.Integrations.Automation.Zapier.Controllers
2217
{
2318
/// <summary>
24-
/// When a Zapier user creates a new "New Content Published" trigger, the API is used to provide him with the list of content types for handling "Published" event.
19+
/// When a Zapier user creates a new "New Content Published" trigger, the API is used to provide the list of content types for handling "Published" event.
2520
/// </summary>
26-
public class ContentController : UmbracoApiController
21+
public class ContentController : ZapierAuthorizedApiController
2722
{
28-
private readonly ZapierSettings Options;
29-
3023
private readonly IContentTypeService _contentTypeService;
3124

32-
private readonly IUserValidationService _userValidationService;
33-
3425
#if NETCOREAPP
3526
public ContentController(IOptions<ZapierSettings> options, IContentTypeService contentTypeService, IUserValidationService userValidationService)
27+
: base(options, userValidationService)
3628
#else
3729
public ContentController(IContentTypeService contentTypeService, IUserValidationService userValidationService)
30+
: base(userValidationService)
3831
#endif
3932
{
40-
#if NETCOREAPP
41-
Options = options.Value;
42-
#else
43-
Options = new ZapierSettings(ConfigurationManager.AppSettings);
44-
#endif
45-
4633
_contentTypeService = contentTypeService;
47-
48-
_userValidationService = userValidationService;
4934
}
5035

5136
public IEnumerable<ContentTypeDto> GetContentTypes()
5237
{
53-
string username = string.Empty;
54-
string password = string.Empty;
55-
56-
#if NETCOREAPP
57-
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.UsernameHeaderKey,
58-
out var usernameValues))
59-
username = usernameValues.First();
60-
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.PasswordHeaderKey,
61-
out var passwordValues))
62-
password = passwordValues.First();
63-
#else
64-
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.UsernameHeaderKey,
65-
out var usernameValues))
66-
username = usernameValues.First();
67-
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.PasswordHeaderKey,
68-
out var passwordValues))
69-
password = passwordValues.First();
70-
#endif
71-
72-
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return Enumerable.Empty<ContentTypeDto>();
73-
74-
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter().GetResult();
75-
if (!isAuthorized) return Enumerable.Empty<ContentTypeDto>();
38+
if (!IsUserValid()) return null;
7639

7740
var contentTypes = _contentTypeService.GetAll();
7841

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

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,52 @@
22
using System.Collections.Generic;
33
using System.Linq;
44

5-
using Umbraco.Cms.Integrations.Automation.Zapier.Configuration;
65
using Umbraco.Cms.Integrations.Automation.Zapier.Extensions;
76
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
87
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
98

109
#if NETCOREAPP
1110
using Microsoft.Extensions.Options;
1211

13-
using Umbraco.Cms.Core.Web;
14-
using Umbraco.Cms.Web.Common.Controllers;
1512
using Umbraco.Cms.Core.Services;
13+
using Umbraco.Cms.Integrations.Automation.Zapier.Configuration;
1614
#else
17-
using System.Configuration;
18-
19-
using Umbraco.Web;
20-
using Umbraco.Web.WebApi;
2115
using Umbraco.Core.Services;
2216
#endif
2317

2418
namespace Umbraco.Cms.Integrations.Automation.Zapier.Controllers
2519
{
2620
/// <summary>
27-
/// When a Zapier user creates a "New Content Published" triggered, he is authenticated, then selects a content type, the API provides an output json with the
21+
/// When a Zapier user creates a "New Content Published" triggered, they are authenticated, then select a content type, the API provides an output json with the
2822
/// structure of a content node matching the selected content type.
2923
/// For version 1.0.0 of the Umbraco Zapier App, the GetSampleContent will be used.
3024
/// </summary>
31-
public class PollingController : UmbracoApiController
25+
public class PollingController : ZapierAuthorizedApiController
3226
{
33-
private IUmbracoContextFactory _umbracoContextFactory;
34-
3527
private IContentService _contentService;
3628

37-
private readonly ZapierSettings Options;
38-
39-
private readonly IUserValidationService _userValidationService;
29+
private IContentTypeService _contentTypeService;
4030

4131
#if NETCOREAPP
42-
public PollingController(IOptions<ZapierSettings> options, IUmbracoContextFactory umbracoContextFactory, IContentService contentService, IUserValidationService userValidationService)
32+
public PollingController(IOptions<ZapierSettings> options, IContentService contentService, IContentTypeService contentTypeService, IUserValidationService userValidationService)
33+
: base(options, userValidationService)
4334
#else
44-
public PollingController(IUmbracoContextFactory umbracoContextFactory, IContentService contentService, IUserValidationService userValidationService)
35+
public PollingController(IContentService contentService, IContentTypeService contentTypeService, IUserValidationService userValidationService)
36+
: base(userValidationService)
4537
#endif
4638
{
47-
#if NETCOREAPP
48-
Options = options.Value;
49-
#else
50-
Options = new ZapierSettings(ConfigurationManager.AppSettings);
51-
#endif
52-
53-
_umbracoContextFactory = umbracoContextFactory;
54-
5539
_contentService = contentService;
5640

57-
_userValidationService = userValidationService;
41+
_contentTypeService = contentTypeService;
5842
}
5943

6044
[Obsolete("Used only for Umbraco Zapier app v1.0.0. For updated versions use GetContentByType")]
6145
public IEnumerable<PublishedContentDto> GetSampleContent()
6246
{
63-
string username = string.Empty;
64-
string password = string.Empty;
65-
66-
#if NETCOREAPP
67-
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.UsernameHeaderKey,
68-
out var usernameValues))
69-
username = usernameValues.First();
70-
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.PasswordHeaderKey,
71-
out var passwordValues))
72-
password = passwordValues.First();
73-
#else
74-
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.UsernameHeaderKey,
75-
out var usernameValues))
76-
username = usernameValues.First();
77-
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.PasswordHeaderKey,
78-
out var passwordValues))
79-
password = passwordValues.First();
80-
#endif
81-
82-
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return null;
83-
84-
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter()
85-
.GetResult();
86-
if (!isAuthorized) return null;
47+
if (!IsUserValid()) return null;
8748

8849
var rootNodes = _contentService.GetRootContent().Where(p => p.Published)
89-
.OrderByDescending(p => p.PublishDate);
50+
.OrderByDescending(p => p.PublishDate);
9051

9152
return rootNodes.Select(p => new PublishedContentDto
9253
{
@@ -98,23 +59,13 @@ public IEnumerable<PublishedContentDto> GetSampleContent()
9859

9960
public List<Dictionary<string, string>> GetContentByType(string alias)
10061
{
101-
var list = new List<Dictionary<string, string>>();
102-
103-
using (var cref = _umbracoContextFactory.EnsureUmbracoContext())
104-
{
105-
var cache = cref.UmbracoContext.Content;
106-
107-
var node = cache.GetByXPath($"//{alias}")
108-
.Where(p => p.IsPublished())
109-
.OrderByDescending(p => p.UpdateDate)
110-
.FirstOrDefault();
62+
if (!IsUserValid()) return null;
11163

112-
if (node == null) return list;
64+
var contentType = _contentTypeService.Get(alias);
11365

114-
list.Add(node.ToContentDictionary());
115-
116-
return list;
117-
}
66+
return new List<Dictionary<string, string>> { contentType.ToContentTypeDictionary() };
11867
}
68+
69+
11970
}
12071
}

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

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,49 @@
1-
using System.Linq;
2-
using Umbraco.Cms.Integrations.Automation.Zapier.Configuration;
3-
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
1+
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
42
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
53

64
#if NETCOREAPP
75
using Microsoft.AspNetCore.Mvc;
86
using Microsoft.Extensions.Options;
9-
using Umbraco.Cms.Web.Common.Controllers;
7+
8+
using Umbraco.Cms.Integrations.Automation.Zapier.Configuration;
109
#else
1110
using System.Web.Http;
12-
using System.Configuration;
13-
using Umbraco.Web.WebApi;
1411
#endif
1512

1613
namespace Umbraco.Cms.Integrations.Automation.Zapier.Controllers
1714
{
1815
/// <summary>
1916
/// Subscription API handling the ON/OFF trigger events in Zapier.
2017
/// </summary>
21-
public class SubscriptionController : UmbracoApiController
18+
public class SubscriptionController : ZapierAuthorizedApiController
2219
{
23-
private readonly ZapierSettings Options;
24-
2520
private readonly ZapierSubscriptionHookService _zapierSubscriptionHookService;
2621

2722
private readonly ZapierFormSubscriptionHookService _zapierFormSubscriptionHookService;
2823

29-
private readonly IUserValidationService _userValidationService;
30-
3124
#if NETCOREAPP
3225
public SubscriptionController(IOptions<ZapierSettings> options,
3326
ZapierSubscriptionHookService zapierSubscriptionHookService,
3427
ZapierFormSubscriptionHookService zapierFormSubscriptionHookService,
3528
IUserValidationService userValidationService)
29+
: base(options, userValidationService)
3630
#else
3731
public SubscriptionController(
3832
ZapierSubscriptionHookService zapierSubscriptionHookService,
3933
ZapierFormSubscriptionHookService zapierFormSubscriptionHookService,
4034
IUserValidationService userValidationService)
35+
: base(userValidationService)
4136
#endif
4237
{
43-
#if NETCOREAPP
44-
Options = options.Value;
45-
#else
46-
Options = new ZapierSettings(ConfigurationManager.AppSettings);
47-
#endif
48-
4938
_zapierSubscriptionHookService = zapierSubscriptionHookService;
5039

5140
_zapierFormSubscriptionHookService = zapierFormSubscriptionHookService;
52-
53-
_userValidationService = userValidationService;
5441
}
5542

5643
[HttpPost]
5744
public bool UpdatePreferences([FromBody] SubscriptionDto dto)
5845
{
59-
string username = string.Empty;
60-
string password = string.Empty;
61-
62-
#if NETCOREAPP
63-
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.UsernameHeaderKey,
64-
out var usernameValues))
65-
username = usernameValues.First();
66-
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.PasswordHeaderKey,
67-
out var passwordValues))
68-
password = passwordValues.First();
69-
#else
70-
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.UsernameHeaderKey,
71-
out var usernameValues))
72-
username = usernameValues.First();
73-
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.PasswordHeaderKey,
74-
out var passwordValues))
75-
password = passwordValues.First();
76-
#endif
77-
78-
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return false;
79-
80-
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter().GetResult();
81-
if (!isAuthorized) return false;
82-
83-
if (dto == null) return false;
46+
if (!IsUserValid() || dto == null) return false;
8447

8548
var result = dto.SubscribeHook
8649
? _zapierSubscriptionHookService.Add(dto.ContentType, dto.HookUrl)
@@ -92,31 +55,7 @@ public bool UpdatePreferences([FromBody] SubscriptionDto dto)
9255
[HttpPost]
9356
public bool UpdateFormPreferences([FromBody] FormSubscriptionDto dto)
9457
{
95-
string username = string.Empty;
96-
string password = string.Empty;
97-
98-
#if NETCOREAPP
99-
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.UsernameHeaderKey,
100-
out var usernameValues))
101-
username = usernameValues.First();
102-
if (Request.Headers.TryGetValue(Constants.ZapierAppConfiguration.PasswordHeaderKey,
103-
out var passwordValues))
104-
password = passwordValues.First();
105-
#else
106-
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.UsernameHeaderKey,
107-
out var usernameValues))
108-
username = usernameValues.First();
109-
if (Request.Headers.TryGetValues(Constants.ZapierAppConfiguration.PasswordHeaderKey,
110-
out var passwordValues))
111-
password = passwordValues.First();
112-
#endif
113-
114-
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return false;
115-
116-
var isAuthorized = _userValidationService.Validate(username, password, Options.UserGroup).GetAwaiter().GetResult();
117-
if (!isAuthorized) return false;
118-
119-
if (dto == null) return false;
58+
if (!IsUserValid() || dto == null) return false;
12059

12160
var result = dto.SubscribeHook
12261
? _zapierFormSubscriptionHookService.Add(dto.FormName, dto.HookUrl)

0 commit comments

Comments
 (0)