Skip to content

Commit

Permalink
Merge pull request #61 from AlexSciFier/fix-no-login
Browse files Browse the repository at this point in the history
Fix for disabled authentication
  • Loading branch information
AlexSciFier authored Aug 23, 2023
2 parents eab2b55 + d181636 commit 4660abe
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 54 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
node_modules
server/data
server/db
data
background
.vscode
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/helpers/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const APP_NAME = "NeonLink";
export const VERSION = "1.4.4";
export const VERSION = "1.4.5";
export const DEF_MAX_ITEMS = 20;
export const DEF_COLUMNS = 3;
export const CARD_HEADER_STYLE = [
Expand Down
16 changes: 14 additions & 2 deletions frontend/src/pages/settings/tabs/mainTab/UsersList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import React, { useEffect, useRef, useState } from "react";
import { notify } from "../../../../components/Notification";
import { deleteJSON, getJSON, putJSON } from "../../../../helpers/fetch";
import UserItem from "./UserItem";
import { appSettingsKeys, useAppSettingsStore } from "../../../../stores/appSettingsStore";

export default function UsersList() {
const [users, setUsers] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(undefined);
const [authenticationEnabled] = useAppSettingsStore(
appSettingsKeys.AuthenticationEnabled
);

const abortController = useRef(null);

Expand Down Expand Up @@ -74,11 +78,19 @@ export default function UsersList() {
}, []);

useEffect(() => {
if (isError) notify("Error", isError?.message || "", "error");
}, [isError]);
if (isError && authenticationEnabled) {
notify(
"Error",
"Can't get list of users. " + isError?.message || "",
"error"
);
}
}, [isError, authenticationEnabled]);

if (isLoading) return <div>Loading...</div>;

if (users.length === 0) return <div>No users</div>;

