Refactored permissions check. Updated redis lib.

This commit is contained in:
Mike Cao 2022-12-01 10:58:50 -08:00
parent 1a7369e1f6
commit a4e80ca3e5
5 changed files with 58 additions and 15 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 } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
@ -26,17 +25,21 @@ export default async (
const { id: websiteId } = req.query;
if (!(await allowQuery(req, UmamiApi.AuthType.Website))) {
return unauthorized(res);
}
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth.user.id, websiteId))) {
return unauthorized(res);
}
const website = await getWebsite({ id: websiteId });
return ok(res, website);
}
if (req.method === 'POST') {
if (!(await canUpdateWebsite(req.auth.user.id, websiteId))) {
return unauthorized(res);
}
const { name, domain, shareId } = req.body;
try {