diff --git a/roles/database/files/sql/creation/fworch-create-tables.sql b/roles/database/files/sql/creation/fworch-create-tables.sql index ff1afb38b..c2096cd86 100755 --- a/roles/database/files/sql/creation/fworch-create-tables.sql +++ b/roles/database/files/sql/creation/fworch-create-tables.sql @@ -919,14 +919,14 @@ Create table "report_template" "report_template_owner" Integer, --FK "filterline_history" Boolean Default TRUE, -- every time a filterline is sent, we save it for future usage (auto-deleted every 90 days) "report_parameters" json, - "viewable_by_all" boolean, -- share with all + "viewable_by_all" boolean Default FALSE, -- share with all primary key ("report_template_id") ); Create table "report_template_viewable_by" ( "report_template_id" integer not null, - "viewer_dn" varchar, -- share with a group / a specific user + "viewer_dn" varchar not null, -- share with a group / a specific user primary key ("report_template_id", "viewer_dn") ); diff --git a/roles/database/files/upgrade/7.1.0.sql b/roles/database/files/upgrade/7.1.0.sql index da28dd502..7c7d572ce 100644 --- a/roles/database/files/upgrade/7.1.0.sql +++ b/roles/database/files/upgrade/7.1.0.sql @@ -1 +1,16 @@ -DROP TABLE IF EXISTS "report_template_viewable_by_user"; +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'report_template_viewable_by_user' AND column_name = 'uiuser_id') THEN + DROP TABLE report_template_viewable_by_user; + END IF; +END $$; + +ALTER TABLE report_template +ADD COLUMN viewable_by_all boolean Default FALSE; + +Create table if not exists "report_template_viewable_by" +( + "report_template_id" integer not null, + "viewer_dn" varchar not null, -- share with a group / a specific user + primary key ("report_template_id", "viewer_dn") +); \ No newline at end of file diff --git a/roles/lib/files/FWO.Api.Client/Data/ReportTemplateViewer.cs b/roles/lib/files/FWO.Api.Client/Data/LdapUser.cs similarity index 64% rename from roles/lib/files/FWO.Api.Client/Data/ReportTemplateViewer.cs rename to roles/lib/files/FWO.Api.Client/Data/LdapUser.cs index 1f9870d5a..1d1cce268 100644 --- a/roles/lib/files/FWO.Api.Client/Data/ReportTemplateViewer.cs +++ b/roles/lib/files/FWO.Api.Client/Data/LdapUser.cs @@ -6,8 +6,8 @@ namespace FWO.Api.Data { - public class ReportTemplateViewer + public class LdapUser { - + public DistName Dn { get; set; } = new DistName(null); } } diff --git a/roles/lib/files/FWO.Api.Client/Data/ReportTemplate.cs b/roles/lib/files/FWO.Api.Client/Data/ReportTemplate.cs index eab15ae1a..e1d0b5730 100644 --- a/roles/lib/files/FWO.Api.Client/Data/ReportTemplate.cs +++ b/roles/lib/files/FWO.Api.Client/Data/ReportTemplate.cs @@ -27,7 +27,7 @@ public class ReportTemplate public ReportParams ReportParams { get; set; } = new ReportParams(); [JsonProperty("viewable_by"), JsonPropertyName("viewable_by")] - public ReportTemplateViewer[] Viewer { get; set; } = new ReportTemplateViewer[0]; + public DistName[] Viewer { get; set; } = new DistName[0]; public bool Detailed = false; diff --git a/roles/lib/files/FWO.Middleware.Client/MiddlewareClient.cs b/roles/lib/files/FWO.Middleware.Client/MiddlewareClient.cs index a8a3b6619..b1882f475 100644 --- a/roles/lib/files/FWO.Middleware.Client/MiddlewareClient.cs +++ b/roles/lib/files/FWO.Middleware.Client/MiddlewareClient.cs @@ -104,10 +104,10 @@ public async Task>> GetAllRoles() return await restClient.ExecuteAsync>(request); } - public async Task>> GetGroups() + public async Task>> GetGroups() { - RestRequest request = new RestRequest("Group/Get", Method.Get); - return await restClient.ExecuteAsync>(request); + RestRequest request = new RestRequest("Group", Method.Get); + return await restClient.ExecuteAsync>(request); } public async Task>> GetGroups(GroupGetParameters parameters) @@ -123,11 +123,10 @@ public async Task>> GetInternalGroup return await restClient.ExecuteAsync>(request); } - public async Task>> GetUsers() + public async Task>> GetUsers() { RestRequest request = new RestRequest("User", Method.Get); - request.AddJsonBody(new object()); - return await restClient.ExecuteAsync>(request); + return await restClient.ExecuteAsync>(request); } public async Task>> GetUsers(LdapUserGetParameters parameters) diff --git a/roles/middleware/files/FWO.Middleware.Server/Controllers/GroupController.cs b/roles/middleware/files/FWO.Middleware.Server/Controllers/GroupController.cs index f44580832..b3f520982 100644 --- a/roles/middleware/files/FWO.Middleware.Server/Controllers/GroupController.cs +++ b/roles/middleware/files/FWO.Middleware.Server/Controllers/GroupController.cs @@ -40,7 +40,7 @@ public async Task>> Get() foreach (Ldap currentLdap in ldaps) { - if (currentLdap.IsInternal() && currentLdap.HasGroupHandling()) + if (currentLdap.HasGroupHandling()) { ldapGroupRequests.Add(Task.Run(() => { diff --git a/roles/ui/files/FWO.UI/Pages/Reporting/ReportTemplateComponent.razor b/roles/ui/files/FWO.UI/Pages/Reporting/ReportTemplateComponent.razor index 0ba04b4ae..fd58102a9 100644 --- a/roles/ui/files/FWO.UI/Pages/Reporting/ReportTemplateComponent.razor +++ b/roles/ui/files/FWO.UI/Pages/Reporting/ReportTemplateComponent.razor @@ -2,63 +2,66 @@ @using FWO.Middleware.Client; @using FWO.Middleware.RequestParameters; @using FWO.Report.Filter +@using RestSharp; @inject UserConfig userConfig @inject ApiConnection apiConnection @inject MiddlewareClient middlewareClient
- -
- - - + + + + + + + + + +
+
+
@@ -120,58 +123,78 @@ - -

@(userConfig.GetText("U1002")) "@reportTemplateInEdit.Name" ?

- -
- - -
+ +

@(userConfig.GetText("U1002")) "@reportTemplateInEdit.Name" ?

+ +
+ + +
- - -

@(userConfig.GetText("template_share_text").Replace("%n", reportTemplateInEdit.Name)))

- - -
- - - -
+ + +

@(userConfig.GetText("template_share_text").Replace("%n", reportTemplateInEdit.Name)))

+
+ + + @(context?.UserName ?? "") + + + + + +
+ + + @(context.UserName) + + + +
+
+ + + +
+
@code { - [CascadingParameter] - private Task? authenticationStateTask { get; set; } + [CascadingParameter] + private Task? authenticationStateTask { get; set; } - [CascadingParameter] - private Action DisplayMessageInUi { get; set; } = DefaultInit.DoNothing; + [CascadingParameter] + private Action DisplayMessageInUi { get; set; } = DefaultInit.DoNothing; - [Parameter] - public Action? OnTemplateLoad { get; set; } + [Parameter] + public Action? OnTemplateLoad { get; set; } public List reportTemplates { get; set; } = new List(); public ReportTemplate reportTemplateInEdit = new ReportTemplate(); private ReportType reportTypeInEdit = ReportType.Rules; private List ownerList = new List(); - private List userDns = new List(); - private List groupDns = new List(); + private string tmpTemplateCommentKey = ""; + private string tmpTemplateComment = ""; - private string tmpTemplateCommentKey = ""; - private string tmpTemplateComment = ""; + private bool ShowSaveTemplateDialog = false; + private bool ShowEditTemplateDialog = false; + private bool ShowDeleteTemplateDialog = false; + private bool ShowShareTemplateDialog = false; - private bool ShowSaveTemplateDialog = false; - private bool ShowEditTemplateDialog = false; - private bool ShowDeleteTemplateDialog = false; - private bool ShowShareTemplateDialog = false; + private List dns = new List(); - private List viewerToAdd = new List(); - private List viewerToRemove = new List(); + private string viewerInputClass = ""; + private DistName? viewerToAdd; + private List viewersToAdd = new List(); + private List viewersToRemove = new List(); - private Collapse collapseControl = new Collapse(); + private Collapse collapseControl = new Collapse(); protected override async Task OnInitializedAsync() { @@ -179,31 +202,41 @@ ownerList = await apiConnection.SendQueryAsync>(FWO.Api.Client.Queries.OwnerQueries.getOwners); } - public async Task Refresh() - { - try - { - var parameters = new { userId = userConfig.User.DbId }; - reportTemplates = (await apiConnection.SendQueryAsync(ReportQueries.getReportTemplates, new { userId = userConfig.User.DbId })).ToList(); - groupDns = (await middlewareClient.GetGroups()).Data ?? new List(); - userDns = ((await middlewareClient.GetUsers()).Data ?? new List()).ConvertAll(user => user.UserDn); - await InvokeAsync(StateHasChanged); - } - catch (Exception exception) - { - DisplayMessageInUi(exception, userConfig.GetText("template_fetch"), "", true); - } - } - - public void Collapse() - { - collapseControl.ForceCollapse(); - } - - public void Uncollapse() - { - collapseControl.ToggleCollapse(); - } + public async Task Refresh() + { + try + { + var parameters = new { userId = userConfig.User.DbId }; + reportTemplates = (await apiConnection.SendQueryAsync(ReportQueries.getReportTemplates, new { userId = userConfig.User.DbId })).ToList(); + RestResponse> groupsRequest = await middlewareClient.GetGroups(); + RestResponse> usersRequest = await middlewareClient.GetUsers(); + if (!groupsRequest.IsSuccessful) + { + throw new Exception("Groups could not be fetched.", groupsRequest.ErrorException); + } + if (!usersRequest.IsSuccessful) + { + throw new Exception("Users could not be fetched.", usersRequest.ErrorException); + } + dns = (groupsRequest.Data ?? new List()).ConvertAll(group => new DistName(group.GroupDn)); + dns.AddRange((usersRequest.Data ?? new List()).ConvertAll(user => new DistName(user.UserDn))); + await InvokeAsync(StateHasChanged); + } + catch (Exception exception) + { + DisplayMessageInUi(exception, userConfig.GetText("template_fetch"), "", true); + } + } + + public void Collapse() + { + collapseControl.ForceCollapse(); + } + + public void Uncollapse() + { + collapseControl.ToggleCollapse(); + } public void NewTemplate(ReportTemplate reportTemplate) { @@ -212,183 +245,200 @@ ShowSaveTemplateDialog = true; } - public ReportTemplate decodeComment(ReportTemplate reportTemplate) - { - if (reportTemplate.Comment != null && reportTemplate.Comment.StartsWith("T01")) - { - tmpTemplateCommentKey = reportTemplate.Comment; - reportTemplate.Comment = userConfig.GetText(reportTemplate.Comment); - tmpTemplateComment = reportTemplate.Comment; - } - return reportTemplate; - } - - public string recodeComment(string Comment) - { - if (tmpTemplateCommentKey != "" && Comment == tmpTemplateComment) - { - Comment = tmpTemplateCommentKey; - } - return Comment; - } - - public string DisplayTime() - { - if(reportTypeInEdit.IsChangeReport()) - { - switch (reportTemplateInEdit.ReportParams.TimeFilter.TimeRangeType) - { - case TimeRangeType.Shortcut: - return userConfig.GetText(reportTemplateInEdit.ReportParams.TimeFilter.TimeRangeShortcut); - case TimeRangeType.Interval: - return userConfig.GetText("last") + " " + - reportTemplateInEdit.ReportParams.TimeFilter.Offset + " " + - userConfig.GetText(reportTemplateInEdit.ReportParams.TimeFilter.Interval.ToString()); - case TimeRangeType.Fixeddates: - if(reportTemplateInEdit.ReportParams.TimeFilter.OpenStart && reportTemplateInEdit.ReportParams.TimeFilter.OpenEnd) - { - return userConfig.GetText("open"); - } - else if(reportTemplateInEdit.ReportParams.TimeFilter.OpenStart) - { - return userConfig.GetText("until") + " " + reportTemplateInEdit.ReportParams.TimeFilter.EndTime.ToString(); - } - else if(reportTemplateInEdit.ReportParams.TimeFilter.OpenEnd) - { - return userConfig.GetText("from") + " " + reportTemplateInEdit.ReportParams.TimeFilter.StartTime.ToString(); - } - return reportTemplateInEdit.ReportParams.TimeFilter.StartTime.ToString() + " - " + reportTemplateInEdit.ReportParams.TimeFilter.EndTime.ToString(); - default: - return ""; - } - } - else - { - if (reportTemplateInEdit.ReportParams.TimeFilter.IsShortcut) - { - return userConfig.GetText(reportTemplateInEdit.ReportParams.TimeFilter.TimeShortcut); - } - else - { - return reportTemplateInEdit.ReportParams.TimeFilter.ReportTime.ToString(); - } - } - } - - private async Task SaveTemplate() - { - try - { - if (reportTemplateInEdit.Sanitize()) - { - DisplayMessageInUi(null, userConfig.GetText("save_template"), userConfig.GetText("U0001"), true); - } - reportTemplateInEdit.CreationDate = DateTime.Now; - reportTemplateInEdit.Owner = userConfig.User.DbId; - - var queryVariables = new - { - reportTemplateName = reportTemplateInEdit.Name, - reportFilterLine = reportTemplateInEdit.Filter, - reportTemplateCreate = reportTemplateInEdit.CreationDate, - reportTemplateComment = recodeComment(reportTemplateInEdit.Comment), - reportTemplateOwner = reportTemplateInEdit.Owner, - reportParameters = reportTemplateInEdit.ReportParams - }; - - ReturnId[]? returnIds = (await apiConnection.SendQueryAsync(ReportQueries.addReportTemplate, queryVariables)).ReturnIds; - if (returnIds != null) - { - reportTemplateInEdit.Id = returnIds[0].NewId; - ShowSaveTemplateDialog = false; - await Refresh(); // avoid strange behavior of new templates in device selection - } - else - { - DisplayMessageInUi(null, userConfig.GetText("save_template"), userConfig.GetText("E1004"), true); - } - } - catch (Exception exception) - { - DisplayMessageInUi(exception, userConfig.GetText("save_template"), "", true); - } - } - - private async Task EditTemplate() - { - try - { - var queryVariables = new - { - reportTemplateId = reportTemplateInEdit.Id, - reportTemplateName = reportTemplateInEdit.Name, - reportFilterLine = reportTemplateInEdit.Filter, - reportTemplateCreate = reportTemplateInEdit.CreationDate, - reportTemplateComment = recodeComment(reportTemplateInEdit.Comment), - reportTemplateOwner = reportTemplateInEdit.Owner, - reportParameters = reportTemplateInEdit.ReportParams - }; - - await apiConnection.SendQueryAsync(ReportQueries.editReportTemplate, queryVariables); - reportTemplates[reportTemplates.FindIndex(reportTemplate => reportTemplate.Id == reportTemplateInEdit.Id)] = reportTemplateInEdit; - - ShowEditTemplateDialog = false; - } - catch (Exception exception) - { - DisplayMessageInUi(exception, userConfig.GetText("edit_template"), "", true); - } - } - - private async Task DeleteTemplate() - { - try - { - var queryVariables = new - { - reportTemplateId = reportTemplateInEdit.Id - }; - - int affectedRows = (await apiConnection.SendQueryAsync(ReportQueries.deleteReportTemplate, queryVariables)).AffectedRows; - if (affectedRows > 0) - { - ReportTemplate? templateToRemove = reportTemplates.Find(reportTemplate => reportTemplate.Id == reportTemplateInEdit.Id); - if (templateToRemove != null) - { - reportTemplates.Remove(templateToRemove); - } - ShowDeleteTemplateDialog = false; - } - else - { - DisplayMessageInUi(null, userConfig.GetText("delete_template"), userConfig.GetText("E1005"), true); - } - } - catch (Exception exception) - { - DisplayMessageInUi(exception, userConfig.GetText("delete_template"), "", true); - } - } - - private async Task ShareTemplate(bool closeOnSuccess) - { - try - { - var queryVariables = new - { - reportTemplateId = reportTemplateInEdit.Id - }; - - //await apiConnection.SendQueryAsync(ReportQueries.shareReportTemplate, queryVariables); - - if (closeOnSuccess) - { - ShowShareTemplateDialog = false; - } - } - catch (Exception exception) - { - DisplayMessageInUi(exception, userConfig.GetText("E_share_title"), userConfig.GetText("E_share_message"), true); - } - } + public ReportTemplate decodeComment(ReportTemplate reportTemplate) + { + if (reportTemplate.Comment != null && reportTemplate.Comment.StartsWith("T01")) + { + tmpTemplateCommentKey = reportTemplate.Comment; + reportTemplate.Comment = userConfig.GetText(reportTemplate.Comment); + tmpTemplateComment = reportTemplate.Comment; + } + return reportTemplate; + } + + public string recodeComment(string Comment) + { + if (tmpTemplateCommentKey != "" && Comment == tmpTemplateComment) + { + Comment = tmpTemplateCommentKey; + } + return Comment; + } + + public string DisplayTime() + { + if (reportTypeInEdit.IsChangeReport()) + { + switch (reportTemplateInEdit.ReportParams.TimeFilter.TimeRangeType) + { + case TimeRangeType.Shortcut: + return userConfig.GetText(reportTemplateInEdit.ReportParams.TimeFilter.TimeRangeShortcut); + case TimeRangeType.Interval: + return userConfig.GetText("last") + " " + + reportTemplateInEdit.ReportParams.TimeFilter.Offset + " " + + userConfig.GetText(reportTemplateInEdit.ReportParams.TimeFilter.Interval.ToString()); + case TimeRangeType.Fixeddates: + if (reportTemplateInEdit.ReportParams.TimeFilter.OpenStart && reportTemplateInEdit.ReportParams.TimeFilter.OpenEnd) + { + return userConfig.GetText("open"); + } + else if (reportTemplateInEdit.ReportParams.TimeFilter.OpenStart) + { + return userConfig.GetText("until") + " " + reportTemplateInEdit.ReportParams.TimeFilter.EndTime.ToString(); + } + else if (reportTemplateInEdit.ReportParams.TimeFilter.OpenEnd) + { + return userConfig.GetText("from") + " " + reportTemplateInEdit.ReportParams.TimeFilter.StartTime.ToString(); + } + return reportTemplateInEdit.ReportParams.TimeFilter.StartTime.ToString() + " - " + reportTemplateInEdit.ReportParams.TimeFilter.EndTime.ToString(); + default: + return ""; + } + } + else + { + if (reportTemplateInEdit.ReportParams.TimeFilter.IsShortcut) + { + return userConfig.GetText(reportTemplateInEdit.ReportParams.TimeFilter.TimeShortcut); + } + else + { + return reportTemplateInEdit.ReportParams.TimeFilter.ReportTime.ToString(); + } + } + } + + private async Task SaveTemplate() + { + try + { + if (reportTemplateInEdit.Sanitize()) + { + DisplayMessageInUi(null, userConfig.GetText("save_template"), userConfig.GetText("U0001"), true); + } + reportTemplateInEdit.CreationDate = DateTime.Now; + reportTemplateInEdit.Owner = userConfig.User.DbId; + + var queryVariables = new + { + reportTemplateName = reportTemplateInEdit.Name, + reportFilterLine = reportTemplateInEdit.Filter, + reportTemplateCreate = reportTemplateInEdit.CreationDate, + reportTemplateComment = recodeComment(reportTemplateInEdit.Comment), + reportTemplateOwner = reportTemplateInEdit.Owner, + reportParameters = reportTemplateInEdit.ReportParams + }; + + ReturnId[]? returnIds = (await apiConnection.SendQueryAsync(ReportQueries.addReportTemplate, queryVariables)).ReturnIds; + if (returnIds != null) + { + reportTemplateInEdit.Id = returnIds[0].NewId; + ShowSaveTemplateDialog = false; + await Refresh(); // avoid strange behavior of new templates in device selection + } + else + { + DisplayMessageInUi(null, userConfig.GetText("save_template"), userConfig.GetText("E1004"), true); + } + } + catch (Exception exception) + { + DisplayMessageInUi(exception, userConfig.GetText("save_template"), "", true); + } + } + + private async Task EditTemplate() + { + try + { + var queryVariables = new + { + reportTemplateId = reportTemplateInEdit.Id, + reportTemplateName = reportTemplateInEdit.Name, + reportFilterLine = reportTemplateInEdit.Filter, + reportTemplateCreate = reportTemplateInEdit.CreationDate, + reportTemplateComment = recodeComment(reportTemplateInEdit.Comment), + reportTemplateOwner = reportTemplateInEdit.Owner, + reportParameters = reportTemplateInEdit.ReportParams + }; + + await apiConnection.SendQueryAsync(ReportQueries.editReportTemplate, queryVariables); + reportTemplates[reportTemplates.FindIndex(reportTemplate => reportTemplate.Id == reportTemplateInEdit.Id)] = reportTemplateInEdit; + + ShowEditTemplateDialog = false; + } + catch (Exception exception) + { + DisplayMessageInUi(exception, userConfig.GetText("edit_template"), "", true); + } + } + + private async Task DeleteTemplate() + { + try + { + var queryVariables = new + { + reportTemplateId = reportTemplateInEdit.Id + }; + + int affectedRows = (await apiConnection.SendQueryAsync(ReportQueries.deleteReportTemplate, queryVariables)).AffectedRows; + if (affectedRows > 0) + { + ReportTemplate? templateToRemove = reportTemplates.Find(reportTemplate => reportTemplate.Id == reportTemplateInEdit.Id); + if (templateToRemove != null) + { + reportTemplates.Remove(templateToRemove); + } + ShowDeleteTemplateDialog = false; + } + else + { + DisplayMessageInUi(null, userConfig.GetText("delete_template"), userConfig.GetText("E1005"), true); + } + } + catch (Exception exception) + { + DisplayMessageInUi(exception, userConfig.GetText("delete_template"), "", true); + } + } + + private bool AddReportTemplateViewer(List viewers, ref DistName? viewerToAdd, ref string inputClass) + { + if (viewerToAdd != null && !viewers.Contains(viewerToAdd)) + { + // Insert at the beginning so that the newest viewer is always displayed at the top + viewers.Insert(0, viewerToAdd); + viewerToAdd = null; + inputClass = "is-valid"; + return true; + } + else + { + inputClass = "is-invalid"; + return false; + } + } + + private async Task ShareTemplate(bool closeOnSuccess) + { + try + { + var queryVariables = new + { + reportTemplateId = reportTemplateInEdit.Id + }; + + //await apiConnection.SendQueryAsync(ReportQueries.shareReportTemplate, queryVariables); + + if (closeOnSuccess) + { + ShowShareTemplateDialog = false; + } + } + catch (Exception exception) + { + DisplayMessageInUi(exception, userConfig.GetText("E_share_title"), userConfig.GetText("E_share_message"), true); + } + } }