Add setAttribute/getAttribute in secure iframes. Store attributes in settings

pull/1/head
yflory 7 years ago
parent fe8b2c2556
commit 04a04aef2b

@ -112,6 +112,7 @@ define([
var updateIndentSettings = function () {
if (!metadataMgr) { return; }
var data = metadataMgr.getPrivateData().settings;
data = data.codemirror || {};
var indentUnit = data[indentKey];
var useTabs = data[useTabsKey];
setIndentation(
@ -255,7 +256,7 @@ define([
readOnly = metadataMgr.getPrivateData().readOnly;
var titleCfg = { getHeadingText: CodeMirror.getHeadingText };
Title = common.createTitle(titleCfg, config.onLocal, common, metadataMgr);
Title = common.createTitle(titleCfg, config.onLocal, common);
var configTb = {
displayed: ['title', 'useradmin', 'spinner', 'share', 'userlist', 'newpad', 'limit'],

@ -194,11 +194,14 @@ define([
return store.getProfile().avatar;
}
};
common.getDisplayName = function () {
common.getDisplayName = function (cb) {
var name;
if (getProxy()) {
return getProxy()[common.displayNameKey] || '';
name = getProxy()[common.displayNameKey];
}
return '';
name = name || '';
if (typeof cb === "function") { cb(null, name); }
return name;
};
common.getAccountName = function () {
return localStorage[common.userNameKey];
@ -479,11 +482,22 @@ define([
var href = getRelativeHref(window.location.href);
getStore().setPadAttribute(href, attr, value, cb);
};
common.setDisplayName = function (value, cb) {
if (getProxy()) {
getProxy()[common.displayNameKey] = value;
}
if (typeof cb === "function") { cb(); }
};
common.setAttribute = function (attr, value, cb) {
getStore().set(["cryptpad", attr].join('.'), value, function (err, data) {
getStore().setAttribute(attr, value, function (err, data) {
if (cb) { cb(err, data); }
});
};
/*common.setAttribute = function (attr, value, cb) {
getStore().set(["cryptpad", attr].join('.'), value, function (err, data) {
if (cb) { cb(err, data); }
});
};*/
common.setLSAttribute = function (attr, value) {
localStorage[attr] = value;
};
@ -494,10 +508,15 @@ define([
getStore().getPadAttribute(href, attr, cb);
};
common.getAttribute = function (attr, cb) {
getStore().get(["cryptpad", attr].join('.'), function (err, data) {
getStore().getAttribute(attr, function (err, data) {
cb(err, data);
});
};
/*common.getAttribute = function (attr, cb) {
getStore().get(["cryptpad", attr].join('.'), function (err, data) {
cb(err, data);
});
};*/
/* this returns a reference to your proxy. changing it will change your drive.
*/
@ -661,11 +680,12 @@ define([
};
// STORAGE: Display Name
common.getLastName = function (cb) {
common.getAttribute('username', function (err, userName) {
common.getLastName = common.getDisplayName;
/* function (cb) {
common.getDisplayName(function (err, userName) {
cb(err, userName);
});
};
};*/
var _onDisplayNameChanged = [];
common.onDisplayNameChanged = function (h) {
if (typeof(h) !== "function") { return; }

@ -61,8 +61,46 @@ define([
cb(void 0, res);
};
ret.setPadAttribute = filesOp.setAttribute;
ret.getPadAttribute = filesOp.getAttribute;
var getAttributeObject = function (attr) {
if (typeof attr === "string") {
console.error('DEPRECATED: use setAttribute with an array, not a string');
return {
obj: storeObj.settings,
key: attr
};
}
if (!Array.isArray(attr)) { throw new Error("Attribute must be string or array"); }
if (attr.length === 0) { throw new Error("Attribute can't be empty"); }
var obj = storeObj.settings;
attr.forEach(function (el, i) {
if (i === attr.length-1) { return; }
if (!obj[el]) {
obj[el] = {};
}
else if (typeof obj[el] !== "object") { throw new Error("Wrong attribute"); }
obj = obj[el];
});
return {
obj: obj,
key: attr[attr.length-1]
};
};
ret.setAttribute = function (attr, value, cb) {
try {
var object = getAttributeObject(attr);
object.obj[object.key] = value;
} catch (e) { return void cb(e); }
cb();
};
ret.getAttribute = function (attr, cb) {
var object;
try {
object = getAttributeObject(attr);
} catch (e) { return void cb(e); }
cb(null, object.obj[object.key]);
};
ret.setPadAttribute = filesOp.setPadAttribute;
ret.getPadAttribute = filesOp.getPadAttribute;
ret.getIdFromHref = filesOp.getIdFromHref;
ret.getDrive = function (key, cb) {

@ -64,5 +64,37 @@ define([], function () {
Cryptpad.feedback('Migrate-2', true);
userObject.version = version = 2;
}
// Migration 3: global attributes from root to 'settings' subobjects
var migrateAttributes = function () {
var drawer = 'cryptpad.userlist-drawer';
var polls = 'cryptpad.hide_poll_text';
var indentKey = 'indentUnit';
var useTabsKey = 'indentWithTabs';
var settings = userObject.settings = userObject.settings || {};
if (settings[indentKey] || settings[useTabsKey]) {
settings.codemirror = settings.codemirror || {};
settings.codemirror.indentUnit = settings[indentKey];
settings.codemirror.indentWithTabs = settings[useTabsKey];
delete settings[indentKey];
delete settings[useTabsKey];
}
if (userObject[drawer]) {
settings.toolbar = settings.toolbar || {};
settings.toolbar['userlist-drawer'] = userObject[drawer];
delete userObject[drawer];
}
if (userObject[polls]) {
settings.poll = settings.poll || {};
settings.poll['hide-text'] = userObject[polls];
delete userObject[polls];
}
};
if (version < 3) {
migrateAttributes();
Cryptpad.feedback('Migrate-3', true);
userObject.version = version = 3;
}
};
});

@ -115,7 +115,7 @@ define([
});
sframeChan.on('Q_SETTINGS_SET_DISPLAY_NAME', function (newName, cb) {
Cryptpad.setAttribute('username', newName, function (err) {
Cryptpad.setDisplayName(newName, function (err) {
if (err) {
console.log("Couldn't set username");
console.error(err);
@ -220,6 +220,20 @@ define([
});
});
sframeChan.on('Q_GET_ATTRIBUTE', function (data, cb) {
Cryptpad.getAttribute(data.key, function (e, data) {
cb({
error: e,
data: data
});
});
});
sframeChan.on('Q_SET_ATTRIBUTE', function (data, cb) {
Cryptpad.setAttribute(data.key, data.value, function (e) {
cb({error:e});
});
});
var onFileUpload = function (sframeChan, data, cb) {
var sendEvent = function (data) {

@ -3,32 +3,24 @@ define(['jquery'], function ($) {
module.create = function (cfg, onLocal, Common, metadataMgr) {
var exp = {};
var metadataMgr = Common.getMetadataMgr();
var sframeChan = Common.getSframeChannel();
var titleUpdated;
exp.defaultTitle = Common.getDefaultTitle();
exp.defaultTitle = metadataMgr.getMetadata().defaultTitle;
exp.title = document.title;
cfg = cfg || {};
var getHeadingText = cfg.getHeadingText || function () { return; };
/* var updateLocalTitle = function (newTitle) {
console.error(newTitle);
exp.title = newTitle;
onLocal();
if (typeof cfg.updateLocalTitle === "function") {
cfg.updateLocalTitle(newTitle);
} else {
document.title = newTitle;
}
};*/
var $title;
exp.setToolbar = function (toolbar) {
$title = toolbar && toolbar.title;
};
exp.getTitle = function () { return exp.title; };
var isDefaultTitle = exp.isDefaultTitle = function (){return exp.title === exp.defaultTitle;};
var suggestTitle = exp.suggestTitle = function (fallback) {
@ -40,32 +32,25 @@ define(['jquery'], function ($) {
}
};
/*var renameCb = function (err, newTitle) {
if (err) { return; }
onLocal();
//updateLocalTitle(newTitle);
};*/
// update title: href is optional; if not specified, we use window.location.href
exp.updateTitle = function (newTitle, cb) {
cb = cb || $.noop;
if (newTitle === exp.title) { return; }
Common.updateTitle(newTitle, cb);
metadataMgr.updateTitle(newTitle);
titleUpdated = cb;
};
// TODO not needed?
/*exp.updateDefaultTitle = function (newDefaultTitle) {
exp.defaultTitle = newDefaultTitle;
if (!$title) { return; }
$title.find('input').attr("placeholder", exp.defaultTitle);
};*/
metadataMgr.onChange(function () {
var md = metadataMgr.getMetadata();
$title.find('span.cp-toolbar-title-value').text(md.title || md.defaultTitle);
$title.find('input').val(md.title || md.defaultTitle);
exp.title = md.title;
//exp.updateTitle(md.title || md.defaultTitle);
});
metadataMgr.onTitleChange(function (title) {
sframeChan.query('Q_SET_PAD_TITLE_IN_DRIVE', title, function (err) {
if (err) { return; }
if (titleUpdated) { titleUpdated(undefined, title); }
});
});
exp.getTitleConfig = function () {

@ -46,12 +46,6 @@ define([
return ctx.cpNfInner.metadataMgr.getPrivateData().accountName;
};
var titleUpdated;
funcs.updateTitle = function (title, cb) {
ctx.metadataMgr.updateTitle(title);
titleUpdated = cb;
};
// UI
funcs.createUserAdminMenu = UI.createUserAdminMenu;
funcs.displayAvatar = UI.displayAvatar;
@ -65,10 +59,7 @@ define([
// Title module
funcs.createTitle = Title.create;
funcs.getDefaultTitle = function () {
if (!ctx.cpNfInner) { throw new Error("cpNfInner is not ready!"); }
return ctx.cpNfInner.metadataMgr.getMetadata().defaultTitle;
};
// Misc
funcs.setDisplayName = function (name, cb) {
ctx.sframeChan.query('Q_SETTINGS_SET_DISPLAY_NAME', name, function (err) {
@ -76,6 +67,7 @@ define([
});
};
// Window
funcs.logout = function (cb) {
ctx.sframeChan.query('Q_LOGOUT', null, function (err) {
if (cb) { cb(err); }
@ -92,6 +84,7 @@ define([
});
};
// Store
funcs.sendAnonRpcMsg = function (msg, content, cb) {
ctx.sframeChan.query('Q_ANON_RPC_MESSAGE', {
msg: msg,
@ -128,6 +121,21 @@ define([
}, cb);
};
funcs.getAttribute = function (key, cb) {
ctx.sframeChan.query('Q_GET_ATTRIBUTE', {
key: key
}, function (err, res) {
cb (err || res.error, res.data);
});
};
funcs.setAttribute = function (key, value, cb) {
cb = cb || $.noop;
ctx.sframeChan.query('Q_SET_ATTRIBUTE', {
key: key,
value: value
}, cb);
};
// Files
funcs.uploadFile = function (data, cb) {
ctx.sframeChan.query('Q_UPLOAD_FILE', data, cb);
@ -361,12 +369,6 @@ define([
// CpNfInner.start() should be here....
}).nThen(function () {
ctx.metadataMgr = MetadataMgr.create(ctx.sframeChan);
ctx.metadataMgr.onTitleChange(function (title) {
ctx.sframeChan.query('Q_SET_PAD_TITLE_IN_DRIVE', title, function (err) {
if (err) { return; }
if (titleUpdated) { titleUpdated(undefined, title); }
});
});
ctx.sframeChan.on('EV_RT_CONNECT', function () { CommonRealtime.setConnectionState(true); });
ctx.sframeChan.on('EV_RT_DISCONNECT', function () { CommonRealtime.setConnectionState(false); });

@ -85,7 +85,9 @@ define({
// Send the new settings to the inner iframe when they are changed in the proxy
'EV_SETTINGS_UPDATE': true,
// Get and set pad attributes stored in the drive from the inner iframe
// Get and set (pad) attributes stored in the drive from the inner iframe
'Q_GET_ATTRIBUTE': true,
'Q_SET_ATTRIBUTE': true,
'Q_GET_PAD_ATTRIBUTE': true,
'Q_SET_PAD_ATTRIBUTE': true,

@ -351,7 +351,7 @@ define([
$content.css('margin-top', h+'px');
});
$closeIcon.click(function () {
Cryptpad.setAttribute('userlist-drawer', false);
Cryptpad.setAttribute(['toolbar', 'userlist-drawer'], false);
hide();
});
$button.click(function () {
@ -359,11 +359,11 @@ define([
if (visible) { hide(); }
else { show(); }
visible = !visible;
Cryptpad.setAttribute('userlist-drawer', visible);
Cryptpad.setAttribute(['toolbar', 'userlist-drawer'], visible);
Cryptpad.feedback(visible?'USERLIST_SHOW': 'USERLIST_HIDE');
});
Cryptpad.getAttribute('userlist-drawer', function (err, val) {
Cryptpad.getAttribute(['toolbar', 'userlist-drawer'], function (err, val) {
if (val === false || mobile) { return void hide(); }
show();
});
@ -840,7 +840,7 @@ define([
if (newName === null && typeof(lastName) === "string") { return; }
if (newName === null) { newName = ''; }
else { Cryptpad.feedback('NAME_CHANGED'); }
Cryptpad.setAttribute('username', newName, function (err) {
Cryptpad.setDisplayName(newName, function (err) {
if (err) {
console.log("Couldn't set username");
console.error(err);

@ -256,17 +256,12 @@ define([
};
var initUserList = function (toolbar, config) {
// TODO clean comments
if (config.metadataMgr) { /* && config.userList.list && config.userList.userNetfluxId) {*/
//var userList = config.userList.list;
//userList.change.push
if (config.metadataMgr) {
var metadataMgr = config.metadataMgr;
metadataMgr.onChange(function () {
if (metadataMgr.isConnected()) {toolbar.connected = true;}
if (!toolbar.connected) { return; }
//if (config.userList.data) {
updateUserList(toolbar, config);
//}
});
}
};
@ -333,7 +328,7 @@ define([
$content.css('margin-top', h+'px');
});
$closeIcon.click(function () {
//Cryptpad.setAttribute('userlist-drawer', false); TODO iframe
Common.setAttribute(['toolbar', 'userlist-drawer'], false);
hide();
});
$button.click(function () {
@ -341,16 +336,14 @@ define([
if (visible) { hide(); }
else { show(); }
visible = !visible;
// TODO iframe
//Cryptpad.setAttribute('userlist-drawer', visible);
Common.setAttribute(['toolbar', 'userlist-drawer'], visible);
Common.feedback(visible?'USERLIST_SHOW': 'USERLIST_HIDE');
});
show();
// TODO iframe
/*Cryptpad.getAttribute('userlist-drawer', function (err, val) {
Common.getAttribute(['toolbar', 'userlist-drawer'], function (err, val) {
if (val === false || mobile) { return void hide(); }
show();
});*/
});
return $container;
};

@ -142,14 +142,14 @@ define([
if (type === 'name') { return data.filename; }
return data.filename || data.title || NEW_FILE_NAME;
};
exp.getAttribute = function (href, attr, cb) {
exp.getPadAttribute = function (href, attr, cb) {
cb = cb || $.noop;
var id = exp.getIdFromHref(href);
if (!id) { return void cb(null, undefined); }
var data = getFileData(id);
cb(null, clone(data[attr]));
};
exp.setAttribute = function (href, attr, value, cb) {
exp.setPadAttribute = function (href, attr, value, cb) {
cb = cb || $.noop;
var id = exp.getIdFromHref(href);
if (!id) { return void cb("E_INVAL_HREF"); }

@ -2774,7 +2774,7 @@ define([
myUserNameTemp = myUserNameTemp.substr(0, 32);
}
var myUserName = myUserNameTemp;
Cryptpad.setAttribute('username', myUserName, function (err) {
Cryptpad.setDisplayName(myUserName, function (err) {
if (err) {
logError("Couldn't set username", err);
return;

@ -514,7 +514,7 @@ define([
readOnly = metadataMgr.getPrivateData().readOnly;
console.log('onInit');
var titleCfg = { getHeadingText: getHeadingText };
Title = common.createTitle(titleCfg, realtimeOptions.onLocal, common, metadataMgr);
Title = common.createTitle(titleCfg, realtimeOptions.onLocal, common);
var configTb = {
displayed: ['userlist', 'title', 'useradmin', 'spinner', 'newpad', 'share', 'limit'],
title: Title.getTitleConfig(),

@ -21,7 +21,7 @@ define([
$(function () {
var HIDE_INTRODUCTION_TEXT = "hide_poll_text";
var HIDE_INTRODUCTION_TEXT = "hide-text";
var defaultName;
var secret = Cryptpad.getSecrets();
@ -782,10 +782,10 @@ var create = function (info) {
.on('disconnect', disconnect)
.on('reconnect', reconnect);
Cryptpad.getAttribute(HIDE_INTRODUCTION_TEXT, function (e, value) {
Cryptpad.getAttribute(['poll', HIDE_INTRODUCTION_TEXT], function (e, value) {
if (e) { console.error(e); }
if (!value) {
Cryptpad.setAttribute(HIDE_INTRODUCTION_TEXT, "1", function (e) {
Cryptpad.setAttribute(['poll', HIDE_INTRODUCTION_TEXT], "1", function (e) {
if (e) { console.error(e); }
});
showHelp(true);

@ -131,33 +131,6 @@ define([
});
};
/*
var addDisplayName = function ($container) {
var $block = $('<div>', {id: DISPLAYNAME_ID}).appendTo($container);
var getValue = function (cb) {
Cryptpad.getLastName(function (err, name) {
if (err) { return void console.error(err); }
cb(name);
});
};
if (APP.readOnly) {
var $span = $('<span>', {'class': DISPLAYNAME_ID}).appendTo($block);
getValue(function (value) {
$span.text(value);
});
return;
}
var setValue = function (value, cb) {
Cryptpad.setAttribute('username', value, function (err) {
cb(err);
});
};
var placeholder = Messages.anonymous;
var rt = Cryptpad.getStore().getProxy().info.realtime;
createEditableInput($block, DISPLAYNAME_ID, placeholder, 32, getValue, setValue, rt);
};
*/
/* jshint ignore:start */
var isFriend = function (proxy, edKey) {
var friends = Cryptpad.find(proxy, ['friends']);

Loading…
Cancel
Save