|
1 |
| -using System.Configuration; |
2 |
| -using System.Linq; |
3 |
| -using System.Web.Http; |
| 1 | +using System.Linq; |
| 2 | +using System.Threading.Tasks; |
4 | 3 |
|
| 4 | +using Umbraco.Cms.Integrations.Automation.Zapier.Configuration; |
5 | 5 | using Umbraco.Cms.Integrations.Automation.Zapier.Models;
|
6 |
| -using Umbraco.Core.Services; |
| 6 | + |
| 7 | + |
| 8 | +#if NETCOREAPP |
| 9 | +using Microsoft.AspNetCore.Mvc; |
| 10 | +using Microsoft.Extensions.Options; |
| 11 | +using Umbraco.Cms.Web.Common.Controllers; |
| 12 | +using Umbraco.Cms.Core.Security; |
| 13 | +using Umbraco.Cms.Core.Services; |
| 14 | +#else |
| 15 | +using System.Web.Http; |
| 16 | +using System.Configuration; |
| 17 | + |
7 | 18 | using Umbraco.Web.WebApi;
|
| 19 | +using Umbraco.Core.Services; |
| 20 | +#endif |
8 | 21 |
|
9 | 22 | namespace Umbraco.Cms.Integrations.Automation.Zapier.Controllers
|
10 | 23 | {
|
11 | 24 | public class AuthController : UmbracoApiController
|
12 | 25 | {
|
13 |
| - private const string UmbracoCmsIntegrationsAutomationZapierUserGroup = "Umbraco.Cms.Integrations.Automation.Zapier.UserGroup"; |
| 26 | + private readonly ZapierSettings Options; |
| 27 | + |
| 28 | + private readonly IUserService _userService; |
| 29 | + |
| 30 | +#if NETCOREAPP |
| 31 | + private readonly IBackOfficeUserManager _backOfficeUserManager; |
| 32 | + |
| 33 | + public AuthController(IBackOfficeUserManager backOfficeUserManager, IUserService userService, IOptions<ZapierSettings> options) |
| 34 | + { |
| 35 | + _backOfficeUserManager = backOfficeUserManager; |
| 36 | + |
| 37 | + _userService = userService; |
| 38 | + |
| 39 | + Options = options.Value; |
| 40 | + } |
| 41 | +#else |
| 42 | + public AuthController(IUserService userService) |
| 43 | + { |
| 44 | + Options = new ZapierSettings(ConfigurationManager.AppSettings); |
| 45 | + |
| 46 | + _userService = userService; |
| 47 | + } |
| 48 | +#endif |
14 | 49 |
|
15 | 50 | [HttpPost]
|
16 |
| - public bool ValidateUser([FromBody] UserModel userModel) |
| 51 | + public async Task<bool> ValidateUser([FromBody] UserModel userModel) |
17 | 52 | {
|
| 53 | +#if NETCOREAPP |
| 54 | + var isUserValid = |
| 55 | + await _backOfficeUserManager.ValidateCredentialsAsync(userModel.Username, userModel.Password); |
| 56 | +#else |
18 | 57 | var isUserValid = Security.ValidateBackOfficeCredentials(userModel.Username, userModel.Password);
|
| 58 | +#endif |
| 59 | + |
19 | 60 | if (!isUserValid) return false;
|
20 | 61 |
|
21 |
| - var userGroup = ConfigurationManager.AppSettings[UmbracoCmsIntegrationsAutomationZapierUserGroup]; |
| 62 | + var userGroup = Options.UserGroup; |
22 | 63 | if (!string.IsNullOrEmpty(userGroup))
|
23 | 64 | {
|
24 |
| - IUserService userService = Services.UserService; |
25 |
| - |
26 |
| - var user = userService.GetByUsername(userModel.Username); |
27 |
| - |
28 |
| - var isValid = user != null && user.Groups.Any(p => p.Name == userGroup); |
| 65 | + var user = _userService.GetByUsername(userModel.Username); |
29 | 66 |
|
30 |
| - return isValid; |
| 67 | + return user != null && user.Groups.Any(p => p.Name == userGroup); |
31 | 68 | }
|
32 | 69 |
|
33 | 70 | return true;
|
|
0 commit comments