Skip to content

Commit a9fa9cc

Browse files
committed
Validate users outside organisation; HTTP revoke action update.
1 parent b185eac commit a9fa9cc

File tree

7 files changed

+40
-17
lines changed

7 files changed

+40
-17
lines changed

src/Umbraco.Cms.Integrations.Crm.Dynamics/App_Plugins/UmbracoCms.Integrations/Crm/Dynamics/js/configuration.controller.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
if (typeof $scope.connected === "function")
1313
$scope.connected();
14+
} else if (response.message.length > 0) {
15+
if (typeof $scope.connected === "undefined")
16+
notificationsService.error("Dynamics Configuration", response.message);
1417
}
1518
});
1619

@@ -41,7 +44,7 @@
4144
window.addEventListener("message", function (event) {
4245
if (event.data.type === "hubspot:oauth:success") {
4346
vm.oauthSuccessEventCount += 1;
44-
47+
4548
if (vm.oauthSuccessEventCount == 1) {
4649
umbracoCmsIntegrationsCrmDynamicsResource.getAccessToken(event.data.code).then(function (response) {
4750

src/Umbraco.Cms.Integrations.Crm.Dynamics/App_Plugins/UmbracoCms.Integrations/Crm/Dynamics/js/dynamics.resource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
revokeAccessToken: function () {
1818
return umbRequestHelper.resourcePromise(
19-
$http.post(`${apiEndpoint}/RevokeAccessToken`),
19+
$http.delete(`${apiEndpoint}/RevokeAccessToken`),
2020
"Failed");
2121
},
2222
getSystemUserFullName: function () {

src/Umbraco.Cms.Integrations.Crm.Dynamics/Controllers/FormsController.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-

2-
#if NETCOREAPP
1+
#if NETCOREAPP
32
using Microsoft.AspNetCore.Mvc;
43
using Microsoft.Extensions.Options;
4+
55
using Umbraco.Cms.Web.BackOffice.Controllers;
66
using Umbraco.Cms.Web.Common.Attributes;
77
#else
@@ -74,11 +74,11 @@ public async Task<OAuthConfigurationDto> CheckOAuthConfiguration()
7474
{
7575
var oauthConfiguration = _dynamicsConfigurationService.GetOAuthConfiguration();
7676

77-
if (oauthConfiguration == null) return new OAuthConfigurationDto();
77+
if (oauthConfiguration == null) return new OAuthConfigurationDto { Message = string.Empty };
7878

7979
var identity = await _dynamicsService.GetIdentity(oauthConfiguration.AccessToken);
8080

81-
if (!identity.IsAuthorized) return new OAuthConfigurationDto();
81+
if (!identity.IsAuthorized) return new OAuthConfigurationDto { Message = identity.Error?.Message };
8282

8383
oauthConfiguration.IsAuthorized = true;
8484

@@ -113,8 +113,10 @@ public async Task<string> GetAccessToken([FromBody] OAuthRequestDto authRequestD
113113

114114
var identity = await _dynamicsService.GetIdentity(tokenDto.AccessToken);
115115

116-
if (identity != null)
116+
if (identity.IsAuthorized)
117117
_dynamicsConfigurationService.AddorUpdateOAuthConfiguration(tokenDto.AccessToken, identity.UserId, identity.FullName);
118+
else
119+
return "Error: " + identity.Error.Message;
118120

119121
return result;
120122
}
@@ -158,7 +160,7 @@ public async Task<ResponseDto<FormDto>> GetForms()
158160
[HttpGet]
159161
public string GetSystemUserFullName() => _dynamicsConfigurationService.GetSystemUserFullName();
160162

161-
[HttpPost]
162-
public void RevokeAccessToken() => _dynamicsConfigurationService.Delete();
163+
[HttpDelete]
164+
public string RevokeAccessToken() => _dynamicsConfigurationService.Delete();
163165
}
164166
}

src/Umbraco.Cms.Integrations.Crm.Dynamics/Models/Dtos/ErrorDto.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class ErrorDto
77
[JsonProperty("status")]
88
public string Status { get; set; }
99

10+
[JsonProperty("code")]
11+
public string Code { get; set; }
12+
1013
[JsonProperty("message")]
1114
public string Message { get; set; }
1215

src/Umbraco.Cms.Integrations.Crm.Dynamics/Models/Dtos/IdentityDto.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ namespace Umbraco.Cms.Integrations.Crm.Dynamics.Models.Dtos
44
{
55
public class IdentityDto
66
{
7-
public bool IsAuthorized { get; set; } = true;
7+
public bool IsAuthorized { get; set; }
88

99
[JsonProperty("systemuserid")]
1010
public string UserId { get; set; }
1111

1212
[JsonProperty("fullname")]
1313
public string FullName { get; set; }
14+
15+
public ErrorDto Error { get; set; }
1416
}
1517
}

src/Umbraco.Cms.Integrations.Crm.Dynamics/Models/Dtos/OAuthConfigurationDto.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ public class OAuthConfigurationDto
1919

2020
[JsonProperty("isAuthorized")]
2121
public bool IsAuthorized { get; set; }
22+
23+
[JsonProperty("message")]
24+
public string Message { get; set; }
2225
}
2326
}

src/Umbraco.Cms.Integrations.Crm.Dynamics/Services/DynamicsService.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public DynamicsService(DynamicsConfigurationService dynamicsConfigurationService
4949

5050
public async Task<IdentityDto> GetIdentity(string accessToken)
5151
{
52-
var userId = await GetUserId(accessToken);
52+
var user = await GetUser(accessToken);
53+
54+
if (!user.IsAuthorized) return user;
5355

5456
var requestMessage = new HttpRequestMessage
5557
{
@@ -64,9 +66,10 @@ public async Task<IdentityDto> GetIdentity(string accessToken)
6466

6567
var result = await response.Content.ReadAsStringAsync();
6668

67-
var systemUsers = JsonConvert.DeserializeObject<ResponseDto<IdentityDto>>(result);
69+
var systemUser = JsonConvert.DeserializeObject<ResponseDto<IdentityDto>>(result).Value.FirstOrDefault(p => p.UserId == user.UserId.ToString());
70+
systemUser.IsAuthorized = true;
6871

69-
return systemUsers.Value.FirstOrDefault(p => p.UserId == userId.ToString());
72+
return systemUser;
7073
}
7174

7275
public async Task<string> GetEmbedCode(string formId)
@@ -92,7 +95,7 @@ public async Task<string> GetEmbedCode(string formId)
9295
return embedCode.Value.FirstOrDefault() != null ? embedCode.Value.First().EmbedCode : string.Empty;
9396
}
9497

95-
private async Task<string> GetUserId(string accessToken)
98+
private async Task<IdentityDto> GetUser(string accessToken)
9699
{
97100
var requestMessage = new HttpRequestMessage
98101
{
@@ -103,11 +106,18 @@ private async Task<string> GetUserId(string accessToken)
103106

104107
var response = await ClientFactory().SendAsync(requestMessage);
105108

106-
if(!response.IsSuccessStatusCode) return String.Empty;
107-
108109
var result = await response.Content.ReadAsStringAsync();
109110

110-
return JObject.Parse(result)["UserId"].ToString();
111+
if (!response.IsSuccessStatusCode)
112+
{
113+
return JsonConvert.DeserializeObject<IdentityDto>(result);
114+
}
115+
116+
return new IdentityDto
117+
{
118+
IsAuthorized = true,
119+
UserId = JObject.Parse(result)["UserId"].ToString()
120+
};
111121
}
112122
}
113123
}

0 commit comments

Comments
 (0)