Skip to content

Commit

Permalink
Client: remove all uses of cookie storage
Browse files Browse the repository at this point in the history
The previous fallback code to look for cookie entries without CATMAID's
cookie suffix is now replaced with a check for local storage entries
without this suffix. Similarly to before, they are removed and properly
namespaced (by adding the suffix).

Closes #2164
  • Loading branch information
tomka committed Dec 21, 2021
1 parent 35d77d4 commit 5e2c7a8
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,10 @@ Miscellaneous:
- If Caps-Lock is enabled, users now see a warning by default if they press down
any key. This can be disabled in the Settings Widget.

- Client settings that are stored in the browser's local storage are now
properly namespaced so that they work with multiple instances on the same
server.

- The new settings.py setting `PROJECT_TOKEN_USER_VISIBILITY` allows to
constrain the users that are visible to non-superuser users. If enabled, only
users that share knowledge about the same project tokens can be seen.
Expand Down
26 changes: 14 additions & 12 deletions django/applications/catmaid/static/js/WindowMaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ var WindowMaker = new function()
var serializedState = stateSerializer.serialize({
'state': stateManager.getState(widget, withInteractionState)
});
localStorage.setItem(key, serializedState);
CATMAID.setLocalStorageItem(key, serializedState);
return true;
};

/**
* Remove all stored widget states in the front-end.
*/
CATMAID.clearLocalSettings = function() {
localStorage.clear();
CATMAID.clearLocalSettings();
};

/**
Expand Down Expand Up @@ -76,7 +76,7 @@ var WindowMaker = new function()
if (stateManager) {
try {
var key = windowManagerStoragePrefix + stateManager.key;
localStorage.removeItem(key);
CATMAID.removeLocalStorageItem(key);
return true;
} catch (e) {
CATMAID.warn("Couldn't save widget state");
Expand Down Expand Up @@ -120,17 +120,19 @@ var WindowMaker = new function()
*/
var loadWidgetState = function(widget, stateManager) {
key = windowManagerStoragePrefix + stateManager.key;
var serializedWidgetData = localStorage.getItem(key);
var serializedWidgetData = CATMAID.getLocalStorageItem(key);
if (!serializedWidgetData) {
// Try to find information in cookie. If the item is found, it is copied
// to the local storage and removed from the cookie. This test can be
// removed in future versions and is only meant to not surprise users with
// lost defaults and stale cookie information.
serializedWidgetData = CATMAID.getCookie(key);
// Try to find information in local storage without a suffix. If the item
// is found, it is copied to the local storage with suffix and the
// unnamespaced version is removed. This test can be removed in future
// versions and is only meant to not surprise users with lost defaults and
// stale local storage information.
serializedWidgetData = CATMAID.getLocalStorageItem(key, true);
if (serializedWidgetData) {
localStorage.setItem(key, serializedWidgetData);
// Remove old cookie entry
CATMAID.setCookie(key, '', -1);
// Remove old local storage entry
CATMAID.removeLocalStorageItem(key, true);
// Add new entry
CATMAID.setLocalStorageItem(key, serializedWidgetData);
}
}
if (serializedWidgetData) {
Expand Down
27 changes: 15 additions & 12 deletions django/applications/catmaid/static/js/layers/stack-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
mirrorIndex = parseInt(lastUsedMirror, 10);

if (mirrorIndex >= this.stack.mirrors.length) {
localStorage.removeItem(this.lastMirrorStorageName);
CATMAID.removeLocalStorageItem(this.lastMirrorStorageName);
mirrorIndex = undefined;
}
}
Expand Down Expand Up @@ -99,16 +99,19 @@
}

