mirror of
https://github.com/umami-software/umami.git
synced 2026-02-08 22:57:12 +01:00
Refactored queries.
This commit is contained in:
parent
f36a689817
commit
8904b7b4ed
27 changed files with 137 additions and 144 deletions
25
lib/auth.ts
25
lib/auth.ts
|
|
@ -10,8 +10,7 @@ import {
|
|||
parseSecureToken,
|
||||
parseToken,
|
||||
} from 'next-basics';
|
||||
import { getTeamUser } from 'queries';
|
||||
import { getTeamWebsite, getTeamWebsiteByTeamMemberId } from 'queries/admin/teamWebsite';
|
||||
import { getTeamUser, getTeamWebsite, findTeamWebsiteByUserId } from 'queries';
|
||||
import { loadWebsite } from './load';
|
||||
import { Auth } from './types';
|
||||
|
||||
|
|
@ -79,19 +78,13 @@ export async function canViewWebsite({ user, shareToken }: Auth, websiteId: stri
|
|||
return true;
|
||||
}
|
||||
|
||||
const teamWebsite = await getTeamWebsiteByTeamMemberId(websiteId, user.id);
|
||||
|
||||
if (teamWebsite) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const website = await loadWebsite(websiteId);
|
||||
|
||||
if (website.userId) {
|
||||
return user.id === website.userId;
|
||||
}
|
||||
|
||||
return false;
|
||||
return !!(await findTeamWebsiteByUserId(websiteId, user.id));
|
||||
}
|
||||
|
||||
export async function canCreateWebsite({ user }: Auth) {
|
||||
|
|
@ -139,15 +132,11 @@ export async function canViewReport(auth: Auth, report: Report) {
|
|||
return true;
|
||||
}
|
||||
|
||||
if ((auth.user.id = report.userId)) {
|
||||
if (auth.user.id == report.userId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (await canViewWebsite(auth, report.websiteId)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return !!(await canViewWebsite(auth, report.websiteId));
|
||||
}
|
||||
|
||||
export async function canUpdateReport(auth: Auth, report: Report) {
|
||||
|
|
@ -155,11 +144,7 @@ export async function canUpdateReport(auth: Auth, report: Report) {
|
|||
return true;
|
||||
}
|
||||
|
||||
if ((auth.user.id = report.userId)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return auth.user.id == report.userId;
|
||||
}
|
||||
|
||||
export async function canCreateTeam({ user }: Auth) {
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import { User, Website } from '@prisma/client';
|
||||
import redis from '@umami/redis-client';
|
||||
import { getSession, getUser, getWebsite } from '../queries';
|
||||
import { getSession, getUserById, getWebsiteById } from '../queries';
|
||||
|
||||
const { fetchObject, storeObject, deleteObject } = redis;
|
||||
|
||||
async function fetchWebsite(id): Promise<Website> {
|
||||
return fetchObject(`website:${id}`, () => getWebsite({ id }));
|
||||
return fetchObject(`website:${id}`, () => getWebsiteById(id));
|
||||
}
|
||||
|
||||
async function storeWebsite(data) {
|
||||
|
|
@ -20,7 +20,7 @@ async function deleteWebsite(id) {
|
|||
}
|
||||
|
||||
async function fetchUser(id): Promise<User> {
|
||||
return fetchObject(`user:${id}`, () => getUser({ id }, { includePassword: true }));
|
||||
return fetchObject(`user:${id}`, () => getUserById(id, { includePassword: true }));
|
||||
}
|
||||
|
||||
async function storeUser(data) {
|
||||
|
|
@ -35,7 +35,7 @@ async function deleteUser(id) {
|
|||
}
|
||||
|
||||
async function fetchSession(id) {
|
||||
return fetchObject(`session:${id}`, () => getSession({ id }));
|
||||
return fetchObject(`session:${id}`, () => getSession(id));
|
||||
}
|
||||
|
||||
async function storeSession(data) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import cache from 'lib/cache';
|
||||
import { getWebsite, getSession, getUser } from 'queries';
|
||||
import { getSession, getUserById, getWebsiteById } from 'queries';
|
||||
import { User, Website, Session } from '@prisma/client';
|
||||
|
||||
export async function loadWebsite(websiteId: string): Promise<Website> {
|
||||
|
|
@ -8,7 +8,7 @@ export async function loadWebsite(websiteId: string): Promise<Website> {
|
|||
if (cache.enabled) {
|
||||
website = await cache.fetchWebsite(websiteId);
|
||||
} else {
|
||||
website = await getWebsite({ id: websiteId });
|
||||
website = await getWebsiteById(websiteId);
|
||||
}
|
||||
|
||||
if (!website || website.deletedAt) {
|
||||
|
|
@ -24,7 +24,7 @@ export async function loadSession(sessionId: string): Promise<Session> {
|
|||
if (cache.enabled) {
|
||||
session = await cache.fetchSession(sessionId);
|
||||
} else {
|
||||
session = await getSession({ id: sessionId });
|
||||
session = await getSession(sessionId);
|
||||
}
|
||||
|
||||
if (!session) {
|
||||
|
|
@ -40,7 +40,7 @@ export async function loadUser(userId: string): Promise<User> {
|
|||
if (cache.enabled) {
|
||||
user = await cache.fetchUser(userId);
|
||||
} else {
|
||||
user = await getUser({ id: userId });
|
||||
user = await getUserById(userId);
|
||||
}
|
||||
|
||||
if (!user || user.deletedAt) {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import { findSession } from 'lib/session';
|
|||
import { getAuthToken, parseShareToken } from 'lib/auth';
|
||||
import { secret, isUuid } from 'lib/crypto';
|
||||
import { ROLES } from 'lib/constants';
|
||||
import { getUser } from '../queries';
|
||||
import { getUserById } from '../queries';
|
||||
import { NextApiRequestCollect } from 'pages/api/send';
|
||||
|
||||
const log = debug('umami:middleware');
|
||||
|
|
@ -53,7 +53,7 @@ export const useAuth = createMiddleware(async (req, res, next) => {
|
|||
const { userId, authKey } = payload || {};
|
||||
|
||||
if (isUuid(userId)) {
|
||||
user = await getUser({ id: userId });
|
||||
user = await getUserById(userId);
|
||||
} else if (redis.enabled && authKey) {
|
||||
user = await redis.get(authKey);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue