Skip to content

Commit cddea53

Browse files
committed
V14: Integrations (Dynamics)
- Refractor authorization check
1 parent bd7581a commit cddea53

File tree

6 files changed

+71
-59
lines changed

6 files changed

+71
-59
lines changed

src/Umbraco.Cms.Integrations.Crm.Dynamics/Api/Management/Controllers/CheckOAuthConfigurationController.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ public CheckOAuthConfigurationController(IOptions<DynamicsSettings> options, Dyn
1919
}
2020

2121
[HttpGet("oauth-configuration")]
22-
[ProducesResponseType(typeof(OAuthConfigurationDto), StatusCodes.Status200OK)]
23-
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
22+
[ProducesResponseType(typeof(OAuthConfigurationDto), StatusCodes.Status200OK)]
2423
public async Task<IActionResult> CheckOAuthConfiguration()
2524
{
2625
var oauthConfiguration = DynamicsConfigurationService.GetOAuthConfiguration();
2726

28-
if (oauthConfiguration == null) return Unauthorized(new OAuthConfigurationDto { Message = string.Empty });
27+
if (oauthConfiguration == null) return Ok(new OAuthConfigurationDto { Message = string.Empty });
2928

3029
var identity = await DynamicsService.GetIdentity(oauthConfiguration.AccessToken);
3130

32-
if (!identity.IsAuthorized) return Unauthorized(new OAuthConfigurationDto { Message = identity.Error != null ? identity.Error.Message : string.Empty });
31+
if (!identity.IsAuthorized) return Ok(new OAuthConfigurationDto { Message = identity.Error != null ? identity.Error.Message : string.Empty });
3332

3433
oauthConfiguration.IsAuthorized = true;
3534

src/Umbraco.Cms.Integrations.Crm.Dynamics/Api/Management/Controllers/GetAuthorizationUrlController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public GetAuthorizationUrlController(IOptions<DynamicsSettings> options, Dynamic
1919

2020
[HttpGet("authorization-url")]
2121
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
22-
public IActionResult GetAuthorizationUrl() => Ok(AuthorizationService.GetAuthorizationUrl());
22+
public IActionResult GetAuthorizationUrl()
23+
{
24+
var url = AuthorizationService.GetAuthorizationUrl();
25+
return Ok(url);
26+
}
2327
}
2428
}

