Skip to content

Commit c9c5ee2

Browse files
committed
Resolved acl/access_groups complexity
1 parent 714f329 commit c9c5ee2

2 files changed

Lines changed: 88 additions & 41 deletions

File tree

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/acl/access-groups.ts

Lines changed: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ export class AccessGroups {
8585
const groupElements: HTMLElement[] = []
8686
for (let comboIndex = 15; comboIndex > 0; comboIndex--) {
8787
const combo = kToCombo(comboIndex)
88-
if ((this.controller.isEditable && RECOMMENDED[comboIndex]) || this.byCombo[combo]) {
88+
if (
89+
(this.controller.isEditable && RECOMMENDED[comboIndex]) ||
90+
this.byCombo[combo]
91+
) {
8992
groupElements.push(this.renderGroup(comboIndex, combo))
9093
}
9194
}
@@ -95,9 +98,11 @@ export class AccessGroups {
9598
private renderGroup (comboIndex: number, combo: string): HTMLElement {
9699
const groupRow = this.controller.dom.createElement('div')
97100
groupRow.classList.add(this.controller.classes.accessGroupListItem)
98-
widgets.makeDropTarget(groupRow, (uris) => this.handleDroppedUris(uris, combo)
99-
.then(() => this.controller.render())
100-
.catch(error => this.controller.renderStatus(error)))
101+
widgets.makeDropTarget(groupRow, uris =>
102+
this.handleDroppedUris(uris, combo)
103+
.then(() => this.controller.render())
104+
.catch(error => this.controller.renderStatus(error))
105+
)
101106
const groupColumns = this.renderGroupElements(comboIndex, combo)
102107
groupColumns.forEach(column => groupRow.appendChild(column))
103108
return groupRow
@@ -106,38 +111,62 @@ export class AccessGroups {
106111
private renderGroupElements (comboIndex, combo): HTMLElement[] {
107112
const groupNameColumn = this.controller.dom.createElement('div')
108113
groupNameColumn.classList.add(this.controller.classes.group)
109-
groupNameColumn.classList.toggle(this.controller.classes[`group-${comboIndex}`], this.controller.isEditable)
114+
groupNameColumn.classList.toggle(
115+
this.controller.classes[`group-${comboIndex}`],
116+
this.controller.isEditable
117+
)
110118
groupNameColumn.innerText = COLLOQUIAL[comboIndex] || ktToList(comboIndex)
111119

112120
const groupAgentsColumn = this.controller.dom.createElement('div')
113121
groupAgentsColumn.classList.add(this.controller.classes.group)
114-
groupAgentsColumn.classList.toggle(this.controller.classes[`group-${comboIndex}`], this.controller.isEditable)
115-
const groupAgentsTable = groupAgentsColumn.appendChild(this.controller.dom.createElement('table'))
122+
groupAgentsColumn.classList.toggle(
123+
this.controller.classes[`group-${comboIndex}`],
124+
this.controller.isEditable
125+
)
126+
const groupAgentsTable = groupAgentsColumn.appendChild(
127+
this.controller.dom.createElement('table')
128+
)
116129
const combos = this.byCombo[combo] || []
117130
combos
118-
.map(([pred, obj]) => this.renderAgent(groupAgentsTable, combo, pred, obj))
131+
.map(([pred, obj]) =>
132+
this.renderAgent(groupAgentsTable, combo, pred, obj)
133+
)
119134
.forEach(agentElement => groupAgentsTable.appendChild(agentElement))
120135

121136
const groupDescriptionElement = this.controller.dom.createElement('div')
122137
groupDescriptionElement.classList.add(this.controller.classes.group)
123-
groupDescriptionElement.classList.toggle(this.controller.classes[`group-${comboIndex}`], this.controller.isEditable)
124-
groupDescriptionElement.innerText = EXPLANATION[comboIndex] || 'Unusual combination'
138+
groupDescriptionElement.classList.toggle(
139+
this.controller.classes[`group-${comboIndex}`],
140+
this.controller.isEditable
141+
)
142+
groupDescriptionElement.innerText =
143+
EXPLANATION[comboIndex] || 'Unusual combination'
125144

126145
return [groupNameColumn, groupAgentsColumn, groupDescriptionElement]
127146
}
128147

129148
private renderAgent (groupAgentsTable, combo, pred, obj): HTMLElement {
130-
const personRow = widgets.personTR(this.controller.dom, ACL(pred), sym(obj), this.controller.isEditable ? {
131-
deleteFunction: () => this.deleteAgent(combo, pred, obj)
132-
.then(() => groupAgentsTable.removeChild(personRow))
133-
.catch(error => this.controller.renderStatus(error))
134-
} : {})
149+
const personRow = widgets.personTR(
150+
this.controller.dom,
151+
ACL(pred),
152+
sym(obj),
153+
this.controller.isEditable
154+
? {
155+
deleteFunction: () =>
156+
this.deleteAgent(combo, pred, obj)
157+
.then(() => groupAgentsTable.removeChild(personRow))
158+
.catch(error => this.controller.renderStatus(error))
159+
}
160+
: {}
161+
)
135162
return personRow
136163
}
137164

138165
private async deleteAgent (combo, pred, obj): Promise<void> {
139166
const combos = this.byCombo[combo] || []
140-
const comboToRemove = combos.find(([comboPred, comboObj]) => comboPred === pred && comboObj === obj)
167+
const comboToRemove = combos.find(
168+
([comboPred, comboObj]) => comboPred === pred && comboObj === obj
169+
)
141170
if (comboToRemove) {
142171
combos.splice(combos.indexOf(comboToRemove), 1)
143172
}
@@ -149,7 +178,10 @@ export class AccessGroups {
149178
await this.controller.save()
150179
}
151180

152-
private async handleDroppedUris (uris: string[], combo: string): Promise<void> {
181+
private async handleDroppedUris (
182+
uris: string[],
183+
combo: string
184+
): Promise<void> {
153185
try {
154186
await Promise.all(uris.map(uri => this.handleDroppedUri(uri, combo)))
155187
await this.controller.save()
@@ -158,7 +190,11 @@ export class AccessGroups {
158190
}
159191
}
160192

161-
private async handleDroppedUri (uri: string, combo: string, secondAttempt: boolean = false): Promise<void> {
193+
private async handleDroppedUri (
194+
uri: string,
195+
combo: string,
196+
secondAttempt: boolean = false
197+
): Promise<void> {
162198
const agent = findAgent(uri, this.store) // eg 'agent', 'origin', agentClass'
163199
const thing = sym(uri)
164200
if (!agent && !secondAttempt) {
@@ -179,7 +215,12 @@ export class AccessGroups {
179215
this.setACLCombo(combo, uri, agent, this.controller.subject)
180216
}
181217

182-
private setACLCombo (combo: string, uri: string, res: PartialAgentTriple, subject: NamedNode): void {
218+
private setACLCombo (
219+
combo: string,
220+
uri: string,
221+
res: PartialAgentTriple,
222+
subject: NamedNode
223+
): void {
183224
if (!(combo in this.byCombo)) {
184225
this.byCombo[combo] = []
185226
}
@@ -224,7 +265,26 @@ function ktToList (k: number): string {
224265
}
225266
return list
226267
}
227-
268+
function isOriginWithSlashes (uri) {
269+
return (
270+
uri.startsWith('http') && uri.split('/').length === 4 && uri.endsWith('/')
271+
)
272+
}
273+
function isAgentClass (obj) {
274+
return (
275+
obj.sameTerm(ns.foaf('Agent')) ||
276+
obj.sameTerm(ns.acl('AuthenticatedAgent')) || // AuthenticatedAgent
277+
obj.sameTerm(ns.rdf('Resource')) ||
278+
obj.sameTerm(ns.owl('Thing'))
279+
)
280+
}
281+
function isAgent (ns, types) {
282+
return (
283+
ns.vcard('Individual').uri in types ||
284+
ns.foaf('Person').uri in types ||
285+
ns.foaf('Agent').uri in types
286+
)
287+
}
228288
function findAgent (uri, kb): PartialAgentTriple | null {
229289
const obj = sym(uri)
230290
const types = kb.findTypeURIs(obj)
@@ -237,11 +297,7 @@ function findAgent (uri, kb): PartialAgentTriple | null {
237297
return { pred: 'origin', obj: obj } // The only way to know an origin alas
238298
}
239299
// @@ This is an almighty kludge needed because drag and drop adds extra slashes to origins
240-
if (
241-
uri.startsWith('http') &&
242-
uri.split('/').length === 4 &&
243-
uri.endsWith('/')
244-
) {
300+
if (isOriginWithSlashes(uri)) {
245301
// there IS third slash
246302
console.log('Assuming final slash on dragged origin URI was unintended!')
247303
return { pred: 'origin', obj: sym(uri.slice(0, -1)) } // Fix a URI where the drag and drop system has added a spurious slash
@@ -252,19 +308,10 @@ function findAgent (uri, kb): PartialAgentTriple | null {
252308
if (ns.vcard('Group').uri in types) {
253309
return { pred: 'agentGroup', obj: obj } // @@ note vcard membership not RDFs
254310
}
255-
if (
256-
obj.sameTerm(ns.foaf('Agent')) ||
257-
obj.sameTerm(ns.acl('AuthenticatedAgent')) || // AuthenticatedAgent
258-
obj.sameTerm(ns.rdf('Resource')) ||
259-
obj.sameTerm(ns.owl('Thing'))
260-
) {
311+
if (isAgentClass(obj)) {
261312
return { pred: 'agentClass', obj: obj }
262313
}
263-
if (
264-
ns.vcard('Individual').uri in types ||
265-
ns.foaf('Person').uri in types ||
266-
ns.foaf('Agent').uri in types
267-
) {
314+
if (isAgent(ns, types)) {
268315
const pref = kb.any(obj, ns.foaf('preferredURI'))
269316
if (pref) return { pred: 'agent', obj: sym(pref) }
270317
return { pred: 'agent', obj: obj }

0 commit comments

Comments
 (0)