mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 12:47:13 +01:00
Updated roles and permissions logic.
This commit is contained in:
parent
4eb3140e43
commit
b57ecf33e6
63 changed files with 432 additions and 546 deletions
|
|
@ -1,14 +1,8 @@
|
|||
import { Prisma, Team } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createTeam(
|
||||
data: Prisma.TeamCreateInput,
|
||||
searchDeleted = false,
|
||||
): Promise<Team> {
|
||||
return prisma.client.team.create({
|
||||
data: { ...data, isDeleted: searchDeleted ? null : false },
|
||||
});
|
||||
}
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { ROLES } from 'lib/constants';
|
||||
import { Website } from 'lib/types';
|
||||
|
||||
export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> {
|
||||
return prisma.client.team.findFirst({
|
||||
|
|
@ -22,19 +16,35 @@ export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
|
|||
});
|
||||
}
|
||||
|
||||
export async function getTeamsByUserId(userId: string): Promise<Team[]> {
|
||||
return prisma.client.teamUser
|
||||
.findMany({
|
||||
where: {
|
||||
export async function getTeamWebsites(teamId: string): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
teamId,
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
export async function createTeam(data: Prisma.TeamCreateInput): Promise<Team> {
|
||||
const { id, userId } = data;
|
||||
|
||||
return prisma.transaction([
|
||||
prisma.client.team.create({
|
||||
data,
|
||||
}),
|
||||
prisma.client.teamUser.create({
|
||||
data: {
|
||||
id: uuid(),
|
||||
teamId: id,
|
||||
userId,
|
||||
role: ROLES.teamOwner,
|
||||
},
|
||||
include: {
|
||||
team: true,
|
||||
},
|
||||
})
|
||||
.then(data => {
|
||||
return data.map(a => a.team);
|
||||
});
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
export async function updateTeam(
|
||||
|
|
@ -42,7 +52,10 @@ export async function updateTeam(
|
|||
where: Prisma.TeamWhereUniqueInput,
|
||||
): Promise<Team> {
|
||||
return prisma.client.team.update({
|
||||
data,
|
||||
data: {
|
||||
...data,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
|
@ -50,7 +63,7 @@ export async function updateTeam(
|
|||
export async function deleteTeam(teamId: string): Promise<Team> {
|
||||
return prisma.client.team.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: {
|
||||
id: teamId,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,23 @@ import { Prisma, TeamUser } from '@prisma/client';
|
|||
import { uuid } from 'lib/crypto';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getTeamUser(teamId: string, userId: string): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.findFirst({
|
||||
where: {
|
||||
teamId,
|
||||
userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeamUsers(teamId: string): Promise<TeamUser[]> {
|
||||
return prisma.client.teamUser.findMany({
|
||||
where: {
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function createTeamUser(
|
||||
userId: string,
|
||||
teamId: string,
|
||||
|
|
@ -17,18 +34,6 @@ export async function createTeamUser(
|
|||
});
|
||||
}
|
||||
|
||||
export async function getTeamUser(where: Prisma.TeamUserWhereInput): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.findFirst({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeamUsers(where: Prisma.TeamUserWhereInput): Promise<TeamUser[]> {
|
||||
return prisma.client.teamUser.findMany({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateTeamUser(
|
||||
data: Prisma.TeamUserUpdateInput,
|
||||
where: Prisma.TeamUserWhereUniqueInput,
|
||||
|
|
@ -42,7 +47,7 @@ export async function updateTeamUser(
|
|||
export async function deleteTeamUser(teamUserId: string): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: {
|
||||
id: teamUserId,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { Prisma } from '@prisma/client';
|
||||
import { Prisma, Team } from '@prisma/client';
|
||||
import cache from 'lib/cache';
|
||||
import prisma from 'lib/prisma';
|
||||
import { Website } from 'lib/types';
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
|
|
@ -9,36 +10,19 @@ export interface User {
|
|||
createdAt?: Date;
|
||||
}
|
||||
|
||||
export async function createUser(data: {
|
||||
id: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}): Promise<{
|
||||
id: string;
|
||||
username: string;
|
||||
}> {
|
||||
return prisma.client.user.create({
|
||||
data,
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUser(
|
||||
where: Prisma.UserWhereUniqueInput,
|
||||
includePassword = false,
|
||||
options: { includePassword?: boolean } = {},
|
||||
): Promise<User> {
|
||||
const { includePassword = false } = options;
|
||||
|
||||
return prisma.client.user.findUnique({
|
||||
where,
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
userRole: {
|
||||
select: { role: true },
|
||||
},
|
||||
password: includePassword,
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -53,24 +37,56 @@ export async function getUsers(): Promise<User[]> {
|
|||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
role: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUsersByTeamId(teamId): Promise<User[]> {
|
||||
return prisma.client.user.findMany({
|
||||
where: {
|
||||
teamUser: {
|
||||
every: {
|
||||
teamId,
|
||||
},
|
||||
export async function getUserTeams(userId: string): Promise<Team[]> {
|
||||
return prisma.client.teamUser
|
||||
.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
include: {
|
||||
team: true,
|
||||
},
|
||||
})
|
||||
.then(data => {
|
||||
return data.map(a => a.team);
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserWebsites(userId: string): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
export async function createUser(data: {
|
||||
id: string;
|
||||
username: string;
|
||||
password: string;
|
||||
role: string;
|
||||
}): Promise<{
|
||||
id: string;
|
||||
username: string;
|
||||
role: string;
|
||||
}> {
|
||||
return prisma.client.user.create({
|
||||
data,
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
createdAt: true,
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -85,8 +101,8 @@ export async function updateUser(
|
|||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
role: true,
|
||||
createdAt: true,
|
||||
userRole: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -106,8 +122,8 @@ export async function deleteUser(
|
|||
websiteIds = websites.map(a => a.id);
|
||||
}
|
||||
|
||||
return client
|
||||
.$transaction([
|
||||
return prisma
|
||||
.transaction([
|
||||
client.websiteEvent.deleteMany({
|
||||
where: { websiteId: { in: websiteIds } },
|
||||
}),
|
||||
|
|
@ -116,13 +132,13 @@ export async function deleteUser(
|
|||
}),
|
||||
client.website.updateMany({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: { id: { in: websiteIds } },
|
||||
}),
|
||||
client.user.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: {
|
||||
id: userId,
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
import { Prisma, UserRole } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createUserRole(
|
||||
data: Prisma.UserRoleCreateInput | Prisma.UserRoleUncheckedCreateInput,
|
||||
): Promise<UserRole> {
|
||||
return prisma.client.userRole.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserRole(where: Prisma.UserRoleWhereInput): Promise<UserRole> {
|
||||
return prisma.client.userRole.findFirst({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserRoles(where: Prisma.UserRoleWhereInput): Promise<UserRole[]> {
|
||||
return prisma.client.userRole.findMany({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserRolesByUserId(userId: string): Promise<UserRole[]> {
|
||||
return prisma.client.userRole.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateUserRole(
|
||||
data: Prisma.UserRoleUpdateInput,
|
||||
where: Prisma.UserRoleWhereUniqueInput,
|
||||
): Promise<UserRole> {
|
||||
return prisma.client.userRole.update({
|
||||
data,
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteUserRole(userRoleId: string): Promise<UserRole> {
|
||||
return prisma.client.userRole.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
},
|
||||
where: {
|
||||
id: userRoleId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -3,6 +3,20 @@ import cache from 'lib/cache';
|
|||
import prisma from 'lib/prisma';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
|
||||
return prisma.client.website.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getWebsites(): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function createWebsite(
|
||||
data: Prisma.WebsiteCreateInput | Prisma.WebsiteUncheckedCreateInput,
|
||||
): Promise<Website> {
|
||||
|
|
@ -55,44 +69,6 @@ export async function resetWebsite(
|
|||
});
|
||||
}
|
||||
|
||||
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
|
||||
return prisma.client.website.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getWebsitesByUserId(userId): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getWebsitesByTeamId(teamId): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
teamId,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAllWebsites(): Promise<Website[]> {
|
||||
return await prisma.client.website.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteWebsite(websiteId: string) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => deleteWebsiteRelationalQuery(websiteId),
|
||||
|
|
@ -127,7 +103,7 @@ async function deleteWebsiteRelationalQuery(
|
|||
async function deleteWebsiteClickhouseQuery(websiteId): Promise<Website> {
|
||||
return prisma.client.website.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: { id: websiteId },
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue