Skip to content
This repository was archived by the owner on Dec 25, 2023. It is now read-only.

Commit d656990

Browse files
authored
Add user and better structure to session json (#310)
* Adopted backend definition * Adopted main module * Adopted album * Adopted context menu * Adopted header * Adopted multi select * Adopted view * Update user object after update
1 parent c6dd815 commit d656990

File tree

8 files changed

+73
-65
lines changed

8 files changed

+73
-65
lines changed

scripts/3rd-party/backend.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,11 @@ const SmartAlbumID = Object.freeze({
276276
/**
277277
* @typedef InitializationData
278278
*
279-
* @property {number} status - `1`: unauthenticated, `2`: authenticated
280-
* @property {boolean} admin
281-
* @property {boolean} may_upload
282-
* @property {boolean} is_locked
279+
* @property {?User} user
280+
* @property {{is_admin: boolean, is_locked: boolean, may_upload: boolean}} rights
283281
* @property {number} update_json - version number of latest available update
284282
* @property {boolean} update_available
285283
* @property {Object.<string, string>} locale
286-
* @property {string} [username] - only if user is not the admin; TODO: Change that
287284
* @property {ConfigurationData} config
288285
* @property {DeviceConfiguration} config_device
289286
*/

scripts/main/album.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,15 +1393,15 @@ album.apply_nsfw_filter = function () {
13931393
* @returns {boolean}
13941394
*/
13951395
album.isUploadable = function () {
1396-
if (lychee.admin) {
1396+
if (lychee.rights.is_admin) {
13971397
return true;
13981398
}
1399-
if (lychee.publicMode || !lychee.may_upload) {
1399+
if (lychee.publicMode || !lychee.rights.may_upload) {
14001400
return false;
14011401
}
14021402

14031403
// TODO: Comparison of numeric user IDs (instead of names) should be more robust
1404-
return album.json === null || !album.json.owner_name || album.json.owner_name === lychee.username;
1404+
return album.json === null || (lychee.user !== null && album.json.owner_name === lychee.user.username);
14051405
};
14061406

14071407
/**

scripts/main/contextMenu.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ contextMenu.add = function (e) {
2525
items.splice(1);
2626
}
2727

28-
if (!lychee.admin) {
28+
if (!lychee.rights.is_admin) {
2929
// remove import from dropbox and server if not admin
3030
items.splice(3, 2);
3131
} else if (!lychee.dropboxKey || lychee.dropboxKey === "") {
@@ -678,7 +678,7 @@ contextMenu.move = function (IDs, e, callback, kind = "UNSORTED", display_root =
678678
addItems(data.albums);
679679
}
680680

681-
if (data.shared_albums && data.shared_albums.length > 0 && lychee.admin) {
681+
if (data.shared_albums && data.shared_albums.length > 0 && lychee.rights.is_admin) {
682682
items = items.concat({});
683683
addItems(data.shared_albums);
684684
}
@@ -717,7 +717,11 @@ contextMenu.sharePhoto = function (photoID, e) {
717717
{ title: build.iconic("twitter", iconClass) + "Twitter", fn: () => photo.share(photoID, "twitter") },
718718
{ title: build.iconic("facebook", iconClass) + "Facebook", fn: () => photo.share(photoID, "facebook") },
719719
{ title: build.iconic("envelope-closed") + "Mail", fn: () => photo.share(photoID, "mail") },
720-
{ title: build.iconic("dropbox", iconClass) + "Dropbox", visible: lychee.admin === true, fn: () => photo.share(photoID, "dropbox") },
720+
{
721+
title: build.iconic("dropbox", iconClass) + "Dropbox",
722+
visible: lychee.rights.is_admin === true,
723+
fn: () => photo.share(photoID, "dropbox"),
724+
},
721725
{ title: build.iconic("link-intact") + lychee.locale["DIRECT_LINKS"], fn: () => photo.showDirectLinks(photoID) },
722726
{ title: build.iconic("grid-two-up") + lychee.locale["QR_CODE"], fn: () => photo.qrCode(photoID) },
723727
];
@@ -782,12 +786,12 @@ contextMenu.config = function (e) {
782786
if (lychee.new_photos_notification) {
783787
items.push({ title: build.iconic("bell") + lychee.locale["NOTIFICATIONS"], fn: notifications.load });
784788
}
785-
if (lychee.admin) {
789+
if (lychee.rights.is_admin) {
786790
items.push({ title: build.iconic("person") + lychee.locale["USERS"], fn: users.list });
787791
}
788792
items.push({ title: build.iconic("key") + lychee.locale["U2F"], fn: u2f.list });
789793
items.push({ title: build.iconic("cloud") + lychee.locale["SHARING"], fn: sharing.list });
790-
if (lychee.admin) {
794+
if (lychee.rights.is_admin) {
791795
items.push({
792796
title: build.iconic("align-left") + lychee.locale["LOGS"],
793797
fn: function () {

scripts/main/header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ header.setMode = function (mode) {
287287
tabindex.makeUnfocusable(e);
288288
}
289289

290-
if (lychee.enable_button_add && lychee.may_upload) {
290+
if (lychee.enable_button_add && lychee.rights.may_upload) {
291291
const e = $(".button_add", ".header__toolbar--albums");
292292
e.show();
293293
tabindex.makeFocusable(e);

scripts/main/lychee.js

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,30 @@ const lychee = {
1818
public_photos_hidden: true,
1919
share_button_visible: false,
2020
/**
21-
* Enable admin mode (multi-user)
22-
* @type boolean
23-
*/
24-
admin: false,
25-
/**
26-
* Enable possibility to upload (multi-user)
27-
* @type boolean
21+
* The authenticated user or `null` if unauthenticated
22+
* @type {?User}
2823
*/
29-
may_upload: false,
24+
user: null,
3025
/**
31-
* Locked user (multi-user)
32-
* @type boolean
26+
* The rights granted by the backend
3327
*/
34-
is_locked: false,
35-
/** @type {?string} */
36-
username: null,
28+
rights: {
29+
/**
30+
* Backend grants admin rights
31+
* @type boolean
32+
*/
33+
is_admin: false,
34+
/**
35+
* Backend grants upload rights
36+
* @type boolean
37+
*/
38+
may_upload: false,
39+
/**
40+
* Backend considers the user to be locked
41+
* @type boolean
42+
*/
43+
is_locked: false,
44+
},
3745
/**
3846
* Values:
3947
*
@@ -242,23 +250,21 @@ lychee.init = function (isFirstInitialization = true) {
242250
function (data) {
243251
lychee.parseInitializationData(data);
244252

245-
if (data.status === 2) {
246-
// Logged in
253+
if (data.user !== null || data.rights.is_admin) {
254+
// Authenticated or no admin is registered
247255
leftMenu.build();
248256
leftMenu.bind();
249257
lychee.setMode("logged_in");
250258

251-
// Show dialog when there is no username and password
252-
// TODO: Refactor this. At least rename the flag `login` to something more understandable like `isAdminUserConfigured`, but rather re-factor the whole logic, i.e. the initial user should be created as part of the installation routine.
259+
// Show dialog to create admin account, if no user is
260+
// authenticated but admin rights are granted.
261+
// TODO: Refactor the whole logic, i.e. the initial user should be created as part of the installation routine.
253262
// In particular it is completely insane to build the UI as if the admin user was successfully authenticated.
254263
// This might leak confidential photos to anybody if the DB is filled
255264
// with photos and the admin password reset to `null`.
256-
if (data.config.login === false) settings.createLogin();
257-
} else if (data.status === 1) {
258-
lychee.setMode("public");
265+
if (data.user === null && data.rights.is_admin) settings.createLogin();
259266
} else {
260-
loadingBar.show("error", "Error: Unexpected status");
261-
return;
267+
lychee.setMode("public");
262268
}
263269

264270
if (isFirstInitialization) {
@@ -277,6 +283,8 @@ lychee.init = function (isFirstInitialization = true) {
277283
* @returns {void}
278284
*/
279285
lychee.parseInitializationData = function (data) {
286+
lychee.user = data.user;
287+
lychee.rights = data.rights;
280288
lychee.update_json = data.update_json;
281289
lychee.update_available = data.update_available;
282290

@@ -295,23 +303,9 @@ lychee.parseInitializationData = function (data) {
295303
lychee.locale[key] = data.locale[key];
296304
}
297305

298-
// Check status
299-
// 0 = No configuration
300-
// 1 = Logged out
301-
// 2 = Logged in
302-
if (data.status === 2) {
303-
// Logged in
304-
lychee.parsePublicInitializationData(data);
306+
lychee.parsePublicInitializationData(data);
307+
if (lychee.user !== null || lychee.rights.is_admin) {
305308
lychee.parseProtectedInitializationData(data);
306-
307-
lychee.may_upload = data.admin || data.may_upload;
308-
lychee.admin = data.admin;
309-
lychee.is_locked = data.is_locked;
310-
lychee.username = data.username;
311-
} else if (data.status === 1) {
312-
lychee.parsePublicInitializationData(data);
313-
} else {
314-
// should not happen.
315309
}
316310
};
317311

@@ -833,10 +827,10 @@ lychee.setTitle = function (title = "", editable = false) {
833827
* @param {string} mode - one out of: `public`, `view`, `logged_in`
834828
*/
835829
lychee.setMode = function (mode) {
836-
if (lychee.is_locked) {
830+
if (lychee.rights.is_locked) {
837831
$("#button_settings_open").remove();
838832
}
839-
if (!lychee.may_upload) {
833+
if (!lychee.rights.may_upload) {
840834
$("#button_sharing").remove();
841835

842836
$(document)
@@ -855,7 +849,7 @@ lychee.setMode = function (mode) {
855849
.unbind(["command+backspace", "ctrl+backspace"])
856850
.unbind(["command+a", "ctrl+a"]);
857851
}
858-
if (!lychee.admin) {
852+
if (!lychee.rights.is_admin) {
859853
$("#button_users, #button_logs, #button_diagnostics").remove();
860854
}
861855

scripts/main/multiselect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ multiselect.toggleItem = function (object, id) {
8080
*/
8181
multiselect.addItem = function (object, id) {
8282
if (album.isSmartID(id) || album.isSearchID(id)) return;
83-
if (!lychee.admin && albums.isShared(id)) return;
83+
if (!lychee.rights.is_admin && albums.isShared(id)) return;
8484
if (multiselect.isSelected(id).selected === true) return;
8585

8686
let isAlbum = object.hasClass("album");

scripts/main/settings.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ settings.createLogin = function () {
3333
return true;
3434
};
3535

36+
/**
37+
* @param {User} updatedAdminUser
38+
* @returns {void}
39+
*/
40+
const successHandler = function (updatedAdminUser) {
41+
lychee.user = updatedAdminUser;
42+
};
43+
3644
/**
3745
* @typedef SetLoginDialogResult
3846
*
@@ -72,7 +80,7 @@ settings.createLogin = function () {
7280
password,
7381
};
7482

75-
api.post("Settings::setLogin", params, null, null, errorHandler);
83+
api.post("Settings::setLogin", params, successHandler, null, errorHandler);
7684
};
7785

7886
const msg = `
@@ -194,11 +202,16 @@ settings.changeLogin = function (params) {
194202
$("input[name=confirm]").removeClass("error");
195203
}
196204

197-
api.post("Settings::updateLogin", params, function () {
198-
$("input[name]").removeClass("error");
199-
loadingBar.show("success", lychee.locale["SETTINGS_SUCCESS_LOGIN"]);
200-
view.settings.content.clearLogin();
201-
});
205+
api.post(
206+
"Settings::updateLogin",
207+
params,
208+
/** @param {User} updatedUser */ function (updatedUser) {
209+
$("input[name]").removeClass("error");
210+
loadingBar.show("success", lychee.locale["SETTINGS_SUCCESS_LOGIN"]);
211+
view.settings.content.clearLogin();
212+
lychee.user = updatedUser;
213+
}
214+
);
202215
};
203216

204217
/**

scripts/main/view.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ view.albums = {
7979
html += build.divider(album.owner_name);
8080
current_owner = album.owner_name;
8181
}
82-
return html + build.album(album, !lychee.admin);
82+
return html + build.album(album, !lychee.rights.is_admin);
8383
}, "");
8484

8585
if (smartData === "" && tagAlbumsData === "" && albumsData === "" && sharedData === "") {
@@ -491,7 +491,7 @@ view.album = {
491491
// },
492492
targetRowHeight: parseFloat($(".photo").css("--lychee-default-height")),
493493
});
494-
// if (lychee.admin) console.log(layoutGeometry);
494+
// if (lychee.rights.is_admin) console.log(layoutGeometry);
495495
$(".justified-layout").css("height", layoutGeometry.containerHeight + "px");
496496
$(".justified-layout > div").each(function (i) {
497497
if (!layoutGeometry.boxes[i]) {
@@ -1034,7 +1034,7 @@ view.settings = {
10341034
init: function () {
10351035
view.settings.clearContent();
10361036
view.settings.content.setLogin();
1037-
if (lychee.admin) {
1037+
if (lychee.rights.is_admin) {
10381038
view.settings.content.setSorting();
10391039
view.settings.content.setDropboxKey();
10401040
view.settings.content.setLang();

0 commit comments

Comments
 (0)