Skip to content

Commit 1649450

Browse files
Merge pull request #1581 from TechnologyEnhancedLearning/Develop/Fixes/TD-6100-refactor-latest
Report Permission Cache update
2 parents 747c451 + 57a0314 commit 1649450

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

AdminUI/LearningHub.Nhs.AdminUI/Services/UserService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ public async Task<LearningHubValidationResult> SendAdminPasswordResetEmail(int u
337337
public async Task<LearningHubValidationResult> ClearUserCachedPermissions(int userId)
338338
{
339339
await this.cacheService.RemoveAsync($"{userId}:AllRolesWithPermissions");
340+
await this.cacheService.RemoveAsync($"{userId}:DatabricksReporter");
340341
await this.cacheService.RemoveAsync($"{userId}:UserHasPublishedResources");
341342
return new LearningHubValidationResult(true);
342343
}

OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/DatabricksService.cs

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,47 +67,57 @@ public DatabricksService(IOptions<DatabricksConfig> databricksConfig,IOptions<Le
6767
/// <inheritdoc/>
6868
public async Task<bool> IsUserReporter(int userId)
6969
{
70+
bool isReporter = false;
7071
string cacheKey = $"{userId}:{CacheKey}";
71-
var userReportPermission = await this.cachingService.GetAsync<bool>(cacheKey);
72-
if (userReportPermission.ResponseEnum == CacheReadResponseEnum.Found)
72+
try
7373
{
74-
return userReportPermission.Item;
75-
}
76-
74+
var userReportPermission = await this.cachingService.GetAsync<bool>(cacheKey);
75+
if (userReportPermission.ResponseEnum == CacheReadResponseEnum.Found)
76+
{
77+
return userReportPermission.Item;
78+
}
7779

78-
DatabricksApiHttpClient databricksInstance = new DatabricksApiHttpClient(this.databricksConfig);
7980

80-
var sqlText = $"CALL {this.databricksConfig.Value.UserPermissionEndpoint}({userId});";
81-
const string requestUrl = "/api/2.0/sql/statements";
81+
DatabricksApiHttpClient databricksInstance = new DatabricksApiHttpClient(this.databricksConfig);
8282

83-
var requestPayload = new
84-
{
85-
warehouse_id = this.databricksConfig.Value.WarehouseId,
86-
statement = sqlText,
87-
wait_timeout = "30s",
88-
on_wait_timeout = "CANCEL"
89-
};
83+
var sqlText = $"CALL {this.databricksConfig.Value.UserPermissionEndpoint}({userId});";
84+
const string requestUrl = "/api/2.0/sql/statements";
9085

91-
var jsonBody = JsonConvert.SerializeObject(requestPayload);
92-
using var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
86+
var requestPayload = new
87+
{
88+
warehouse_id = this.databricksConfig.Value.WarehouseId,
89+
statement = sqlText,
90+
wait_timeout = "30s",
91+
on_wait_timeout = "CANCEL"
92+
};
9393

94-
var response = await databricksInstance.GetClient().PostAsync(requestUrl, content);
94+
var jsonBody = JsonConvert.SerializeObject(requestPayload);
95+
using var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
9596

96-
var databricksResponse = await databricksInstance.GetClient().PostAsync(requestUrl, content);
97-
if (databricksResponse.StatusCode is not HttpStatusCode.OK)
98-
{
99-
//log failure
100-
return false;
101-
}
102-
var responseResult = await databricksResponse.Content.ReadAsStringAsync();
97+
var response = await databricksInstance.GetClient().PostAsync(requestUrl, content);
10398

104-
responseResult = responseResult.Trim();
105-
var root = JsonDocument.Parse(responseResult).RootElement;
106-
string data = root.GetProperty("result").GetProperty("data_array")[0][0].GetString();
107-
bool isReporter = data == "1";
99+
var databricksResponse = await databricksInstance.GetClient().PostAsync(requestUrl, content);
100+
if (databricksResponse.StatusCode is not HttpStatusCode.OK)
101+
{
102+
//log failure
103+
return false;
104+
}
105+
var responseResult = await databricksResponse.Content.ReadAsStringAsync();
106+
107+
responseResult = responseResult.Trim();
108+
var root = JsonDocument.Parse(responseResult).RootElement;
109+
string data = root.GetProperty("result").GetProperty("data_array")[0][0].GetString();
110+
isReporter = data == "1";
111+
112+
await this.cachingService.SetAsync(cacheKey, isReporter);
113+
return isReporter;
108114

109-
await this.cachingService.SetAsync(cacheKey, isReporter);
110-
return isReporter;
115+
}
116+
catch
117+
{
118+
await this.cachingService.SetAsync(cacheKey, isReporter);
119+
return isReporter;
120+
}
111121
}
112122

113123
/// <inheritdoc/>

OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/NavigationPermissionService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task<NavigationModel> GetNavigationModelAsync(IPrincipal user, bool
4848
}
4949
else if (user.IsInRole("Administrator"))
5050
{
51-
return AuthenticatedAdministrator(controllerName);
51+
return await AuthenticatedAdministrator(controllerName, currentUserId);
5252
}
5353
else if (user.IsInRole("ReadOnly"))
5454
{
@@ -97,8 +97,9 @@ public NavigationModel NotAuthenticated()
9797
/// The AuthenticatedAdministrator.
9898
/// </summary>
9999
/// <param name="controllerName">The controller name.</param>
100+
/// <param name="userId">userId.</param>
100101
/// <returns>The <see cref="NavigationModel"/>.</returns>
101-
private NavigationModel AuthenticatedAdministrator(string controllerName)
102+
private async Task<NavigationModel> AuthenticatedAdministrator(string controllerName, int userId)
102103
{
103104
return new NavigationModel()
104105
{
@@ -115,7 +116,7 @@ private NavigationModel AuthenticatedAdministrator(string controllerName)
115116
ShowSignOut = true,
116117
ShowMyAccount = true,
117118
ShowBrowseCatalogues = true,
118-
ShowReports = true,
119+
ShowReports = await this.databricksService.IsUserReporter(userId),
119120
};
120121
}
121122

0 commit comments

Comments
 (0)