mirror of
https://github.com/umami-software/umami.git
synced 2026-02-07 06:07:17 +01:00
Feat/um 114 roles and permissions (#1683)
* Auth checkpoint. * Merge branch 'dev' into feat/um-114-roles-and-permissions
This commit is contained in:
parent
a4e80ca3e5
commit
06bebadbb9
27 changed files with 331 additions and 482 deletions
|
|
@ -1,6 +1,5 @@
|
|||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { checkPermission } from 'lib/auth';
|
||||
import { UmamiApi } from 'lib/constants';
|
||||
import { canDeleteUser, canUpdateUser, canViewUser, checkAdmin } from 'lib/auth';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
|
|
@ -27,7 +26,7 @@ export default async (
|
|||
const { id } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (id !== userId) {
|
||||
if (await canViewUser(userId, id)) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
|
|
@ -37,12 +36,12 @@ export default async (
|
|||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { username, password } = req.body;
|
||||
|
||||
if (id !== userId) {
|
||||
if (await canUpdateUser(userId, id)) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const { username, password } = req.body;
|
||||
|
||||
const user = await getUser({ id });
|
||||
|
||||
const data: any = {};
|
||||
|
|
@ -52,7 +51,7 @@ export default async (
|
|||
}
|
||||
|
||||
// Only admin can change these fields
|
||||
if (!(await checkPermission(req, UmamiApi.Permission.Admin))) {
|
||||
if (username && (await checkAdmin(userId))) {
|
||||
data.username = username;
|
||||
}
|
||||
|
||||
|
|
@ -71,12 +70,12 @@ export default async (
|
|||
}
|
||||
|
||||
if (req.method === 'DELETE') {
|
||||
if (id === userId) {
|
||||
return badRequest(res, 'You cannot delete your own user.');
|
||||
if (canDeleteUser(userId)) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
if (!(await checkPermission(req, UmamiApi.Permission.Admin))) {
|
||||
return unauthorized(res);
|
||||
if (id === userId) {
|
||||
return badRequest(res, 'You cannot delete your own user.');
|
||||
}
|
||||
|
||||
await deleteUser(id);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { allowQuery } from 'lib/auth';
|
||||
import { UmamiApi } from 'lib/constants';
|
||||
import { canUpdateUser } from 'lib/auth';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import {
|
||||
|
|
@ -30,12 +29,15 @@ export default async (
|
|||
|
||||
const { current_password, new_password } = req.body;
|
||||
const { id } = req.query;
|
||||
|
||||
if (!(await allowQuery(req, UmamiApi.AuthType.User))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
const {
|
||||
user: { id: userId },
|
||||
} = req.auth;
|
||||
|
||||
if (req.method === 'POST') {
|
||||
if (canUpdateUser(userId, id)) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const user = await getUser({ id });
|
||||
|
||||
if (!checkPassword(current_password, user.password)) {
|
||||
|
|
|
|||
|
|
@ -1,20 +1,17 @@
|
|||
import { UserRole } from '@prisma/client';
|
||||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { checkPermission } from 'lib/auth';
|
||||
import { canUpdateUserRole } from 'lib/auth';
|
||||
import { UmamiApi } from 'lib/constants';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { createUserRole, deleteUserRole, getUserRole, getUserRoles } from 'queries';
|
||||
import { deleteUserRole, getUserRole, getUserRoles, updateUserRole } from 'queries';
|
||||
|
||||
export interface UserRoleRequestQuery {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface UserRoleRequestBody {
|
||||
roleId: string;
|
||||
teamId?: string;
|
||||
role: UmamiApi.Role;
|
||||
userRoleId?: string;
|
||||
}
|
||||
|
||||
|
|
@ -29,7 +26,7 @@ export default async (
|
|||
} = req.auth;
|
||||
const { id } = req.query;
|
||||
|
||||
if (id !== userId || !(await checkPermission(req, UmamiApi.Permission.Admin))) {
|
||||
if (await canUpdateUserRole(userId)) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
|
|
@ -40,17 +37,17 @@ export default async (
|
|||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { roleId, teamId } = req.body;
|
||||
const { role } = req.body;
|
||||
|
||||
const userRole = getUserRole({ userId: id, roleId, teamId });
|
||||
const userRole = await getUserRole({ userId: id });
|
||||
|
||||
if (userRole) {
|
||||
if (userRole && userRole.role === role) {
|
||||
return badRequest(res, 'Role already exists for User.');
|
||||
} else {
|
||||
const updated = await updateUserRole({ role }, { id: userRole.id });
|
||||
|
||||
return ok(res, updated);
|
||||
}
|
||||
|
||||
const updated = await createUserRole({ id: uuid(), userId: id, roleId, teamId });
|
||||
|
||||
return ok(res, updated);
|
||||
}
|
||||
|
||||
if (req.method === 'DELETE') {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { checkPermission } from 'lib/auth';
|
||||
import { UmamiApi } from 'lib/constants';
|
||||
import { canCreateUser, canViewUsers } from 'lib/auth';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
|
|
@ -19,17 +18,25 @@ export default async (
|
|||
) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
if (!(await checkPermission(req, UmamiApi.Permission.Admin))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
const {
|
||||
user: { id: userId },
|
||||
} = req.auth;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (canViewUsers(userId)) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const users = await getUsers();
|
||||
|
||||
return ok(res, users);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
if (canCreateUser(userId)) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const { username, password, id } = req.body;
|
||||
|
||||
const user = await getUser({ username });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue