trilium/src/public/javascripts/services/protected_session.js

186 lines
5.2 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
2018-04-08 21:25:35 +08:00
import noteDetailService from './note_detail.js';
import utils from './utils.js';
import server from './server.js';
import protectedSessionHolder from './protected_session_holder.js';
2018-03-26 09:29:35 +08:00
import infoService from "./info.js";
const $dialog = $("#protected-session-password-dialog");
const $passwordForm = $("#protected-session-password-form");
const $password = $("#protected-session-password");
const $noteDetailWrapper = $("#note-detail-wrapper");
const $protectButton = $("#protect-button");
const $unprotectButton = $("#unprotect-button");
const $protectedSessionOnButton = $("#protected-session-on");
const $protectedSessionOffButton = $("#protected-session-off");
let protectedSessionDeferred = null;
async function enterProtectedSession() {
if (!protectedSessionHolder.isProtectedSessionAvailable()) {
await ensureProtectedSession(true, true);
}
}
async function leaveProtectedSession() {
if (protectedSessionHolder.isProtectedSessionAvailable()) {
utils.reloadApp();
}
}
/** returned promise resolves with true if new protected session was established, false if no action was necessary */
function ensureProtectedSession(requireProtectedSession, modal) {
const dfd = $.Deferred();
if (requireProtectedSession && !protectedSessionHolder.isProtectedSessionAvailable()) {
// using deferred instead of promise because it allows resolving from outside
protectedSessionDeferred = dfd;
if (treeService.getCurrentNode().data.isProtected) {
$noteDetailWrapper.hide();
}
$dialog.toggleClass("modalless", !modal);
$dialog.modal();
}
else {
dfd.resolve(false);
}
return dfd.promise();
}
async function setupProtectedSession() {
const password = $password.val();
$password.val("");
const response = await enterProtectedSessionOnServer(password);
if (!response.success) {
2018-03-26 09:29:35 +08:00
infoService.showError("Wrong password.");
return;
2017-11-05 06:18:55 +08:00
}
protectedSessionHolder.setProtectedSessionId(response.protectedSessionId);
2017-11-11 11:55:19 +08:00
$dialog.modal("hide");
await noteDetailService.reload();
treeService.reload();
if (protectedSessionDeferred !== null) {
ensureDialogIsClosed($dialog, $password);
$noteDetailWrapper.show();
2017-11-15 12:01:23 +08:00
protectedSessionDeferred.resolve(true);
protectedSessionDeferred = null;
$protectedSessionOnButton.addClass('active');
$protectedSessionOffButton.removeClass('active');
2017-11-09 11:33:08 +08:00
}
infoService.showMessage("Protected session has been started.");
}
function ensureDialogIsClosed() {
// this may fal if the dialog has not been previously opened (not sure if still true with Bootstrap modal)
try {
$dialog.modal('hide');
}
catch (e) {}
$password.val('');
}
2017-11-11 11:55:19 +08:00
async function enterProtectedSessionOnServer(password) {
return await server.post('login/protected', {
password: password
});
}
2017-11-11 11:55:19 +08:00
async function protectNoteAndSendToServer() {
if (noteDetailService.getCurrentNote().isProtected) {
return;
}
await ensureProtectedSession(true, true);
2018-04-08 21:25:35 +08:00
const note = noteDetailService.getCurrentNote();
2018-03-26 11:25:17 +08:00
note.isProtected = true;
2018-04-08 21:25:35 +08:00
await noteDetailService.saveNote(note);
2018-03-26 11:25:17 +08:00
treeService.setProtected(note.noteId, note.isProtected);
noteDetailService.setNoteBackgroundIfProtected(note);console.log(note);
}
async function unprotectNoteAndSendToServer() {
const currentNote = noteDetailService.getCurrentNote();
if (!currentNote.isProtected) {
infoService.showAndLogError(`Note ${currentNote.noteId} is not protected`);
return;
}
if (!protectedSessionHolder.isProtectedSessionAvailable()) {
console.log("Unprotecting notes outside of protected session is not allowed.");
// the reason is that it's not easy to handle even with ensureProtectedSession,
// because we would first have to make sure the note is loaded and only then unprotect
// we used to have a bug where we would overwrite the previous note with unprotected content.
return;
}
currentNote.isProtected = false;
await noteDetailService.saveNote(currentNote);
treeService.setProtected(currentNote.noteId, currentNote.isProtected);
noteDetailService.setNoteBackgroundIfProtected(currentNote);
}
async function protectSubtree(noteId, protect) {
await ensureProtectedSession(true, true);
2017-11-23 09:46:42 +08:00
2018-04-02 08:33:10 +08:00
await server.put('notes/' + noteId + "/protect/" + (protect ? 1 : 0));
2017-11-15 13:04:26 +08:00
2018-03-26 09:29:35 +08:00
infoService.showMessage("Request to un/protect sub tree has finished successfully");
2017-11-15 13:04:26 +08:00
treeService.reload();
2018-04-08 21:25:35 +08:00
noteDetailService.reload();
}
2017-11-15 13:04:26 +08:00
$passwordForm.submit(() => {
setupProtectedSession();
2017-11-09 11:33:08 +08:00
return false;
});
2017-11-09 11:33:08 +08:00
// this doesn't work, event is not triggered :/
$dialog.on("show.bs.modal", e => function() {
if ($(this).hasClass("modalless")) {
// return "stolen" focus to tree
treeService.getCurrentNode().setFocus();
}
else {
$password.focus();
}
});
$protectButton.click(protectNoteAndSendToServer);
$unprotectButton.click(unprotectNoteAndSendToServer);
$dialog.on("shown.bs.modal", e => $password.focus());
export default {
ensureProtectedSession,
protectSubtree,
ensureDialogIsClosed,
enterProtectedSession,
leaveProtectedSession
};