Merge branch 'dev' into feat/um-114-roles-and-permissions

This commit is contained in:
Brian Cao 2022-12-01 20:30:46 -08:00
parent 3085bdd713
commit ab9f403448
28 changed files with 345 additions and 412 deletions

View file

@ -1,7 +1,6 @@
import { Website } from 'interface/api/models';
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
import { allowQuery } from 'lib/auth';
import { UmamiApi } from 'lib/constants';
import { canViewWebsite, canUpdateWebsite, canDeleteWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
@ -24,19 +23,26 @@ export default async (
await useCors(req, res);
await useAuth(req, res);
const {
user: { id: userId },
} = req.auth;
const { id: websiteId } = req.query;
if (!(await allowQuery(req, UmamiApi.AuthType.Website))) {
return unauthorized(res);
}
if (req.method === 'GET') {
if (!(await canViewWebsite(userId, websiteId))) {
return unauthorized(res);
}
const website = await getWebsite({ id: websiteId });
return ok(res, website);
}
if (req.method === 'POST') {
if (!(await canUpdateWebsite(userId, websiteId))) {
return unauthorized(res);
}
const { name, domain, shareId } = req.body;
try {
@ -51,6 +57,10 @@ export default async (
}
if (req.method === 'DELETE') {
if (!(await canDeleteWebsite(userId, websiteId))) {
return unauthorized(res);
}
await deleteWebsite(websiteId);
return ok(res);