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

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

@ -61,8 +61,46 @@ define([
cb(void 0, res); cb(void 0, res);
}; };
ret.setPadAttribute = filesOp.setAttribute; var getAttributeObject = function (attr) {
ret.getPadAttribute = filesOp.getAttribute; 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.getIdFromHref = filesOp.getIdFromHref;
ret.getDrive = function (key, cb) { ret.getDrive = function (key, cb) {

@ -64,5 +64,37 @@ define([], function () {
Cryptpad.feedback('Migrate-2', true); Cryptpad.feedback('Migrate-2', true);
userObject.version = version = 2; 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) { sframeChan.on('Q_SETTINGS_SET_DISPLAY_NAME', function (newName, cb) {
Cryptpad.setAttribute('username', newName, function (err) { Cryptpad.setDisplayName(newName, function (err) {
if (err) { if (err) {
console.log("Couldn't set username"); console.log("Couldn't set username");
console.error(err); 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 onFileUpload = function (sframeChan, data, cb) {
var sendEvent = function (data) { var sendEvent = function (data) {

@ -3,32 +3,24 @@ define(['jquery'], function ($) {
module.create = function (cfg, onLocal, Common, metadataMgr) { module.create = function (cfg, onLocal, Common, metadataMgr) {
var exp = {}; 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; exp.title = document.title;
cfg = cfg || {}; cfg = cfg || {};
var getHeadingText = cfg.getHeadingText || function () { return; }; 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; var $title;
exp.setToolbar = function (toolbar) { exp.setToolbar = function (toolbar) {
$title = toolbar && toolbar.title; $title = toolbar && toolbar.title;
}; };
exp.getTitle = function () { return exp.title; }; exp.getTitle = function () { return exp.title; };
var isDefaultTitle = exp.isDefaultTitle = function (){return exp.title === exp.defaultTitle;}; var isDefaultTitle = exp.isDefaultTitle = function (){return exp.title === exp.defaultTitle;};
var suggestTitle = exp.suggestTitle = function (fallback) { 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 // update title: href is optional; if not specified, we use window.location.href
exp.updateTitle = function (newTitle, cb) { exp.updateTitle = function (newTitle, cb) {
cb = cb || $.noop; cb = cb || $.noop;
if (newTitle === exp.title) { return; } 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 () { metadataMgr.onChange(function () {
var md = metadataMgr.getMetadata(); var md = metadataMgr.getMetadata();
$title.find('span.cp-toolbar-title-value').text(md.title || md.defaultTitle); $title.find('span.cp-toolbar-title-value').text(md.title || md.defaultTitle);
$title.find('input').val(md.title || md.defaultTitle); $title.find('input').val(md.title || md.defaultTitle);
exp.title = md.title; 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 () { exp.getTitleConfig = function () {

@ -46,12 +46,6 @@ define([
return ctx.cpNfInner.metadataMgr.getPrivateData().accountName; return ctx.cpNfInner.metadataMgr.getPrivateData().accountName;
}; };
var titleUpdated;
funcs.updateTitle = function (title, cb) {
ctx.metadataMgr.updateTitle(title);
titleUpdated = cb;
};
// UI // UI
funcs.createUserAdminMenu = UI.createUserAdminMenu; funcs.createUserAdminMenu = UI.createUserAdminMenu;
funcs.displayAvatar = UI.displayAvatar; funcs.displayAvatar = UI.displayAvatar;
@ -65,10 +59,7 @@ define([
// Title module // Title module
funcs.createTitle = Title.create; funcs.createTitle = Title.create;
funcs.getDefaultTitle = function () { // Misc
if (!ctx.cpNfInner) { throw new Error("cpNfInner is not ready!"); }
return ctx.cpNfInner.metadataMgr.getMetadata().defaultTitle;
};
funcs.setDisplayName = function (name, cb) { funcs.setDisplayName = function (name, cb) {
ctx.sframeChan.query('Q_SETTINGS_SET_DISPLAY_NAME', name, function (err) { ctx.sframeChan.query('Q_SETTINGS_SET_DISPLAY_NAME', name, function (err) {
@ -76,6 +67,7 @@ define([
}); });
}; };
// Window
funcs.logout = function (cb) { funcs.logout = function (cb) {
ctx.sframeChan.query('Q_LOGOUT', null, function (err) { ctx.sframeChan.query('Q_LOGOUT', null, function (err) {
if (cb) { cb(err); } if (cb) { cb(err); }
@ -92,6 +84,7 @@ define([
}); });
}; };
// Store
funcs.sendAnonRpcMsg = function (msg, content, cb) { funcs.sendAnonRpcMsg = function (msg, content, cb) {
ctx.sframeChan.query('Q_ANON_RPC_MESSAGE', { ctx.sframeChan.query('Q_ANON_RPC_MESSAGE', {
msg: msg, msg: msg,
@ -128,6 +121,21 @@ define([
}, cb); }, 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 // Files
funcs.uploadFile = function (data, cb) { funcs.uploadFile = function (data, cb) {
ctx.sframeChan.query('Q_UPLOAD_FILE', data, cb); ctx.sframeChan.query('Q_UPLOAD_FILE', data, cb);
@ -361,12 +369,6 @@ define([
// CpNfInner.start() should be here.... // CpNfInner.start() should be here....
}).nThen(function () { }).nThen(function () {
ctx.metadataMgr = MetadataMgr.create(ctx.sframeChan); 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_CONNECT', function () { CommonRealtime.setConnectionState(true); });
ctx.sframeChan.on('EV_RT_DISCONNECT', function () { CommonRealtime.setConnectionState(false); }); 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 // Send the new settings to the inner iframe when they are changed in the proxy
'EV_SETTINGS_UPDATE': true, '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_GET_PAD_ATTRIBUTE': true,
'Q_SET_PAD_ATTRIBUTE': true, 'Q_SET_PAD_ATTRIBUTE': true,

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

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

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

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

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

@ -21,7 +21,7 @@ define([
$(function () { $(function () {
var HIDE_INTRODUCTION_TEXT = "hide_poll_text"; var HIDE_INTRODUCTION_TEXT = "hide-text";
var defaultName; var defaultName;
var secret = Cryptpad.getSecrets(); var secret = Cryptpad.getSecrets();
@ -782,10 +782,10 @@ var create = function (info) {
.on('disconnect', disconnect) .on('disconnect', disconnect)
.on('reconnect', reconnect); .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 (e) { console.error(e); }
if (!value) { if (!value) {
Cryptpad.setAttribute(HIDE_INTRODUCTION_TEXT, "1", function (e) { Cryptpad.setAttribute(['poll', HIDE_INTRODUCTION_TEXT], "1", function (e) {
if (e) { console.error(e); } if (e) { console.error(e); }
}); });
showHelp(true); 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 */ /* jshint ignore:start */
var isFriend = function (proxy, edKey) { var isFriend = function (proxy, edKey) {
var friends = Cryptpad.find(proxy, ['friends']); var friends = Cryptpad.find(proxy, ['friends']);

Loading…
Cancel
Save