src/Umbraco.Cms.Integrations.Crm.Dynamics/Client/src/config/authorization/authorization-property-editor.element.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@ const elementName = "dynamics-authorization";
1010
@customElement(elementName)
1111
export class DynamicsAuthorizationElement extends UmbElementMixin(LitElement){
1212
#dynamicsContext!: typeof DYNAMICS_CONTEXT_TOKEN.TYPE;
13+
#settingsModel?: OAuthConfigurationDtoModel;
1314

1415
@state()
1516
private _oauthSetup: DynamicsOAuthSetup = {
1617
isConnected: false,
17-
isAccessTokenExpired: false,
18+
isAccessTokenExpired: true,
1819
isAccessTokenValid: false
1920
};
2021

21-
@state()
22-
private _oAuthConfig: OAuthConfigurationDtoModel | undefined;
23-
2422
constructor() {
2523
super();
2624

2725
this.consumeContext(DYNAMICS_CONTEXT_TOKEN, (context) => {
2826
if (!context) return;
2927
this.#dynamicsContext = context;
28+
this.observe(context.settingsModel, (settingsModel) => {
29+
this.#settingsModel = settingsModel;
30+
});
3031
});
3132
}
3233

@@ -37,56 +38,55 @@ export class DynamicsAuthorizationElement extends UmbElementMixin(LitElement){
3738
}
3839

3940
async #checkOAuthConfiguration(){
40-
const { data } = await this.#dynamicsContext.checkOauthConfiguration();
41-
if (!data) {
42-
this._oauthSetup = {
43-
isConnected: false,
44-
isAccessTokenExpired: true,
45-
isAccessTokenValid: false
46-
}
41+
//const { data } = await this.#dynamicsContext.checkOauthConfiguration();
42+
if (!this.#settingsModel) {
43+
return;
44+
}
45+
46+
if (!this.#settingsModel.isAuthorized) {
47+
this._showError("Unable to connect to Dynamics. Please review the settings of the form picker property's data type.")
4748
} else {
48-
if (!data.isAuthorized) {
49-
this._showError("Unable to connect to Dynamics. Please review the settings of the form picker property's data type.")
50-
} else {
51-
this._oauthSetup = {
52-
isConnected: true,
53-
isAccessTokenExpired: false,
54-
isAccessTokenValid: true
55-
}
56-
57-
this._oAuthConfig = data;
49+
this._oauthSetup = {
50+
isConnected: true,
51+
isAccessTokenExpired: false,
52+
isAccessTokenValid: true
5853
}
5954
}
6055
}
6156

6257
async #connectButtonClick(){
58+
const { data } = await this.#dynamicsContext.getAuthorizationUrl();
59+
if (!data) return;
60+
61+
window.open(data, "Authorize", "width=900,height=700,modal=yes,alwaysRaised=yes");
62+
6363
window.addEventListener("message", async (event: MessageEvent) => {
6464
if (event.data.type === "dynamics:oauth:success") {
6565

6666
const oauthRequestDtoModel: OAuthRequestDtoModel = {
6767
code: event.data.code
6868
};
6969

70-
const { data } = await this.#dynamicsContext.getAccessToken(oauthRequestDtoModel);
71-
if (!data) return;
72-
73-
if (data.startsWith("Error:")) {
74-
this._showError(data);
75-
} else {
76-
this._oauthSetup = {
77-
isConnected: true
78-
};
79-
this._showSuccess("OAuth Connected");
80-
81-
}
82-
70+
await this.getAccessToken(oauthRequestDtoModel);
8371
}
8472
}, false);
73+
}
8574

86-
const { data } = await this.#dynamicsContext.getAuthorizationUrl();
75+
async getAccessToken(oauthRequestDtoModel: OAuthRequestDtoModel){
76+
const { data } = await this.#dynamicsContext.getAccessToken(oauthRequestDtoModel);
8777
if (!data) return;
8878

89-
window.open(data, "Authorize", "width=900,height=700,modal=yes,alwaysRaised=yes");
79+
if (data.startsWith("Error:")) {
80+
this._showError(data);
81+
} else {
82+
this._oauthSetup = {
83+
isConnected: true
84+
};
85+
this._showSuccess("OAuth Connected");
86+
87+
this.dispatchEvent(new CustomEvent("connect"));
88+
}
89+
9090
}
9191

9292
async #revokeButtonClick(){
@@ -121,7 +121,7 @@ export class DynamicsAuthorizationElement extends UmbElementMixin(LitElement){
121121
${this._oauthSetup.isConnected ?
122122
html`
123123
<span>
124-
<b>Connected</b>: ${this._oAuthConfig?.fullName}
124+
<b>Connected</b>: ${this.#settingsModel?.fullName}
125125
</span>
126126
` :
127127
html`

src/Umbraco.Cms.Integrations.Crm.Dynamics/Client/src/context/dynamics.context.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { UmbControllerBase } from "@umbraco-cms/backoffice/class-api";
22
import { UmbContextToken } from "@umbraco-cms/backoffice/context-api";
33
import type { UmbControllerHost } from "@umbraco-cms/backoffice/controller-api";
44
import { DynamicsRepository } from "../repository/dynamics.repository";
5-
import { OAuthRequestDtoModel } from "@umbraco-integrations/dynamics/generated";
5+
import { OAuthConfigurationDtoModel, OAuthRequestDtoModel } from "@umbraco-integrations/dynamics/generated";
6+
import { UmbObjectState } from "@umbraco-cms/backoffice/observable-api";
67

78
export class DynamicsContext extends UmbControllerBase{
89
#repository: DynamicsRepository;
10+
#settingsModel = new UmbObjectState<OAuthConfigurationDtoModel | undefined>(undefined);
11+
settingsModel = this.#settingsModel.asObservable();
912

1013
constructor(host: UmbControllerHost){
1114
super(host);
@@ -16,6 +19,7 @@ export class DynamicsContext extends UmbControllerBase{
1619

1720
async hostConnected() {
1821
super.hostConnected();
22+
await this.checkOauthConfiguration();
1923
}
2024

2125
async getForms(module: string){
@@ -31,7 +35,8 @@ export class DynamicsContext extends UmbControllerBase{
3135
}
3236

3337
async checkOauthConfiguration(){
34-
return await this.#repository.checkOauthConfiguration();
38+
const { data } = await this.#repository.checkOauthConfiguration();
39+
this.#settingsModel.setValue(data);
3540
}
3641

3742
async getAccessToken(oAuthRequestDtoModel: OAuthRequestDtoModel){

src/Umbraco.Cms.Integrations.Crm.Dynamics/Client/src/modal/dynamics-form-modal.element.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const elementName = "dynamics-forms-modal";
1111
@customElement(elementName)
1212
export default class DynamicsFormModalElement extends UmbModalBaseElement<DynamicsFormPickerModalData, DynamicsFormPickerModalValue>{
1313
#dynamicsContext!: typeof DYNAMICS_CONTEXT_TOKEN.TYPE;
14-
14+
#settingsModel?: OAuthConfigurationDtoModel;
15+
1516
@state()
1617
private _loading = false;
1718
@state()
@@ -23,6 +24,9 @@ export default class DynamicsFormModalElement extends UmbModalBaseElement<Dynami
2324
this.consumeContext(DYNAMICS_CONTEXT_TOKEN, (context) => {
2425
if (!context) return;
2526
this.#dynamicsContext = context;
27+
this.observe(context.settingsModel, (settingsModel) => {
28+
this.#settingsModel = settingsModel;
29+
});
2630
});
2731
}
2832

@@ -32,16 +36,16 @@ export default class DynamicsFormModalElement extends UmbModalBaseElement<Dynami
3236
}
3337

3438
async #checkOAuthConfiguration(){
35-
const { data } = await this.#dynamicsContext.checkOauthConfiguration();
36-
if (!data) {
39+
40+
if (!this.#settingsModel) {
41+
return;
42+
}
3743

44+
if (!this.#settingsModel.isAuthorized) {
45+
this._showError("Unable to connect to Dynamics. Please review the settings of the form picker property's data type.")
3846
} else {
39-
if (!data.isAuthorized) {
40-
this._showError("Unable to connect to Dynamics. Please review the settings of the form picker property's data type.")
41-
} else {
4247

43-
await this.#getForms();
44-
}
48+
await this.#getForms();
4549
}
4650
}
4751

src/Umbraco.Cms.Integrations.Crm.Dynamics/Client/src/property-editor/dynamics-form-picker-property-editor.element.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
33
import { UmbPropertyEditorUiElement } from '@umbraco-cms/backoffice/extension-registry';
44
import { DYNAMICS_CONTEXT_TOKEN } from "../context/dynamics.context";
55
import { UMB_MODAL_MANAGER_CONTEXT } from "@umbraco-cms/backoffice/modal";
6-
import { FormDtoModel } from "@umbraco-integrations/dynamics/generated";
6+
import { FormDtoModel, OAuthConfigurationDtoModel } from "@umbraco-integrations/dynamics/generated";
77
import { DynamicsFormPickerConfiguration } from "../types/types";
88
import { UmbPropertyEditorConfigCollection } from "@umbraco-cms/backoffice/property-editor";
99
import { DYNAMICS_MODAL_TOKEN } from "../modal/dynamics.modal-token";
@@ -14,6 +14,7 @@ const elementName = "dynamics-form-picker";
1414
@customElement(elementName)
1515
export class DynamicsFormPickerPropertyEditor extends UmbLitElement implements UmbPropertyEditorUiElement {
1616
#dynamicsContext!: typeof DYNAMICS_CONTEXT_TOKEN.TYPE;
17+
#settingsModel?: OAuthConfigurationDtoModel;
1718
#modalManagerContext?: typeof UMB_MODAL_MANAGER_CONTEXT.TYPE;
1819
@state()
1920
private _config?: DynamicsFormPickerConfiguration;
@@ -45,9 +46,9 @@ export class DynamicsFormPickerPropertyEditor extends UmbLitElement implements U
4546
this.consumeContext(DYNAMICS_CONTEXT_TOKEN, (context) => {
4647
if (!context) return;
4748
this.#dynamicsContext = context;
48-
// this.observe(context.settingsModel, (settingsModel) => {
49-
// this.#settingsModel = settingsModel;
50-
// });
49+
this.observe(context.settingsModel, (settingsModel) => {
50+
this.#settingsModel = settingsModel;
51+
});
5152
});
5253
}
5354

@@ -60,9 +61,8 @@ export class DynamicsFormPickerPropertyEditor extends UmbLitElement implements U
6061
}
6162

6263
async #checkOAuthConfiguration(){
63-
const {data} = await this.#dynamicsContext.checkOauthConfiguration();
64-
if(!data) return;
65-
if(!data.isAuthorized) this._showError("Unable to connect to Dynamics. Please review the settings of the form picker property's data type.");
64+
if(!this.#settingsModel) return;
65+
if(!this.#settingsModel.isAuthorized) this._showError("Unable to connect to Dynamics. Please review the settings of the form picker property's data type.");
6666

6767
await this.#getForm();
6868
}

0 commit comments

Comments
 (0)