forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess-controller.ts
More file actions
261 lines (239 loc) · 10.5 KB
/
access-controller.ts
File metadata and controls
261 lines (239 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* Contains the [[AccessController]] class
* @packageDocumentation
*/
import { adoptACLDefault, getProspectiveHolder, makeACLGraphbyCombo, sameACL } from './acl'
import { fetcher, graph, NamedNode, UpdateManager } from 'rdflib'
import { AccessGroups } from './access-groups'
import { DataBrowserContext } from 'pane-registry'
import { shortNameForFolder } from './acl-control'
import * as utils from '../utils'
import * as debug from '../debug'
import { style } from '../style'
/**
* Rendered HTML component used in the databrowser's Sharing pane.
*/
export class AccessController {
public mainCombo: AccessGroups
public defaultsCombo: AccessGroups | null
private readonly isContainer: boolean
private defaultsDiffer: boolean
private readonly rootElement: HTMLDivElement
private isUsingDefaults: boolean
constructor (
public subject: NamedNode,
public noun: string,
public context: DataBrowserContext,
private statusElement: HTMLElement,
public targetIsProtected: boolean,
private targetDoc: NamedNode,
private targetACLDoc: NamedNode,
private defaultHolder: NamedNode | null,
private defaultACLDoc: NamedNode | null,
private prospectiveDefaultHolder: NamedNode | undefined,
public store,
public dom: HTMLDocument
) {
this.rootElement = dom.createElement('div')
this.rootElement.setAttribute('style', style.aclGroupContent)
this.isContainer = targetDoc.uri.slice(-1) === '/' // Give default for all directories
if (defaultHolder && defaultACLDoc) {
this.isUsingDefaults = true
const aclDefaultStore = adoptACLDefault(this.targetDoc, targetACLDoc, defaultHolder, defaultACLDoc)
this.mainCombo = new AccessGroups(targetDoc, targetACLDoc, this, aclDefaultStore, { defaults: this.isContainer })
this.defaultsCombo = null
this.defaultsDiffer = false
} else {
this.isUsingDefaults = false
this.mainCombo = new AccessGroups(targetDoc, targetACLDoc, this, store)
this.defaultsCombo = new AccessGroups(targetDoc, targetACLDoc, this, store, { defaults: this.isContainer })
this.defaultsDiffer = !sameACL(this.mainCombo.aclMap, this.defaultsCombo.aclMap)
}
}
public get isEditable (): boolean {
return !this.isUsingDefaults
}
public render (): HTMLElement {
this.rootElement.innerHTML = ''
if (this.isUsingDefaults) {
this.renderStatus(`The sharing for this ${this.noun} is the default for folder `)
if (this.defaultHolder) {
const defaultHolderLink = this.statusElement.appendChild(this.dom.createElement('a'))
defaultHolderLink.href = this.defaultHolder.uri
defaultHolderLink.innerText = shortNameForFolder(this.defaultHolder)
}
} else if (!this.defaultsDiffer && this.isContainer) {
this.renderStatus('This is also the default for things in this folder.')
} else {
this.renderStatus('')
}
this.rootElement.appendChild(this.mainCombo.render())
if (this.defaultsCombo && this.defaultsDiffer) {
this.rootElement.appendChild(this.renderRemoveDefaultsController())
this.rootElement.appendChild(this.defaultsCombo.render())
} else if (this.isEditable && this.isContainer) {
this.rootElement.appendChild(this.renderAddDefaultsController())
}
if (!this.targetIsProtected && this.isUsingDefaults) {
this.rootElement.appendChild(this.renderAddAclsController())
} else if (!this.targetIsProtected) {
this.rootElement.appendChild(this.renderRemoveAclsController())
}
return this.rootElement
}
private renderRemoveAclsController (): HTMLElement {
const useDefaultButton = this.dom.createElement('button')
useDefaultButton.innerText = `Remove custom sharing settings for this ${this.noun} -- just use default${this.prospectiveDefaultHolder ? ` for ${utils.label(this.prospectiveDefaultHolder)}` : ''}`
useDefaultButton.setAttribute('style', style.bigButton)
useDefaultButton.addEventListener('click', () => this.removeAcls()
.then(() => this.render())
.catch(error => this.renderStatus(error)))
return useDefaultButton
}
private renderAddAclsController (): HTMLElement {
const addAclButton = this.dom.createElement('button')
addAclButton.innerText = `Set specific sharing for this ${this.noun}`
addAclButton.setAttribute('style', style.bigButton)
addAclButton.addEventListener('click', () => this.addAcls()
.then(() => this.render())
.catch(error => this.renderStatus(error)))
return addAclButton
}
private renderAddDefaultsController (): HTMLElement {
const containerElement = this.dom.createElement('div')
containerElement.setAttribute('style', style.defaultsController)
const noticeElement = containerElement.appendChild(this.dom.createElement('div'))
noticeElement.innerText = 'Sharing for things within the folder currently tracks sharing for the folder.'
noticeElement.setAttribute('style', style.defaultsControllerNotice)
const button = containerElement.appendChild(this.dom.createElement('button'))
button.innerText = 'Set the sharing of folder contents separately from the sharing for the folder'
button.setAttribute('style', style.bigButton)
button.addEventListener('click', () => this.addDefaults()
.then(() => this.render()))
return containerElement
}
private renderRemoveDefaultsController (): HTMLElement {
const containerElement = this.dom.createElement('div')
containerElement.setAttribute('style', style.defaultsController)
const noticeElement = containerElement.appendChild(this.dom.createElement('div'))
noticeElement.innerText = 'Access to things within this folder:'
noticeElement.setAttribute('style', style.defaultsControllerNotice)
const button = containerElement.appendChild(this.dom.createElement('button'))
button.innerText = 'Set default for folder contents to just track the sharing for the folder'
button.setAttribute('style', style.bigButton)
button.addEventListener('click', () => this.removeDefaults()
.then(() => this.render())
.catch(error => this.renderStatus(error)))
return containerElement
}
public renderTemporaryStatus (message: string): void {
// @@ TODO Introduce better system for error notification to user https://github.com/solidos/mashlib/issues/87
this.statusElement.setAttribute('style', style.aclControlBoxStatusRevealed)
this.statusElement.innerText = message
this.statusElement.setAttribute('style', style.temporaryStatusInit)
setTimeout(() => {
this.statusElement.setAttribute('style', style.temporaryStatusEnd)
})
setTimeout(() => {
this.statusElement.innerText = ''
}, 5000)
}
public renderStatus (message: string): void {
// @@ TODO Introduce better system for error notification to user https://github.com/solidos/mashlib/issues/87
if (!message) {
this.statusElement.setAttribute('style', style.aclControlBoxStatusRevealed)
}
this.statusElement.innerText = message
}
private async addAcls (): Promise<void> {
if (!this.defaultHolder || !this.defaultACLDoc) {
const message = 'Unable to find defaults to copy'
debug.error(message)
return Promise.reject(message)
}
const aclGraph = adoptACLDefault(this.targetDoc, this.targetACLDoc, this.defaultHolder, this.defaultACLDoc)
aclGraph.statements.forEach(st => this.store.add(st.subject, st.predicate, st.object, this.targetACLDoc))
try {
await this.store.fetcher.putBack(this.targetACLDoc)
this.isUsingDefaults = false
return Promise.resolve()
} catch (error) {
const message = ` Error writing back access control file! ${error}`
debug.error(message)
return Promise.reject(message)
}
}
private async addDefaults (): Promise<void> {
this.defaultsCombo = new AccessGroups(this.targetDoc, this.targetACLDoc, this, this.store, { defaults: true })
this.defaultsDiffer = true
}
private async removeAcls (): Promise<void> {
try {
await this.store.fetcher.delete(this.targetACLDoc.uri, {})
this.isUsingDefaults = true
try {
this.prospectiveDefaultHolder = await getProspectiveHolder(this.targetDoc.uri)
} catch (error) {
// No need to show this error in status, but good to warn about it in console
debug.warn(error)
}
} catch (error) {
const message = `Error deleting access control file: ${this.targetACLDoc}: ${error}`
debug.error(message)
return Promise.reject(message)
}
}
private async removeDefaults (): Promise<void> {
const fallbackCombo = this.defaultsCombo
try {
this.defaultsCombo = null
this.defaultsDiffer = false
await this.save()
} catch (error) {
this.defaultsCombo = fallbackCombo
this.defaultsDiffer = true
debug.error(error)
return Promise.reject(error)
}
}
public save (): Promise<void> {
// build graph
const newAClGraph = graph()
if (!this.isContainer) {
makeACLGraphbyCombo(newAClGraph, this.targetDoc, this.mainCombo.byCombo, this.targetACLDoc, true)
} else if (this.defaultsCombo && this.defaultsDiffer) {
// Pair of controls
makeACLGraphbyCombo(newAClGraph, this.targetDoc, this.mainCombo.byCombo, this.targetACLDoc, true)
makeACLGraphbyCombo(newAClGraph, this.targetDoc, this.defaultsCombo.byCombo, this.targetACLDoc, false, true)
} else {
// Linked controls
makeACLGraphbyCombo(newAClGraph, this.targetDoc, this.mainCombo.byCombo, this.targetACLDoc, true, true)
}
// add authenticated fetcher
newAClGraph.fetcher = fetcher(newAClGraph, { fetch: this.store.fetcher._fetch })
const updater = newAClGraph.updater || new UpdateManager(newAClGraph)
// save ACL resource
return new Promise((resolve, reject) => {
updater.put(
this.targetACLDoc,
newAClGraph.statementsMatching(undefined, undefined, undefined, this.targetACLDoc),
'text/turtle',
(uri, ok, message) => {
if (!ok) {
return reject(new Error(`ACL file save failed: ${message}`))
}
this.store.fetcher.unload(this.targetACLDoc)
this.store.add(newAClGraph.statements)
this.store.fetcher.requested[this.targetACLDoc.uri] = 'done' // missing: save headers
this.mainCombo.store = this.store
if (this.defaultsCombo) {
this.defaultsCombo.store = this.store
}
this.defaultsDiffer = !!this.defaultsCombo && !sameACL(this.mainCombo.aclMap, this.defaultsCombo.aclMap)
debug.log('ACL modification: success!')
resolve()
}
)
})
}
}