mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
use uuid
This commit is contained in:
parent
246e4e5f4f
commit
17041efaae
73 changed files with 491 additions and 874 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { ok, unauthorized, badRequest, checkPassword, createSecureToken } from 'next-basics';
|
||||
import { getAccount } from 'queries';
|
||||
import { getUser } from 'queries';
|
||||
import { secret } from 'lib/crypto';
|
||||
|
||||
export default async (req, res) => {
|
||||
|
|
@ -9,12 +9,11 @@ export default async (req, res) => {
|
|||
return badRequest(res);
|
||||
}
|
||||
|
||||
const account = await getAccount({ username });
|
||||
const user = await getUser({ username });
|
||||
|
||||
if (account && checkPassword(password, account.password)) {
|
||||
const { id, username, isAdmin, accountUuid } = account;
|
||||
const user = { userId: id, username, isAdmin, accountUuid };
|
||||
const token = createSecureToken(user, secret());
|
||||
if (user && checkPassword(password, user.password)) {
|
||||
const { id: userId, username, isAdmin } = user;
|
||||
const token = createSecureToken({ userId, username, isAdmin }, secret());
|
||||
|
||||
return ok(res, { token, user });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ export default async (req, res) => {
|
|||
|
||||
await useSession(req, res);
|
||||
|
||||
const { website, session } = req.session;
|
||||
const { websiteId, session } = req.session;
|
||||
|
||||
const { type, payload } = getJsonBody(req);
|
||||
|
||||
|
|
@ -68,14 +68,12 @@ export default async (req, res) => {
|
|||
url = url.replace(/\/$/, '');
|
||||
}
|
||||
|
||||
const eventUuid = uuid();
|
||||
|
||||
if (type === 'pageview') {
|
||||
await savePageView(website, { session, url, referrer });
|
||||
await savePageView(websiteId, { pageViewId: uuid(), session, url, referrer });
|
||||
} else if (type === 'event') {
|
||||
await saveEvent(website, {
|
||||
await saveEvent(websiteId, {
|
||||
eventId: uuid(),
|
||||
session,
|
||||
eventUuid,
|
||||
url,
|
||||
eventName,
|
||||
eventData,
|
||||
|
|
@ -86,7 +84,7 @@ export default async (req, res) => {
|
|||
|
||||
const token = createToken(
|
||||
{
|
||||
website,
|
||||
websiteId,
|
||||
session,
|
||||
},
|
||||
secret(),
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ export default async (req, res) => {
|
|||
const { userId } = req.auth;
|
||||
|
||||
const websites = await getUserWebsites({ userId });
|
||||
const ids = websites.map(({ websiteUuid }) => websiteUuid);
|
||||
const ids = websites.map(({ id }) => id);
|
||||
const token = createToken({ websites: ids }, secret());
|
||||
const data = await getRealtimeData(ids, subMinutes(new Date(), 30));
|
||||
|
||||
|
|
|
|||
|
|
@ -3,14 +3,14 @@ import { ok, notFound, methodNotAllowed, createToken } from 'next-basics';
|
|||
import { secret } from 'lib/crypto';
|
||||
|
||||
export default async (req, res) => {
|
||||
const { id } = req.query;
|
||||
const { id: shareId } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const website = await getWebsite({ shareId: id });
|
||||
const website = await getWebsite({ shareId });
|
||||
|
||||
if (website) {
|
||||
const { websiteUuid } = website;
|
||||
const data = { id: websiteUuid };
|
||||
const { id } = website;
|
||||
const data = { id };
|
||||
const token = createToken(data, secret());
|
||||
|
||||
return ok(res, { ...data, token });
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { getAccount, deleteAccount, updateAccount } from 'queries';
|
||||
import { getUser, deleteUser, updateUser } from 'queries';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
|
||||
export default async (req, res) => {
|
||||
|
|
@ -13,9 +13,9 @@ export default async (req, res) => {
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const account = await getAccount({ id: +id });
|
||||
const user = await getUser({ id });
|
||||
|
||||
return ok(res, account);
|
||||
return ok(res, user);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
|
|
@ -25,7 +25,7 @@ export default async (req, res) => {
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const account = await getAccount({ id: +id });
|
||||
const user = await getUser({ id });
|
||||
|
||||
const data = {};
|
||||
|
||||
|
|
@ -39,15 +39,15 @@ export default async (req, res) => {
|
|||
}
|
||||
|
||||
// Check when username changes
|
||||
if (data.username && account.username !== data.username) {
|
||||
const accountByUsername = await getAccount({ username });
|
||||
if (data.username && user.username !== data.username) {
|
||||
const userByUsername = await getUser({ username });
|
||||
|
||||
if (accountByUsername) {
|
||||
return badRequest(res, 'Account already exists');
|
||||
if (userByUsername) {
|
||||
return badRequest(res, 'User already exists');
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await updateAccount(data, { id: +id });
|
||||
const updated = await updateUser(data, { id });
|
||||
|
||||
return ok(res, updated);
|
||||
}
|
||||
|
|
@ -57,7 +57,7 @@ export default async (req, res) => {
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
await deleteAccount(userId);
|
||||
await deleteUser(id);
|
||||
|
||||
return ok(res);
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { getAccount, updateAccount } from 'queries';
|
||||
import { getUser, updateUser } from 'queries';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import {
|
||||
badRequest,
|
||||
|
|
@ -15,22 +15,22 @@ export default async (req, res) => {
|
|||
await useAuth(req, res);
|
||||
|
||||
const { current_password, new_password } = req.body;
|
||||
const { id: accountUuid } = req.query;
|
||||
const { id } = req.query;
|
||||
|
||||
if (!(await allowQuery(req, TYPE_ACCOUNT))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const account = await getAccount({ accountUuid });
|
||||
const user = await getUser({ id });
|
||||
|
||||
if (!checkPassword(current_password, account.password)) {
|
||||
if (!checkPassword(current_password, user.password)) {
|
||||
return badRequest(res, 'Current password is incorrect');
|
||||
}
|
||||
|
||||
const password = hashPassword(new_password);
|
||||
|
||||
const updated = await updateAccount({ password }, { accountUuid });
|
||||
const updated = await updateUser({ password }, { id });
|
||||
|
||||
return ok(res, updated);
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import { ok, unauthorized, methodNotAllowed, badRequest, hashPassword } from 'next-basics';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { createAccount, getAccount, getAccounts } from 'queries';
|
||||
import { createUser, getUser, getUsers } from 'queries';
|
||||
|
||||
export default async (req, res) => {
|
||||
await useAuth(req, res);
|
||||
|
|
@ -13,24 +13,24 @@ export default async (req, res) => {
|
|||
}
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const accounts = await getAccounts();
|
||||
const users = await getUsers();
|
||||
|
||||
return ok(res, accounts);
|
||||
return ok(res, users);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { username, password, account_uuid } = req.body;
|
||||
const { username, password } = req.body;
|
||||
|
||||
const account = await getAccount({ username });
|
||||
const user = await getUser({ username });
|
||||
|
||||
if (account) {
|
||||
return badRequest(res, 'Account already exists');
|
||||
if (user) {
|
||||
return badRequest(res, 'User already exists');
|
||||
}
|
||||
|
||||
const created = await createAccount({
|
||||
const created = await createUser({
|
||||
id: uuid(),
|
||||
username,
|
||||
password: hashPassword(password),
|
||||
accountUuid: account_uuid || uuid(),
|
||||
});
|
||||
|
||||
return ok(res, created);
|
||||
|
|
@ -1,39 +1,39 @@
|
|||
import { allowQuery } from 'lib/auth';
|
||||
import { useAuth, useCors } from 'lib/middleware';
|
||||
import { getRandomChars, methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
|
||||
import { deleteWebsite, getAccount, getWebsite, updateWebsite } from 'queries';
|
||||
import { deleteWebsite, getUser, getWebsite, updateWebsite } from 'queries';
|
||||
import { TYPE_WEBSITE } from 'lib/constants';
|
||||
|
||||
export default async (req, res) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
|
||||
const { id: websiteUuid } = req.query;
|
||||
const { id } = req.query;
|
||||
|
||||
if (!(await allowQuery(req, TYPE_WEBSITE))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const website = await getWebsite({ websiteUuid });
|
||||
const website = await getWebsite({ id });
|
||||
|
||||
return ok(res, website);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { name, domain, owner, enableShareUrl, shareId } = req.body;
|
||||
const { accountUuid } = req.auth;
|
||||
let account;
|
||||
const { userId } = req.auth;
|
||||
let user;
|
||||
|
||||
if (accountUuid) {
|
||||
account = await getAccount({ accountUuid });
|
||||
if (userId) {
|
||||
user = await getUser({ id: userId });
|
||||
|
||||
if (!account) {
|
||||
return serverError(res, 'Account does not exist.');
|
||||
if (!user) {
|
||||
return serverError(res, 'User does not exist.');
|
||||
}
|
||||
}
|
||||
|
||||
const website = await getWebsite({ websiteUuid });
|
||||
const website = await getWebsite({ id });
|
||||
|
||||
const newShareId = enableShareUrl ? website.shareId || getRandomChars(8) : null;
|
||||
|
||||
|
|
@ -43,9 +43,9 @@ export default async (req, res) => {
|
|||
name,
|
||||
domain,
|
||||
shareId: shareId ? shareId : newShareId,
|
||||
userId: account ? account.id : +owner || undefined,
|
||||
userId: +owner || user.id,
|
||||
},
|
||||
{ websiteUuid },
|
||||
{ id },
|
||||
);
|
||||
} catch (e) {
|
||||
if (e.message.includes('Unique constraint') && e.message.includes('share_id')) {
|
||||
|
|
@ -61,7 +61,7 @@ export default async (req, res) => {
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
await deleteWebsite(websiteUuid);
|
||||
await deleteWebsite(id);
|
||||
|
||||
return ok(res);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ export default async (req, res) => {
|
|||
let domain;
|
||||
|
||||
if (type === 'referrer') {
|
||||
const website = await getWebsite({ websiteUuid: websiteId });
|
||||
const website = await getWebsite({ id: websiteId });
|
||||
|
||||
if (!website) {
|
||||
return badRequest(res);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { createWebsite, getAccount, getAllWebsites, getUserWebsites } from 'queries';
|
||||
import { createWebsite, getUser, getAllWebsites, getUserWebsites } from 'queries';
|
||||
import { ok, methodNotAllowed, unauthorized, getRandomChars } from 'next-basics';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { uuid } from 'lib/crypto';
|
||||
|
|
@ -8,14 +8,14 @@ export default async (req, res) => {
|
|||
|
||||
const { user_id, include_all } = req.query;
|
||||
const { userId: currentUserId, isAdmin } = req.auth;
|
||||
const accountUuid = user_id || req.auth.accountUuid;
|
||||
let account;
|
||||
const id = user_id || currentUserId;
|
||||
let user;
|
||||
|
||||
if (accountUuid) {
|
||||
account = await getAccount({ accountUuid });
|
||||
if (id) {
|
||||
user = await getUser({ id });
|
||||
}
|
||||
|
||||
const userId = account ? account.id : user_id;
|
||||
const userId = user ? user.id : user_id;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (userId && userId !== currentUserId && !isAdmin) {
|
||||
|
|
@ -23,9 +23,7 @@ export default async (req, res) => {
|
|||
}
|
||||
|
||||
const websites =
|
||||
isAdmin && include_all
|
||||
? await getAllWebsites()
|
||||
: await getUserWebsites({ userId: account?.id });
|
||||
isAdmin && include_all ? await getAllWebsites() : await getUserWebsites({ userId });
|
||||
|
||||
return ok(res, websites);
|
||||
}
|
||||
|
|
@ -33,15 +31,14 @@ export default async (req, res) => {
|
|||
if (req.method === 'POST') {
|
||||
const { name, domain, owner, enableShareUrl } = req.body;
|
||||
|
||||
const website_owner = account ? account.id : +owner;
|
||||
const website_owner = user ? userId : +owner;
|
||||
|
||||
if (website_owner !== currentUserId && !isAdmin) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const websiteUuid = uuid();
|
||||
const shareId = enableShareUrl ? getRandomChars(8) : null;
|
||||
const website = await createWebsite(website_owner, { websiteUuid, name, domain, shareId });
|
||||
const website = await createWebsite(website_owner, { id: uuid(), name, domain, shareId });
|
||||
|
||||
return ok(res, website);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue