1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-09 18:25:58 +01:00

Add dialog for generating tokens.

This commit is contained in:
Juliusz Chroboczek 2023-05-03 18:37:52 +02:00
parent 1afd995844
commit 2c852206bd
3 changed files with 102 additions and 1 deletions

View file

@ -1337,3 +1337,7 @@ header .collapse:hover {
margin-left: 10px; margin-left: 10px;
margin-right: 10px; margin-right: 10px;
} }
#invite-dialog {
background-color: #eee;
}

View file

@ -281,6 +281,21 @@
</div> </div>
</div> </div>
<dialog id="invite-dialog">
<form method="dialog">
<label for="invite-username">Username (optional):</label>
<input id="invite-username" type="text"/>
<br>
<label for="invite-not-before">Not before:</label>
<input id="invite-not-before" type="datetime-local"/>
<br>
<label for="invite-expires">Expires:</label>
<input id="invite-expires" type="datetime-local"/>
<br>
<button id="invite-cancel" value="cancel" type="button">Cancel</button>
<button value="invite" value="invite">Invite</button>
</dialog>
<script src="/protocol.js" defer></script> <script src="/protocol.js" defer></script>
<script src="/external/toastify/toastify.js" defer></script> <script src="/external/toastify/toastify.js" defer></script>
<script src="/external/contextual/contextual.js" defer></script> <script src="/external/contextual/contextual.js" defer></script>

View file

@ -2094,6 +2094,88 @@ function stringCompare(a, b) {
return 0 return 0
} }
/**
* @param {string} v
*/
function dateFromInput(v) {
let d = new Date(v);
if(d.toString() === 'Invalid Date')
throw new Error('Invalid date');
return d;
}
/**
* @param {Date} d
*/
function dateToInput(d) {
let dd = new Date(d);
dd.setMinutes(dd.getMinutes() - dd.getTimezoneOffset());
return dd.toISOString().slice(0, -1);
}
function inviteMenu() {
let d = /** @type {HTMLDialogElement} */
(document.getElementById('invite-dialog'));
if(!('HTMLDialogElement' in window) || !d.showModal) {
makeToken();
return;
}
d.returnValue = '';
let c = getButtonElement('invite-cancel');
c.onclick = function(e) { d.close('cancel'); };
let u = getInputElement('invite-username');
u.value = '';
let now = new Date();
now.setMilliseconds(0);
now.setSeconds(0);
let nb = getInputElement('invite-not-before');
nb.min = dateToInput(now);
let ex = getInputElement('invite-expires');
let expires = new Date(now);
expires.setDate(expires.getDate() + 2);
ex.min = dateToInput(expires);
ex.value = dateToInput(expires);
d.showModal();
}
document.getElementById('invite-dialog').onclose = function(e) {
if(!(this instanceof HTMLDialogElement))
throw new Error('Unexpected type for this');
let dialog = /** @type {HTMLDialogElement} */(this);
if(dialog.returnValue !== 'invite')
return;
let u = getInputElement('invite-username');
let username = u.value.trim() || null;
let nb = getInputElement('invite-not-before');
let notBefore = null;
if(nb.value) {
try {
notBefore = dateFromInput(nb.value);
} catch(e) {
displayError(`Couldn't parse ${nb.value}: ${e}`);
return;
}
}
let ex = getInputElement('invite-expires');
let expires = null;
if(ex.value) {
try {
expires = dateFromInput(ex.value);
} catch(e) {
displayError(`Couldn't parse ${nb.value}: ${e}`);
return;
}
}
let template = {}
if(username)
template.username = username;
if(notBefore)
template['not-before'] = notBefore;
if(expires)
template.expires = expires;
makeToken(template);
};
/** /**
* @param {HTMLElement} elt * @param {HTMLElement} elt
*/ */
@ -2122,7 +2204,7 @@ function userMenu(elt) {
if(serverConnection.version !== "1" && if(serverConnection.version !== "1" &&
serverConnection.permissions.indexOf('token') >= 0) { serverConnection.permissions.indexOf('token') >= 0) {
items.push({label: 'Invite user', onClick: () => { items.push({label: 'Invite user', onClick: () => {
makeToken(); inviteMenu();
}}); }});
} }
if(serverConnection.permissions.indexOf('present') >= 0 && canFile()) if(serverConnection.permissions.indexOf('present') >= 0 && canFile())