return (
<ul className="border rounded px-4 py-2 flex flex-col gap-1">
{users.map((user) => (
Expand Down
37 changes: 29 additions & 8 deletions server/db/sqlite/stores/backgrounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,43 @@ export default class BackgroundsStore {
}

deleteItem(id, userId) {
const deleteQuery = `DELETE FROM backgrounds WHERE id=:id AND (userId=:userId OR userId IS NULL)`;
return this.db.prepare(deleteQuery).run({ id, userId }).changes;
const deleteQuery = `DELETE FROM backgrounds WHERE id=:id`;
let deleteParams = {id};
if (userId) {
deleteQuery += " AND (userId IN (:userId, 0) OR userId IS NULL)";
deleteParams += { userId };
}
return this.db.prepare(deleteQuery).run(deleteParams).changes;
}

getAll(userId) {
const selectQuery = `SELECT * FROM backgrounds WHERE userId=:userId OR userId IS NULL`;
return this.db.prepare(selectQuery).all({ userId });
let selectQuery = `SELECT * FROM backgrounds`;
let selectParams = {};
if (userId) {
selectQuery += " WHERE userId IN (:userId, 0) OR userId IS NULL";
selectParams += { userId };
}

return this.db.prepare(selectQuery).all(selectParams);
}

getItemById(id, userId) {
const selectQuery = `SELECT * FROM backgrounds WHERE id=:id AND (userId=:userId OR userId IS NULL)`;
return this.db.prepare(selectQuery).all({ id, userId });
let selectQuery = `SELECT * FROM backgrounds WHERE id=:id`;
let selectParams = {id};
if (userId) {
selectQuery += " AND (userId IN (:userId, 0) OR userId IS NULL)";
selectParams += { userId };
}
return this.db.prepare(selectQuery).all(selectParams);
}

getItemByUrl(url, userId) {
const selectQuery = `SELECT * FROM backgrounds WHERE url=:url AND (userId=:userId OR userId IS NULL)`;
return this.db.prepare(selectQuery).all({ url, userId });
let selectQuery = `SELECT * FROM backgrounds WHERE url=:url`;
let selectParams = {url};
if (userId) {
selectQuery += " AND (userId IN (:userId, 0) OR userId IS NULL)";
selectParams += { userId };
}
return this.db.prepare(selectQuery).all(selectParams);
}
}
70 changes: 48 additions & 22 deletions server/db/sqlite/stores/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,15 @@ export default class BookmarksStore {
category !== undefined
? "LEFT JOIN category ON category.id = bookmarks.categoryId"
: ""
}
WHERE userId = :userId`;
}`;

const selectParams = {
userId,
};

if (search || tag || category) {
const conditions = [];
const conditions = [];

if (search || tag || category) {
if (search) {
conditions.push("bookmarks.search LIKE :search");
selectParams.search = `%${search}%`;
Expand All @@ -81,10 +80,15 @@ export default class BookmarksStore {
conditions.push("category.name = :category");
selectParams.category = category;
}
}

selectQuery += ` WHERE ${conditions.join(" AND ")}`;
if (userId) {
conditions.push("(userId IN (:userId, 0) OR userId IS NULL)");
selectParams.userId = userId;
}

selectQuery += ` WHERE ${conditions.join(" AND ")}`;

selectQuery += ` GROUP BY bookmarks.id
ORDER BY bookmarks.created DESC`;

Expand Down Expand Up @@ -146,7 +150,11 @@ export default class BookmarksStore {

const conditions = [];

conditions.push("bookmarks.userId = :userId");
if (userId) {
conditions.push("(bookmarks.userId IN (:userId, 0) OR bookmarks.userId IS NULL)");
countParams.userId = userId;
selectParams.userId = userId;
}

if (search || tag || category) {
if (search) {
Expand All @@ -166,8 +174,10 @@ export default class BookmarksStore {
}
}

countQuery += ` WHERE ${conditions.join(" AND ")}`;
selectQuery += ` WHERE ${conditions.join(" AND ")}`;
if (conditions.length > 0) {
countQuery += ` WHERE ${conditions.join(" AND ")}`;
selectQuery += ` WHERE ${conditions.join(" AND ")}`;
}

selectQuery += ` GROUP BY bookmarks.id
ORDER BY bookmarks.created DESC
Expand All @@ -192,22 +202,27 @@ export default class BookmarksStore {
id, url, title, desc, bookmarks.categoryId, created, bookmarkPosition.position
FROM bookmarks
LEFT JOIN bookmarkPosition ON bookmarks.id = bookmarkPosition.bookmarkId
WHERE
bookmarks.categoryId = :categoryId
AND
bookmarks.userId = :userId
ORDER BY bookmarkPosition.position`;
`;
let selectParams = { categoryId, userId }

let conditions = []
conditions.push("bookmarks.categoryId = :categoryId")
if(userId){
conditions.push("(bookmarks.userId IN (:userId, 0) OR bookmarks.userId IS NULL)")
}
selectQuery += `WHERE ${conditions.join(" AND ")} `
selectQuery += "ORDER BY bookmarkPosition.position"

return this.db
.prepare(selectQuery)
.all({ categoryId, userId })
.all(selectParams)
.map((bookmark) => {
return { ...bookmark, tags: bookmark.tags?.split(",") };
});
}

getItemById(userId, id) {
const selectQuery = `SELECT
let selectQuery = `SELECT
bookmarks.id,
url,
title,
Expand All @@ -218,16 +233,27 @@ export default class BookmarksStore {
FROM bookmarks
LEFT JOIN bookmarksTags ON bookmarksTags.bookmarkId = bookmarks.id
LEFT JOIN tags ON bookmarksTags.tagId = tags.id
WHERE bookmarks.id = :id
AND bookmarks.userId = :userId
GROUP BY bookmarks.id`;

return this.db.prepare(selectQuery).get({ id, userId });
`;
let selectParams = {id}
let conditions = ["bookmarks.id = :id"]
conditions.push()
if(userId){
conditions.push("(bookmarks.userId IN (:userId, 0) OR bookmarks.userId IS NULL)")
selectParams.userId = userId
}
selectQuery += `WHERE ${conditions.join(" AND ")} `
selectQuery += `GROUP BY bookmarks.id`
return this.db.prepare(selectQuery).get(selectParams);
}

getItemByUrl(userId, url) {
const selectQuery = `SELECT * FROM bookmarks WHERE url = :url AND userId = :userId`;
return this.db.prepare(selectQuery).get({ url, userId });
const selectQuery = `SELECT * FROM bookmarks WHERE url = :url`;
let selectParams = {url}
if(userId){
selectQuery += " AND (userId IN (:userId, 0) OR userId IS NULL)"
selectParams.userId = userId
}
return this.db.prepare(selectQuery).get(selectParams);
}

getIconByBookmarkId(id) {
Expand Down
34 changes: 22 additions & 12 deletions server/db/sqlite/stores/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ export default class CategoriesStore {
}

getAll(userId) {
const selectQuery = `SELECT
let selectQuery = `SELECT
id, name, color, position FROM category
INNER JOIN categoryPosition ON categoryPosition.categoryId = category.id
WHERE userId = :userId
ORDER BY position ASC`;
INNER JOIN categoryPosition ON categoryPosition.categoryId = category.id`
let selectParams = {}
if(userId){
selectQuery += ` WHERE (userId IN (:userId, 0) OR userId IS NULL) `
selectParams.userId = userId
}
selectQuery += ` ORDER BY position ASC`;

return this.db.prepare(selectQuery).all({ userId });
return this.db.prepare(selectQuery).all(selectParams);
}

getItemById(id) {
Expand All @@ -42,15 +46,21 @@ export default class CategoriesStore {
}

getItemByName(name, userId) {
const selectQuery = `SELECT
let selectQuery = `SELECT
id, name, color, position FROM category
INNER JOIN categoryPosition ON categoryPosition.categoryId = category.id
WHERE
name = :name
AND userId = :userId
ORDER BY position ASC`;

return this.db.prepare(selectQuery).get({ name, userId });
`;
let selectParams = {name}
let conditions = []
conditions.push("name = :name")
if(userId){
conditions.push("(userId IN (:userId, 0) OR userId IS NULL)")
selectParams.userId = userId
}
selectQuery += ` WHERE ${conditions.join(" AND ")} `
selectQuery += `ORDER BY position ASC`
return this.db.prepare(selectQuery).get(selectParams);
}

deleteItem(id) {
Expand Down
25 changes: 17 additions & 8 deletions server/db/sqlite/stores/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ export default class TagsStore {
}

existsItemByName(name, userId) {
const selectQuery = `SELECT COUNT(*) AS count FROM tags WHERE name = :name AND userId = :userId`;
const result = this.db.prepare(selectQuery).get({ name, userId });
let selectQuery = `SELECT COUNT(*) AS count FROM tags WHERE name = :name`;
let selectParams = { name };
if (userId) {
selectQuery += ` AND (userId IN (:userId, 0) OR userId IS NULL) `;
selectParams.userId = userId;
}

const result = this.db.prepare(selectQuery).get(selectParams);
return result && result.count > 0;
}

Expand All @@ -26,22 +32,25 @@ export default class TagsStore {
id, name
FROM tags`;

const selectParams = { userId };
const conditions = [];
conditions.push("tags.userId = :userId");
let selectParams = {};
let conditions = [];
if (userId) {
conditions.push("(tags.userId IN (:userId, 0) OR tags.userId IS NULL)");
selectParams.userId = userId;
}

if (name) {
conditions.push("tags.name LIKE :name");
selectParams.name = `%${name}%`;
}

selectQuery += ` WHERE ${conditions.join(" AND ")}`;
if (conditions.length > 0) {
selectQuery += ` WHERE ${conditions.join(" AND ")}`;
}

selectQuery += ` GROUP BY tags.id
ORDER BY tags.name`;

console.log(selectQuery, selectParams);

return this.db.prepare(selectQuery).all(selectParams);
}

Expand Down

0 comments on commit 4660abe

Please sign in to comment.