mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Add grant to create website/team.
This commit is contained in:
parent
84236c0cd9
commit
280f6a9113
@ -4,11 +4,12 @@ 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 { getTeamUser, getTeamWebsite, findTeamWebsiteByUserId } from 'queries';
|
import { findTeamWebsiteByUserId, getTeamUser, getTeamWebsite, getWebsitesByUserId } from 'queries';
|
||||||
import { loadWebsite } from './load';
|
import { loadWebsite } from './load';
|
||||||
import { Auth } from './types';
|
import { Auth } from './types';
|
||||||
|
|
||||||
const log = debug('umami:auth');
|
const log = debug('umami:auth');
|
||||||
|
const cloudMode = process.env.CLOUD_MODE;
|
||||||
|
|
||||||
export async function setAuthKey(user, expire = 0) {
|
export async function setAuthKey(user, expire = 0) {
|
||||||
const authKey = `auth:${getRandomChars(32)}`;
|
const authKey = `auth:${getRandomChars(32)}`;
|
||||||
@ -57,7 +58,15 @@ export async function canViewWebsite({ user, shareToken }: Auth, websiteId: stri
|
|||||||
return !!(await findTeamWebsiteByUserId(websiteId, user.id));
|
return !!(await findTeamWebsiteByUserId(websiteId, user.id));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function canCreateWebsite({ user }: Auth) {
|
export async function canCreateWebsite({ user, grant }: Auth) {
|
||||||
|
if (cloudMode) {
|
||||||
|
if (grant.find(a => a === PERMISSIONS.websiteCreate)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (await getWebsitesByUserId(user.id)).count < Number(process.env.WEBSITE_LIMIT);
|
||||||
|
}
|
||||||
|
|
||||||
if (user.isAdmin) {
|
if (user.isAdmin) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -109,7 +118,15 @@ export async function canDeleteReport(auth: Auth, report: Report) {
|
|||||||
return canUpdateReport(auth, report);
|
return canUpdateReport(auth, report);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function canCreateTeam({ user }: Auth) {
|
export async function canCreateTeam({ user, grant }: Auth) {
|
||||||
|
if (cloudMode) {
|
||||||
|
if (grant.find(a => a === PERMISSIONS.teamCreate)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (user.isAdmin) {
|
if (user.isAdmin) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ export const useAuth = createMiddleware(async (req, res, next) => {
|
|||||||
const shareToken = await parseShareToken(req);
|
const shareToken = await parseShareToken(req);
|
||||||
|
|
||||||
let user = null;
|
let user = null;
|
||||||
const { userId, authKey } = payload || {};
|
const { userId, authKey, grant } = payload || {};
|
||||||
|
|
||||||
if (isUuid(userId)) {
|
if (isUuid(userId)) {
|
||||||
user = await getUserById(userId);
|
user = await getUserById(userId);
|
||||||
@ -72,7 +72,13 @@ export const useAuth = createMiddleware(async (req, res, next) => {
|
|||||||
user.isAdmin = user.role === ROLES.admin;
|
user.isAdmin = user.role === ROLES.admin;
|
||||||
}
|
}
|
||||||
|
|
||||||
(req as any).auth = { user, token, shareToken, authKey };
|
(req as any).auth = {
|
||||||
|
user,
|
||||||
|
grant,
|
||||||
|
token,
|
||||||
|
shareToken,
|
||||||
|
authKey,
|
||||||
|
};
|
||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
@ -4,6 +4,7 @@ import {
|
|||||||
DATA_TYPE,
|
DATA_TYPE,
|
||||||
EVENT_TYPE,
|
EVENT_TYPE,
|
||||||
KAFKA_TOPIC,
|
KAFKA_TOPIC,
|
||||||
|
PERMISSIONS,
|
||||||
REPORT_FILTER_TYPES,
|
REPORT_FILTER_TYPES,
|
||||||
REPORT_TYPES,
|
REPORT_TYPES,
|
||||||
ROLES,
|
ROLES,
|
||||||
@ -17,6 +18,7 @@ import { TIME_UNIT } from './date';
|
|||||||
type ObjectValues<T> = T[keyof T];
|
type ObjectValues<T> = T[keyof T];
|
||||||
|
|
||||||
export type TimeUnit = ObjectValues<typeof TIME_UNIT>;
|
export type TimeUnit = ObjectValues<typeof TIME_UNIT>;
|
||||||
|
export type Permission = ObjectValues<typeof PERMISSIONS>;
|
||||||
|
|
||||||
export type CollectionType = ObjectValues<typeof COLLECTION_TYPE>;
|
export type CollectionType = ObjectValues<typeof COLLECTION_TYPE>;
|
||||||
export type Role = ObjectValues<typeof ROLES>;
|
export type Role = ObjectValues<typeof ROLES>;
|
||||||
@ -78,6 +80,7 @@ export interface Auth {
|
|||||||
role: string;
|
role: string;
|
||||||
isAdmin: boolean;
|
isAdmin: boolean;
|
||||||
};
|
};
|
||||||
|
grant?: Permission[];
|
||||||
shareToken?: {
|
shareToken?: {
|
||||||
websiteId: string;
|
websiteId: string;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user