Skip to content

Commit

Permalink
Cache information for OnAddCommonCustomDimensions telemetry subscriber (
Browse files Browse the repository at this point in the history
#567)

<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary <!-- Provide a general summary of your changes -->
`AzureADGraph.GetTenantDetail(TenantInfo);` is called everytime and the
information returned is the exact same.
Cache the information for each session that runs this.

#### Work Item(s) <!-- Add the issue number here after the #. The issue
needs to be open and approved. Submitting PRs with no linked issues or
unapproved issues is highly discouraged. -->
Fixes
[AB#499449](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/499449)
  • Loading branch information
darjoo authored Feb 16, 2024
1 parent 9bc7866 commit 9dfaf05
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 33 deletions.
4 changes: 4 additions & 0 deletions src/System Application/App/Azure AD User Management/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
{
"from": 9029,
"to": 9029
},
{
"from": 9034,
"to": 9034
}
],
"internalsVisibleTo": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ namespace System.Azure.Identity;
using System;
using System.Security.User;
using System.Environment;
using System.Telemetry;
using System.Globalization;
using System.Security.AccessControl;
using System.Environment.Configuration;

Expand Down Expand Up @@ -312,37 +310,6 @@ codeunit 9017 "Azure AD User Mgmt. Impl."
exit(AccessControl.Count() > TempAccessControlWithDefaultPermissions.Count());
end;

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Telemetry Custom Dimensions", OnAddCommonCustomDimensions, '', true, true)]
local procedure OnAddCommonCustomDimensions(var Sender: Codeunit "Telemetry Custom Dimensions")
var
Language: Codeunit Language;
PlanIds: Codeunit "Plan Ids";
UserAccountHelper: DotNet NavUserAccountHelper;
IsAdmin: Boolean;
CountryCode: Text;
begin
if not UserAccountHelper.IsAzure() then
exit;

// Add IsAdmin
IsAdmin := AzureADGraphUser.IsUserDelegatedAdmin() or AzureADPlan.IsPlanAssignedToUser(PlanIds.GetGlobalAdminPlanId()) or AzureADPlan.IsPlanAssignedToUser(PlanIds.GetD365AdminPlanId()) or AzureADPlan.IsPlanAssignedToUser(PlanIds.GetBCAdminPlanId());
Sender.AddCommonCustomDimension('IsAdmin', Language.ToDefaultLanguage(IsAdmin));

// Add CountryCode
if TryGetCountryCode(CountryCode) then
Sender.AddCommonCustomDimension('CountryCode', CountryCode);
end;

[TryFunction]
local procedure TryGetCountryCode(var CountryCode: Text)
var
TenantInfo: DotNet TenantInfo;
begin
AzureADGraph.GetTenantDetail(TenantInfo);
if not IsNull(TenantInfo) then
CountryCode := TenantInfo.CountryLetterCode();
end;

[InternalEvent(false)]
local procedure OnIsUserTenantAdmin(var IsUserTenantAdmin: Boolean; var Handled: Boolean)
begin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace System.Azure.Identity;

using System;
using System.Globalization;
using System.Telemetry;

codeunit 9034 "Azure AD User Subscribers"
{
Access = Internal;
InherentEntitlements = X;
InherentPermissions = X;
SingleInstance = true;

var
AzureADGraphUser: Codeunit "Azure AD Graph User";
AzureADGraph: Codeunit "Azure AD Graph";
AzureADPlan: Codeunit "Azure AD Plan";
Initialized, IsAzure, IsAdmin : Boolean;
CountryCode: Text;

local procedure GetInformation()
var
PlanIds: Codeunit "Plan Ids";
UserAccountHelper: DotNet NavUserAccountHelper;
begin
if Initialized then
exit;
Initialized := true;

IsAzure := UserAccountHelper.IsAzure();
if not IsAzure then
exit;

IsAdmin := AzureADGraphUser.IsUserDelegatedAdmin() or AzureADPlan.IsPlanAssignedToUser(PlanIds.GetGlobalAdminPlanId()) or AzureADPlan.IsPlanAssignedToUser(PlanIds.GetD365AdminPlanId());
if TryGetCountryCode() then;
end;

[TryFunction]
local procedure TryGetCountryCode()
var
TenantInfo: DotNet TenantInfo;
begin
AzureADGraph.GetTenantDetail(TenantInfo);
if not IsNull(TenantInfo) then
CountryCode := TenantInfo.CountryLetterCode();
end;

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Telemetry Custom Dimensions", OnAddCommonCustomDimensions, '', true, true)]
local procedure OnAddCommonCustomDimensions(var Sender: Codeunit "Telemetry Custom Dimensions")
var
Language: Codeunit Language;
begin
GetInformation();

if not IsAzure then
exit;

// Add IsAdmin
Sender.AddCommonCustomDimension('IsAdmin', Language.ToDefaultLanguage(IsAdmin));

// Add CountryCode
if CountryCode <> '' then
Sender.AddCommonCustomDimension('CountryCode', CountryCode);
end;
}

0 comments on commit 9dfaf05

Please sign in to comment.