Skip to content

Commit 7f2a772

Browse files
authored
chore: mention child events in access explanations (#504)
1 parent b25de82 commit 7f2a772

File tree

2 files changed

+55
-36
lines changed

2 files changed

+55
-36
lines changed

web/static/admin_events.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var Validity;
5151
Validity["onsite"] = "onsite";
5252
})(Validity || (Validity = {}));
5353
const allAccessModes = ["readers", "writers", "reporters"];
54-
let events;
54+
let sortedEvents;
5555
let accessControlList = null;
5656
async function loadAccessControlList() {
5757
const { json: eventsJson, err: eventsErr } = await ims.fetchNoThrow(url_events + "?include_groups=true", {
@@ -63,7 +63,7 @@ async function loadAccessControlList() {
6363
window.alert(message);
6464
return { err: message };
6565
}
66-
events = eventsJson ?? [];
66+
const events = eventsJson ?? [];
6767
const { json, err } = await ims.fetchNoThrow(url_acl, null);
6868
if (err != null) {
6969
const message = `Failed to load access control list: ${err}`;
@@ -72,18 +72,8 @@ async function loadAccessControlList() {
7272
return { err: message };
7373
}
7474
accessControlList = json;
75-
return { err: null };
76-
}
77-
function drawAccess() {
78-
const container = document.getElementById("event_access_container");
79-
container.replaceChildren();
80-
if (accessControlList == null) {
81-
return;
82-
}
83-
const accessTemplate = document.getElementById("event_access_template");
84-
const accessModeTemplate = document.getElementById("event_access_mode_template");
8575
// Sort by group status first, then by name
86-
const sortedEvents = events.toSorted((a, b) => {
76+
sortedEvents = events.toSorted((a, b) => {
8777
const aGroup = a.is_group ? a.id : (a.parent_group ?? -1);
8878
const bGroup = b.is_group ? b.id : (b.parent_group ?? -1);
8979
if (aGroup !== bGroup) {
@@ -94,14 +84,24 @@ function drawAccess() {
9484
}
9585
return b.name.localeCompare(a.name);
9686
});
87+
return { err: null };
88+
}
89+
function drawAccess() {
90+
const container = document.getElementById("event_access_container");
91+
container.replaceChildren();
92+
if (accessControlList == null) {
93+
return;
94+
}
95+
const accessTemplate = document.getElementById("event_access_template");
96+
const accessModeTemplate = document.getElementById("event_access_mode_template");
9797
for (const event of sortedEvents) {
9898
const eventAccessFrag = accessTemplate.content.cloneNode(true);
9999
let eventWithGroupName = event.name;
100100
if (event.is_group) {
101101
eventWithGroupName = `Group: ${eventWithGroupName}`;
102102
}
103103
if (event.parent_group) {
104-
const parentGroup = events.find(value => { return value.id === event.parent_group; });
104+
const parentGroup = sortedEvents.find(value => { return value.id === event.parent_group; });
105105
if (parentGroup) {
106106
eventWithGroupName += ` (${parentGroup.name})`;
107107
}
@@ -117,7 +117,7 @@ function drawAccess() {
117117
const parentGroupInput = editEventModalElement.querySelector("#edit_parent_group");
118118
// groups can't have parent groups
119119
parentGroupInput.disabled = event.is_group ?? false;
120-
const currentParent = events.find(value => { return value.id === event.parent_group; });
120+
const currentParent = sortedEvents.find(value => { return value.id === event.parent_group; });
121121
parentGroupInput.value = currentParent?.name ?? "";
122122
editEventModal?.show();
123123
});
@@ -223,7 +223,17 @@ function updateEventAccess(event, mode) {
223223
if (explainMsgs.length === 0) {
224224
explainMsgs.push("No permissions");
225225
}
226-
modal.querySelector(".modal-body").textContent = explainMsgs.join("\n");
226+
const modalBody = modal.querySelector(".modal-body");
227+
modalBody.textContent = explainMsgs.join("\n");
228+
const eventData = sortedEvents.find(value => { return value.name === event; });
229+
if (eventData && eventData.is_group) {
230+
modalBody.textContent += "\n\nThis is an event group, so all permissions above also apply to its child events:\n";
231+
for (const event of sortedEvents) {
232+
if (event.parent_group === eventData.id) {
233+
modalBody.textContent += `${indent}${event.name}\n`;
234+
}
235+
}
236+
}
227237
explainModal?.show();
228238
});
229239
}
@@ -430,7 +440,7 @@ async function setParentGroup(sender) {
430440
requestBod.parent_group = 0;
431441
}
432442
else {
433-
const newParent = events.find(value => { return value.name === parentGroupName; });
443+
const newParent = sortedEvents.find(value => { return value.name === parentGroupName; });
434444
if (!newParent) {
435445
const message = `No group by that name`;
436446
console.log(message);

web/typescript/admin_events.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type EventAccess = Partial<Record<AccessMode, Access[]>>;
9393
// key is event name
9494
type EventsAccess = Record<string, EventAccess|null>;
9595

96-
let events: ims.EventData[];
96+
let sortedEvents: ims.EventData[];
9797
let accessControlList: EventsAccess|null = null;
9898

9999
async function loadAccessControlList() : Promise<{err: string|null}> {
@@ -106,7 +106,7 @@ async function loadAccessControlList() : Promise<{err: string|null}> {
106106
window.alert(message);
107107
return {err: message};
108108
}
109-
events = eventsJson??[];
109+
const events = eventsJson??[];
110110
const {json, err} = await ims.fetchNoThrow<EventsAccess>(url_acl, null);
111111
if (err != null) {
112112
const message = `Failed to load access control list: ${err}`;
@@ -115,6 +115,18 @@ async function loadAccessControlList() : Promise<{err: string|null}> {
115115
return {err: message};
116116
}
117117
accessControlList = json;
118+
// Sort by group status first, then by name
119+
sortedEvents = events.toSorted((a: ims.EventData, b: ims.EventData): number => {
120+
const aGroup = a.is_group ? a.id : (a.parent_group??-1);
121+
const bGroup = b.is_group ? b.id : (b.parent_group??-1);
122+
if (aGroup !== bGroup) {
123+
return bGroup-aGroup;
124+
}
125+
if (a.is_group !== b.is_group) {
126+
return a.is_group ? -1 : 1;
127+
}
128+
return b.name.localeCompare(a.name);
129+
});
118130
return {err: null};
119131
}
120132

@@ -128,19 +140,6 @@ function drawAccess(): void {
128140
const accessTemplate = document.getElementById("event_access_template") as HTMLTemplateElement;
129141
const accessModeTemplate = document.getElementById("event_access_mode_template") as HTMLTemplateElement;
130142

131-
// Sort by group status first, then by name
132-
const sortedEvents: ims.EventData[] = events.toSorted((a: ims.EventData, b: ims.EventData): number => {
133-
const aGroup = a.is_group ? a.id : (a.parent_group??-1);
134-
const bGroup = b.is_group ? b.id : (b.parent_group??-1);
135-
if (aGroup !== bGroup) {
136-
return bGroup-aGroup;
137-
}
138-
if (a.is_group !== b.is_group) {
139-
return a.is_group ? -1 : 1;
140-
}
141-
return b.name.localeCompare(a.name);
142-
});
143-
144143
for (const event of sortedEvents) {
145144
const eventAccessFrag = accessTemplate.content.cloneNode(true) as DocumentFragment;
146145

@@ -149,7 +148,7 @@ function drawAccess(): void {
149148
eventWithGroupName = `Group: ${eventWithGroupName}`;
150149
}
151150
if (event.parent_group) {
152-
const parentGroup = events.find(value => {return value.id === event.parent_group});
151+
const parentGroup = sortedEvents.find(value => {return value.id === event.parent_group});
153152
if (parentGroup) {
154153
eventWithGroupName += ` (${parentGroup.name})`;
155154
}
@@ -170,7 +169,7 @@ function drawAccess(): void {
170169

171170
// groups can't have parent groups
172171
parentGroupInput.disabled = event.is_group??false;
173-
const currentParent = events.find(value => {return value.id === event.parent_group});
172+
const currentParent = sortedEvents.find(value => {return value.id === event.parent_group});
174173
parentGroupInput.value = currentParent?.name??"";
175174

176175
editEventModal?.show();
@@ -290,7 +289,17 @@ function updateEventAccess(event: string, mode: AccessMode): void {
290289
if (explainMsgs.length === 0) {
291290
explainMsgs.push("No permissions");
292291
}
293-
modal.querySelector(".modal-body")!.textContent = explainMsgs.join("\n");
292+
const modalBody = modal.querySelector(".modal-body")!;
293+
modalBody.textContent = explainMsgs.join("\n");
294+
const eventData = sortedEvents.find(value => {return value.name === event});
295+
if (eventData && eventData.is_group) {
296+
modalBody.textContent += "\n\nThis is an event group, so all permissions above also apply to its child events:\n";
297+
for (const event of sortedEvents) {
298+
if (event.parent_group === eventData.id) {
299+
modalBody.textContent += `${indent}${event.name}\n`;
300+
}
301+
}
302+
}
294303
explainModal?.show();
295304
})
296305
}
@@ -540,7 +549,7 @@ async function setParentGroup(sender: HTMLInputElement): Promise<void> {
540549
// unset parent_group
541550
requestBod.parent_group = 0;
542551
} else {
543-
const newParent = events.find(value => {return value.name === parentGroupName});
552+
const newParent = sortedEvents.find(value => {return value.name === parentGroupName});
544553
if (!newParent) {
545554
const message = `No group by that name`;
546555
console.log(message);

0 commit comments

Comments
 (0)