-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrant.html
More file actions
298 lines (236 loc) · 7.51 KB
/
Copy pathgrant.html
File metadata and controls
298 lines (236 loc) · 7.51 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
html, body, main {
font-family: Verdana;
height: 100%;
}
main {
display: flex;
justify-content: center;
align-items: center;
}
.content {
padding: 20px;
border: 1px solid #ccc;
}
.header-text {
font-size: 18px;
margin-bottom: 15px;
}
.perm-list {
max-height: 70vh;
overflow-y: scroll;
}
.perm-list-item {
margin: 10px;
padding: 10px;
border: 1px solid #ccc;
}
.gemdrive-button {
border: transparent;
font-size: 20px;
padding: .5em 1em;
cursor: pointer;
color: white;
border-radius: 4px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
}
.button-confirm {
background: rgb(28, 184, 65);
/* this is a green */
}
.button-cancel {
background: rgb(202, 60, 60);
/* this is a maroon */
}
.button-row {
display: flex;
justify-content: center;
}
.button-row__button {
margin: 10px;
}
#email-container {
font-weight: bold;
}
.perms-list {
list-style-type: none;
}
.path {
padding: .5em 0;
font-weight: bold;
font-size: 1.2em;
font-family: Courier New;
}
.success, .fail {
padding: 20px;
font-size: 22px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<main>
<div class='content'>
<div class='header-text'>
<span id='email-container'></span>
requests access to the following:
</div>
<div class='perm-list'></div>
<div class='button-row'>
<button class='gemdrive-button button-confirm button-row__button'>Approve</button>
<button class='gemdrive-button button-cancel button-row__button'>Deny</button>
</div>
</div>
<div class='success' hidden>
Successfully updated permissions. You may close this tab.
</div>
<div class='fail' hidden>
Failed to update permissions. You may close this tab.
</div>
</main>
<script>
const contentEl = document.querySelector('.content');
const successEl = document.querySelector('.success');
const failEl = document.querySelector('.fail');
const permListEl = document.querySelector('.perm-list');
const emailEl = document.querySelector('#email-container');
const authorizeBtnEl = document.querySelector('.button-confirm');
const denyBtnEl = document.querySelector('.button-cancel');
let dialog = null;
const urlParams = new URLSearchParams(window.location.search);
const scope = urlParams.get('scope');
const email = urlParams.get('email');
//history.pushState(null, '', window.location.pathname);
emailEl.innerText = email;
const allPerms = parsePermsFromScope(scope);
if (!allPathsSet(allPerms)) {
authorizeBtnEl.setAttribute('disabled', true);
authorizeBtnEl.setAttribute('title', "Must make selections");
}
for (let i = 0; i < allPerms.length; i++) {
const permParams = allPerms[i];
const item = PermListItem(i, permParams);
permListEl.appendChild(item.dom);
item.dom.addEventListener('perms-changed', (e) => {
const { key, perm } = e.detail;
allPerms[key].perm = perm;
});
}
authorizeBtnEl.addEventListener('click', (e) => {
const driveUri = window.location.href.split('/.gemdrive/auth/grant')[0];
const grantUrl = driveUri + '/.gemdrive/auth/addPerms';
fetch(grantUrl, {
method: 'POST',
body: JSON.stringify({
email,
requests: allPerms,
}),
})
.then(response => {
if (response.status === 200) {
return response.text();
}
else {
throw new Error("Failed");
}
})
.then(text => {
contentEl.setAttribute('hidden', true);
successEl.removeAttribute('hidden');
})
.catch(e => {
contentEl.setAttribute('hidden', true);
failEl.removeAttribute('hidden');
console.error(e);
});
});
denyBtnEl.addEventListener('click', (e) => {
contentEl.setAttribute('hidden', true);
failEl.removeAttribute('hidden');
});
function allPathsSet(perms) {
return perms
.map(p => p.path !== undefined && p.path.length > 0)
.reduce((acc, curr) => {
return acc && curr;
}, true);
}
function parsePermsFromScope(scope) {
const allPerms = [];
const items = scope.split(' ');
for (const item of items) {
const perms = {};
const params = item.split(';');
for (const param of params) {
const parts = param.split('=');
const key = parts[0];
const value = parts[1];
perms[key] = value.replace(/\[\]/g, ' ');
}
allPerms.push(perms);
}
return allPerms;
}
function PermListItem(key, permParams) {
const dom = document.createElement('div');
dom.classList.add('perm-list-item');
const pathContainerEl = document.createElement('div');
dom.appendChild(pathContainerEl);
const itemTypeText = permParams.type === 'dir' ? "Directory" : "File";
const typeLabelEl = document.createElement('div');
pathContainerEl.appendChild(typeLabelEl);
typeLabelEl.innerText = itemTypeText;
if (permParams.hint) {
typeLabelEl.innerText += ": " + permParams.hint;
}
const pathEl = document.createElement('div');
pathEl.classList.add('path');
pathContainerEl.appendChild(pathEl);
if (permParams.path) {
pathEl.innerText = permParams.path;
}
const permsEl = document.createElement('span');
dom.appendChild(permsEl);
permsEl.classList.add('perms-list');
const readCheckEl = document.createElement('input');
permsEl.appendChild(readCheckEl);
readCheckEl.setAttribute('type', 'checkbox');
readCheckEl.setAttribute('checked', true);
readCheckEl.setAttribute('disabled', true);
const readLabelEl = document.createElement('span');
permsEl.appendChild(readLabelEl);
readLabelEl.innerText = " Read ";
const writeCheckEl = document.createElement('input');
permsEl.appendChild(writeCheckEl);
writeCheckEl.setAttribute('type', 'checkbox');
const writeLabelEl = document.createElement('span');
permsEl.appendChild(writeLabelEl);
writeLabelEl.innerText = " Write";
if (permParams.perm === 'write') {
writeCheckEl.setAttribute('checked', true);
}
writeCheckEl.addEventListener('change', (e) => {
dom.dispatchEvent(new CustomEvent('perms-changed', {
bubbles: true,
detail: {
key,
perm: e.target.checked ? 'write' : 'read',
},
}));
});
function onPathChange(newPath) {
pathEl.innerText = newPath;
}
return {
dom,
onPathChange,
};
}
</script>
</body>
</html>