forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacl-control.ts
More file actions
216 lines (195 loc) · 7.12 KB
/
acl-control.ts
File metadata and controls
216 lines (195 loc) · 7.12 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
/**
* Functions for rendering the ACL User Interface.
* See https://github.com/solidos/userguide/blob/main/views/sharing/userguide.md#view
* for a screenshot.
* @packageDocumentation
*/
import ns from '../ns'
import * as utils from '../utils'
import { getACLorDefault, getProspectiveHolder } from './acl'
import { Store, NamedNode } from 'rdflib'
import { DataBrowserContext } from 'pane-registry'
import { AccessController } from './access-controller'
import { style } from '../style'
import { log, warn } from '../debug'
let global: Window = window
const preventBrowserDropEventsDone = Symbol('prevent double triggering of drop event')
/**
* See https://coshx.com/preventing-drag-and-drop-disasters-with-a-chrome-userscript
* Without this dropping anything onto a browser page will cause chrome etc to jump to diff page
* throwing away all the user's work.
*
* In apps which may use drag and drop, this utility takes care of the fact
* by default in a browser, an uncaught user drop into a browser window
* causes the browser to lose all its work in that window and navigate to another page
*
* @param document The DOM
* @returns void
*/
export function preventBrowserDropEvents (document: HTMLDocument): void {
log('preventBrowserDropEvents called.')
if (typeof global !== 'undefined') {
if (global[preventBrowserDropEventsDone]) return
global[preventBrowserDropEventsDone] = true
}
document.addEventListener('drop', handleDrop, false)
document.addEventListener('dragenter', preventDrag, false)
document.addEventListener('dragover', preventDrag, false)
}
/** @internal */
export function preventDrag (e) {
e.stopPropagation()
e.preventDefault()
}
/** @internal */
export function handleDrop (e) {
if (e.dataTransfer.files.length > 0) {
if (
!global.confirm('Are you sure you want to drop this file here? (Cancel opens it in a new tab)')
) {
e.stopPropagation()
e.preventDefault()
log('@@@@ document-level DROP suppressed: ' + e.dataTransfer.dropEffect
)
}
}
}
/**
* Get a folder's own filename in the directory tree. Also works for
* domain names; the URL protocol ('https://') acts as the tree root
* with short name '/' (see also test/unit/acl/acl-control.test.ts).
*
* ```typescript
* shortNameForFolder($rdf.namedNode('http://example.com/some/folder/'))
* // 'folder'
*
* shortNameForFolder($rdf.namedNode('http://example.com/some/folder'))
* // 'folder'
*
* shortNameForFolder($rdf.namedNode('http://example.com/'))
* // 'example.com'
*
* shortNameForFolder($rdf.namedNode('http://example.com'))
* // 'example.com'
*
* shortNameForFolder($rdf.namedNode('http://'))
* // '/'
* ```
*
* It also works with relative URLs:
* ```typescript
* shortNameForFolder($rdf.namedNode('../folder/'))
* // 'folder'
* ```
*
* @param x RDF Node for the folder URL
* @returns Short name for the folder
*/
export function shortNameForFolder (x: NamedNode): string {
let str = x.uri
// Strip the trailing slash
if (str.slice(-1) === '/') {
str = str.slice(0, -1)
}
// Remove the path if present, keeping only the part
// after the last slash.
const slash = str.lastIndexOf('/')
if (slash >= 0) {
str = str.slice(slash + 1)
}
// Return the folder's filename, or '/' if nothing found
// (but see https://github.com/solidos/solid-ui/issues/196
// regarding whether this happens at the domain root or
// not)
return str || '/'
}
/**
* A wrapper that retrieves ACL data and uses it
* to render an [[AccessController]] component.
* Presumably the '5' is a version number of some sort,
* but all we know is it was already called ACLControlBox5
* when it was introduced into solid-ui in
* https://github.com/solidos/solid-ui/commit/948b874bd93e7bf5160e6e224821b888f07d15f3#diff-4192a29f38a0ababd563b36b47eba5bbR54
*/
export function ACLControlBox5 (
subject: NamedNode,
context: DataBrowserContext,
noun: string,
kb: Store
): HTMLElement {
const dom = context.dom
const doc = subject.doc() // The ACL is actually to the doc describing the thing
const container = dom.createElement('div')
container.setAttribute('style', style.aclControlBoxContainer)
const header = container.appendChild(dom.createElement('h1'))
header.textContent = `Sharing for ${noun} ${utils.label(subject)}`
header.setAttribute('style', style.aclControlBoxHeader)
const status = container.appendChild(dom.createElement('div'))
status.setAttribute('style', style.aclControlBoxStatus)
try {
loadController(doc, kb, subject, noun, context, dom, status)
.then(controller => container.appendChild(controller.render()))
} catch (error) {
status.innerText = error
}
return container
}
async function loadController (
doc: NamedNode,
kb: Store,
subject: NamedNode,
noun: string,
context: DataBrowserContext,
dom: HTMLDocument,
status: HTMLElement
): Promise<AccessController> {
return new Promise((resolve, reject) => getACLorDefault(doc, async (
ok,
isDirectACL,
targetDoc,
targetACLDoc,
defaultHolder,
defaultACLDoc
) => {
if (!ok) {
return reject(new Error(`Error reading ${isDirectACL ? '' : ' default '}ACL. status ${targetDoc}: ${targetACLDoc}`))
}
const targetDirectory = getDirectory(targetDoc as NamedNode)
const targetIsProtected = isStorage(targetDoc as NamedNode, targetACLDoc as NamedNode, kb) || hasProtectedAcl(targetDoc as NamedNode)
if (!targetIsProtected && targetDirectory) {
try {
const prospectiveDefaultHolder = await getProspectiveHolder(targetDirectory)
return resolve(getController(prospectiveDefaultHolder))
} catch (error) {
// No need to show this error in status, but good to warn about it in console
warn(error)
}
}
return resolve(getController())
function getController (prospectiveDefaultHolder?: NamedNode) {
return new AccessController(subject, noun, context, status, targetIsProtected, targetDoc as NamedNode, targetACLDoc as NamedNode, defaultHolder as NamedNode,
defaultACLDoc as NamedNode, prospectiveDefaultHolder, kb, dom as HTMLDocument)
}
}))
}
function getDirectory (doc: NamedNode): string | null {
const str = doc.uri.split('#')[0]
const p = str.slice(0, -1).lastIndexOf('/')
const q = str.indexOf('//')
return (q >= 0 && p < q + 2) || p < 0 ? null : str.slice(0, p + 1)
}
function isStorage (doc: NamedNode, aclDoc: NamedNode, store: Store): boolean {
// @@ TODO: The methods used for targetIsStorage are HACKs - it should not be relied upon, and work is
// @@ underway to standardize a behavior that does not rely upon this hack
// @@ hopefully fixed as part of https://github.com/solidos/data-interoperability-panel/issues/10
return store.holds(doc, ns.rdf('type'), ns.space('Storage'), aclDoc)
}
function hasProtectedAcl (targetDoc: NamedNode): boolean {
// @@ TODO: This is hacky way of knowing whether or not a certain ACL file can be removed
// Hopefully we'll find a better, standardized solution to this - https://github.com/solidos/specification/issues/37
return targetDoc.uri === targetDoc.site().uri
}
/** @internal */
export function setGlobalWindow (window: Window) {
global = window
}