diff --git a/AdminUI/LearningHub.Nhs.AdminUI/Services/UserService.cs b/AdminUI/LearningHub.Nhs.AdminUI/Services/UserService.cs index 5f329c61..815eb3c5 100644 --- a/AdminUI/LearningHub.Nhs.AdminUI/Services/UserService.cs +++ b/AdminUI/LearningHub.Nhs.AdminUI/Services/UserService.cs @@ -337,6 +337,7 @@ public async Task SendAdminPasswordResetEmail(int u public async Task ClearUserCachedPermissions(int userId) { await this.cacheService.RemoveAsync($"{userId}:AllRolesWithPermissions"); + await this.cacheService.RemoveAsync($"{userId}:DatabricksReporter"); await this.cacheService.RemoveAsync($"{userId}:UserHasPublishedResources"); return new LearningHubValidationResult(true); } diff --git a/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/DatabricksService.cs b/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/DatabricksService.cs index 472f4626..ad5baef8 100644 --- a/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/DatabricksService.cs +++ b/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/DatabricksService.cs @@ -67,47 +67,57 @@ public DatabricksService(IOptions databricksConfig,IOptions public async Task IsUserReporter(int userId) { + bool isReporter = false; string cacheKey = $"{userId}:{CacheKey}"; - var userReportPermission = await this.cachingService.GetAsync(cacheKey); - if (userReportPermission.ResponseEnum == CacheReadResponseEnum.Found) + try { - return userReportPermission.Item; - } - + var userReportPermission = await this.cachingService.GetAsync(cacheKey); + if (userReportPermission.ResponseEnum == CacheReadResponseEnum.Found) + { + return userReportPermission.Item; + } - DatabricksApiHttpClient databricksInstance = new DatabricksApiHttpClient(this.databricksConfig); - var sqlText = $"CALL {this.databricksConfig.Value.UserPermissionEndpoint}({userId});"; - const string requestUrl = "/api/2.0/sql/statements"; + DatabricksApiHttpClient databricksInstance = new DatabricksApiHttpClient(this.databricksConfig); - var requestPayload = new - { - warehouse_id = this.databricksConfig.Value.WarehouseId, - statement = sqlText, - wait_timeout = "30s", - on_wait_timeout = "CANCEL" - }; + var sqlText = $"CALL {this.databricksConfig.Value.UserPermissionEndpoint}({userId});"; + const string requestUrl = "/api/2.0/sql/statements"; - var jsonBody = JsonConvert.SerializeObject(requestPayload); - using var content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); + var requestPayload = new + { + warehouse_id = this.databricksConfig.Value.WarehouseId, + statement = sqlText, + wait_timeout = "30s", + on_wait_timeout = "CANCEL" + }; - var response = await databricksInstance.GetClient().PostAsync(requestUrl, content); + var jsonBody = JsonConvert.SerializeObject(requestPayload); + using var content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); - var databricksResponse = await databricksInstance.GetClient().PostAsync(requestUrl, content); - if (databricksResponse.StatusCode is not HttpStatusCode.OK) - { - //log failure - return false; - } - var responseResult = await databricksResponse.Content.ReadAsStringAsync(); + var response = await databricksInstance.GetClient().PostAsync(requestUrl, content); - responseResult = responseResult.Trim(); - var root = JsonDocument.Parse(responseResult).RootElement; - string data = root.GetProperty("result").GetProperty("data_array")[0][0].GetString(); - bool isReporter = data == "1"; + var databricksResponse = await databricksInstance.GetClient().PostAsync(requestUrl, content); + if (databricksResponse.StatusCode is not HttpStatusCode.OK) + { + //log failure + return false; + } + var responseResult = await databricksResponse.Content.ReadAsStringAsync(); + + responseResult = responseResult.Trim(); + var root = JsonDocument.Parse(responseResult).RootElement; + string data = root.GetProperty("result").GetProperty("data_array")[0][0].GetString(); + isReporter = data == "1"; + + await this.cachingService.SetAsync(cacheKey, isReporter); + return isReporter; - await this.cachingService.SetAsync(cacheKey, isReporter); - return isReporter; + } + catch + { + await this.cachingService.SetAsync(cacheKey, isReporter); + return isReporter; + } } /// diff --git a/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/NavigationPermissionService.cs b/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/NavigationPermissionService.cs index 9e28c504..c8f3914a 100644 --- a/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/NavigationPermissionService.cs +++ b/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/NavigationPermissionService.cs @@ -48,7 +48,7 @@ public async Task GetNavigationModelAsync(IPrincipal user, bool } else if (user.IsInRole("Administrator")) { - return AuthenticatedAdministrator(controllerName); + return await AuthenticatedAdministrator(controllerName, currentUserId); } else if (user.IsInRole("ReadOnly")) { @@ -97,8 +97,9 @@ public NavigationModel NotAuthenticated() /// The AuthenticatedAdministrator. /// /// The controller name. + /// userId. /// The . - private NavigationModel AuthenticatedAdministrator(string controllerName) + private async Task AuthenticatedAdministrator(string controllerName, int userId) { return new NavigationModel() { @@ -115,7 +116,7 @@ private NavigationModel AuthenticatedAdministrator(string controllerName) ShowSignOut = true, ShowMyAccount = true, ShowBrowseCatalogues = true, - ShowReports = true, + ShowReports = await this.databricksService.IsUserReporter(userId), }; }