var readStateItem = function(key) {
var item = localStorage.getItem(key);
var item = CATMAID.getLocalStorageItem(key);
if (!item) {
// Try to load custom mirror from cookie if no local storage information
// is found. This is removed in a future release and is only meant to not
// cause surprising defaults after an update.
item = CATMAID.getCookie(key);
// Try to find information in local storage without a suffix. If the item
// is found, it is copied to the local storage with suffix and the
// unnamespaced version is removed. This test can be removed in future
// versions and is only meant to not surprise users with lost defaults and
// stale local storage information.
item = CATMAID.getLocalStorageItem(key, true);
if (item) {
localStorage.setItem(key, item);
// Remove old cookie entry
CATMAID.setCookie(key, '', -1);
// Remove old local storage entry
CATMAID.removeLocalStorageItem(key, true);
// Add new entry
CATMAID.setLocalStorageItem(key, item);
}
}
return item;
Expand Down Expand Up @@ -335,7 +338,7 @@
var customMirrorData = getMirrorData();
var newMirrorIndex = self.stack.addMirror(customMirrorData);
self.switchToMirror(newMirrorIndex);
localStorage.setItem(self.customMirrorStorageName,
CATMAID.setLocalStorageItem(self.customMirrorStorageName,
JSON.stringify(customMirrorData));

// Update layer control UI to reflect settings changes.
Expand All @@ -362,7 +365,7 @@
customMirrorIndices.sort().reverse().forEach(function(ci) {
this.stack.removeMirror(ci);
}, this);
localStorage.removeItem(this.customMirrorStorageName);
CATMAID.removeLocalStorageItem(this.customMirrorStorageName);
this.switchToMirror(this.mirrorIndex, true);

CATMAID.msg("Done", "Custom mirrors cleared");
Expand Down Expand Up @@ -566,7 +569,7 @@
this.stackViewer.replaceStackLayer(layerKey, newStackLayer);

// Store last used mirror information in cookie
localStorage.setItem(this.lastMirrorStorageName, mirrorIndex);
CATMAID.setLocalStorageItem(this.lastMirrorStorageName, mirrorIndex);
};

/**
Expand Down
75 changes: 75 additions & 0 deletions django/applications/catmaid/static/libs/catmaid/CATMAID.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,81 @@ var requestQueue = new CATMAID.RequestQueue();
}
};

/**
* Return a named value from local storage.
*
* @param {String} name The key for the value to retrieve.
* @param {Boolean} noSuffix Optional, disable automatic appending of
* CATMAID's instance identifier suffix to the
* localStorage key.
* @returns The returned value or undefined if no value for
* the passed in name is available.
*/
CATMAID.getLocalStorageItem = function(name, noSuffix=false) {
if (!noSuffix) {
name = `${name}_${CATMAID.cookieSuffix}`;
}

return localStorage.getItem(name);
};

/**
* Define a local storage value for a particular name.
*
* @param {String} name The key for the value to store.
* @param {String} value The value to store.
* @param {Boolean} noSufifx Optional, disable automatic appending of
* CATMAID's instance identifier to the local
* storage name.
*/
CATMAID.setLocalStorageItem = function(name, value, noSuffix=false) {
try {
if (!noSuffix) {
name = `${name}_${CATMAID.cookieSuffix}`;
}
localStorage.setItem(name, value);
} catch (e) {
CATMAID.handleError(e);
}
};

/**
* Remove a local storage value with a particular name.
*
* @param {String} name The key for the value to store.
* @param {Boolean} noSufifx Optional, disable automatic appending of
* CATMAID's instance identifier to the local
* storage name.
*/
CATMAID.removeLocalStorageItem = function(name, noSuffix=false) {
if (!noSuffix) {
name = `${name}_${CATMAID.cookieSuffix}`;
}
localStorage.removeItem(name);
};

/**
* Clear local storage, usually for a suffix.
*
* @param {Boolean} noSufifx Optional, disable automatic appending of
* CATMAID's instance identifier to the local
* storage name. Will clear storage for entire
* domain.
*/
CATMAID.clearLocalStorage = function(noSuffix=false) {
if (noSuffix) {
localStorage.clear();
return;
}
let suffix = `_${CATMAID.cookieSuffix}`;
for (let i = 0; i < localStorage.length; i++) {
let name = localStorage.key(i);
if (name.endsWith(suffix)) {
localStorage.removeItem(name);
}
}
};

/**
* Return a named value from a cookie.
*
Expand Down

0 comments on commit 5e2c7a8

Please sign in to comment.