mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
commit
3e71994767
@ -4,7 +4,7 @@ import debug from 'debug';
|
|||||||
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER } from 'lib/constants';
|
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER } from 'lib/constants';
|
||||||
import { secret } from 'lib/crypto';
|
import { secret } from 'lib/crypto';
|
||||||
import { createSecureToken, ensureArray, getRandomChars, parseToken } from 'next-basics';
|
import { createSecureToken, ensureArray, getRandomChars, parseToken } from 'next-basics';
|
||||||
import { findTeamWebsiteByUserId, getTeamUser, getTeamWebsite, getWebsitesByUserId } from 'queries';
|
import { findTeamWebsiteByUserId, getTeamUser, getTeamWebsite } from 'queries';
|
||||||
import { loadWebsite } from './load';
|
import { loadWebsite } from './load';
|
||||||
import { Auth } from './types';
|
import { Auth } from './types';
|
||||||
|
|
||||||
@ -60,11 +60,11 @@ export async function canViewWebsite({ user, shareToken }: Auth, websiteId: stri
|
|||||||
|
|
||||||
export async function canCreateWebsite({ user, grant }: Auth) {
|
export async function canCreateWebsite({ user, grant }: Auth) {
|
||||||
if (cloudMode) {
|
if (cloudMode) {
|
||||||
if (grant.find(a => a === PERMISSIONS.websiteCreate)) {
|
if (grant?.find(a => a === PERMISSIONS.websiteCreate)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (await getWebsitesByUserId(user.id)).count < Number(process.env.WEBSITE_LIMIT);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user.isAdmin) {
|
if (user.isAdmin) {
|
||||||
@ -120,7 +120,7 @@ export async function canDeleteReport(auth: Auth, report: Report) {
|
|||||||
|
|
||||||
export async function canCreateTeam({ user, grant }: Auth) {
|
export async function canCreateTeam({ user, grant }: Auth) {
|
||||||
if (cloudMode) {
|
if (cloudMode) {
|
||||||
if (grant.find(a => a === PERMISSIONS.teamCreate)) {
|
if (grant?.find(a => a === PERMISSIONS.teamCreate)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,17 +2,20 @@ import { User, Website } from '@prisma/client';
|
|||||||
import redis from '@umami/redis-client';
|
import redis from '@umami/redis-client';
|
||||||
import { getSession, getUserById, getWebsiteById } from '../queries';
|
import { getSession, getUserById, getWebsiteById } from '../queries';
|
||||||
|
|
||||||
const { fetchObject, storeObject, deleteObject } = redis;
|
const { fetchObject, storeObject, deleteObject, expire } = redis;
|
||||||
|
|
||||||
async function fetchWebsite(id): Promise<Website> {
|
async function fetchWebsite(id): Promise<Website> {
|
||||||
return fetchObject(`website:${id}`, () => getWebsiteById(id));
|
return fetchObject(`website:${id}`, () => getWebsiteById(id), 86400);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function storeWebsite(data) {
|
async function storeWebsite(data) {
|
||||||
const { id } = data;
|
const { id } = data;
|
||||||
const key = `website:${id}`;
|
const key = `website:${id}`;
|
||||||
|
|
||||||
return storeObject(key, data);
|
const obj = await storeObject(key, data);
|
||||||
|
await expire(key, 86400);
|
||||||
|
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteWebsite(id) {
|
async function deleteWebsite(id) {
|
||||||
@ -20,14 +23,17 @@ async function deleteWebsite(id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function fetchUser(id): Promise<User> {
|
async function fetchUser(id): Promise<User> {
|
||||||
return fetchObject(`user:${id}`, () => getUserById(id, { includePassword: true }));
|
return fetchObject(`user:${id}`, () => getUserById(id, { includePassword: true }), 86400);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function storeUser(data) {
|
async function storeUser(data) {
|
||||||
const { id } = data;
|
const { id } = data;
|
||||||
const key = `user:${id}`;
|
const key = `user:${id}`;
|
||||||
|
|
||||||
return storeObject(key, data);
|
const obj = await storeObject(key, data);
|
||||||
|
await expire(key, 86400);
|
||||||
|
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteUser(id) {
|
async function deleteUser(id) {
|
||||||
@ -35,14 +41,17 @@ async function deleteUser(id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function fetchSession(id) {
|
async function fetchSession(id) {
|
||||||
return fetchObject(`session:${id}`, () => getSession(id));
|
return fetchObject(`session:${id}`, () => getSession(id), 86400);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function storeSession(data) {
|
async function storeSession(data) {
|
||||||
const { id } = data;
|
const { id } = data;
|
||||||
const key = `session:${id}`;
|
const key = `session:${id}`;
|
||||||
|
|
||||||
return storeObject(key, data);
|
const obj = await storeObject(key, data);
|
||||||
|
await expire(key, 86400);
|
||||||
|
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteSession(id) {
|
async function deleteSession(id) {
|
||||||
|
@ -1,12 +1,27 @@
|
|||||||
import { secret, uuid, isUuid } from 'lib/crypto';
|
import { isUuid, secret, uuid } from 'lib/crypto';
|
||||||
import { getClientInfo, getJsonBody } from 'lib/detect';
|
import { getClientInfo, getJsonBody } from 'lib/detect';
|
||||||
import { parseToken } from 'next-basics';
|
import { parseToken } from 'next-basics';
|
||||||
import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send';
|
import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send';
|
||||||
import { createSession } from 'queries';
|
import { createSession } from 'queries';
|
||||||
import cache from './cache';
|
import cache from './cache';
|
||||||
|
import clickhouse from './clickhouse';
|
||||||
import { loadSession, loadWebsite } from './load';
|
import { loadSession, loadWebsite } from './load';
|
||||||
|
|
||||||
export async function findSession(req: NextApiRequestCollect) {
|
export async function findSession(req: NextApiRequestCollect): Promise<{
|
||||||
|
id: any;
|
||||||
|
websiteId: string;
|
||||||
|
hostname: string;
|
||||||
|
browser: string;
|
||||||
|
os: any;
|
||||||
|
device: string;
|
||||||
|
screen: string;
|
||||||
|
language: string;
|
||||||
|
country: any;
|
||||||
|
subdivision1: any;
|
||||||
|
subdivision2: any;
|
||||||
|
city: any;
|
||||||
|
ownerId: string;
|
||||||
|
}> {
|
||||||
const { payload } = getJsonBody<CollectRequestBody>(req);
|
const { payload } = getJsonBody<CollectRequestBody>(req);
|
||||||
|
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
@ -53,6 +68,25 @@ export async function findSession(req: NextApiRequestCollect) {
|
|||||||
|
|
||||||
const sessionId = uuid(websiteId, hostname, ip, userAgent);
|
const sessionId = uuid(websiteId, hostname, ip, userAgent);
|
||||||
|
|
||||||
|
// Clickhouse does not require session lookup
|
||||||
|
if (clickhouse.enabled) {
|
||||||
|
return {
|
||||||
|
id: sessionId,
|
||||||
|
websiteId,
|
||||||
|
hostname,
|
||||||
|
browser,
|
||||||
|
os: os as any,
|
||||||
|
device,
|
||||||
|
screen,
|
||||||
|
language,
|
||||||
|
country,
|
||||||
|
subdivision1,
|
||||||
|
subdivision2,
|
||||||
|
city,
|
||||||
|
ownerId: website.userId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Find session
|
// Find session
|
||||||
let session = await loadSession(sessionId);
|
let session = await loadSession(sessionId);
|
||||||
|
|
||||||
|
@ -23,6 +23,6 @@
|
|||||||
"jsx": "preserve",
|
"jsx": "preserve",
|
||||||
"incremental": false
|
"incremental": false
|
||||||
},
|
},
|
||||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
|
"include": ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx", "next-env.d.ts"],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user