mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Add permission checks.
This commit is contained in:
parent
51e2331315
commit
78225691df
20 changed files with 225 additions and 333 deletions
|
|
@ -19,6 +19,27 @@ export async function getPermissions(where: Prisma.PermissionWhereInput): Promis
|
|||
});
|
||||
}
|
||||
|
||||
export async function getPermissionsByUserId(userId, name?: string): Promise<Permission[]> {
|
||||
return prisma.client.permission.findMany({
|
||||
where: {
|
||||
...(name ? { name } : {}),
|
||||
RolePermission: {
|
||||
every: {
|
||||
role: {
|
||||
is: {
|
||||
userRoles: {
|
||||
every: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updatePermission(
|
||||
data: Prisma.PermissionUpdateInput,
|
||||
where: Prisma.PermissionWhereUniqueInput,
|
||||
|
|
|
|||
|
|
@ -1,51 +1,54 @@
|
|||
import { Prisma, Team, TeamUser } from '@prisma/client';
|
||||
import { Prisma, Team } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createTeam(data: Prisma.TeamCreateInput): Promise<Team> {
|
||||
return prisma.client.role.create({
|
||||
data,
|
||||
export async function createTeam(
|
||||
data: Prisma.TeamCreateInput,
|
||||
searchDeleted = false,
|
||||
): Promise<Team> {
|
||||
return prisma.client.team.create({
|
||||
data: { ...data, isDeleted: searchDeleted ? null : false },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeam(where: Prisma.TeamWhereUniqueInput): Promise<Team> {
|
||||
return prisma.client.role.findUnique({
|
||||
export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> {
|
||||
return prisma.client.team.findFirst({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
|
||||
return prisma.client.role.findMany({
|
||||
return prisma.client.team.findMany({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeamsByUserId(userId: string): Promise<
|
||||
(TeamUser & {
|
||||
team: Team;
|
||||
})[]
|
||||
> {
|
||||
return prisma.client.teamUser.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
include: {
|
||||
team: true,
|
||||
},
|
||||
});
|
||||
export async function getTeamsByUserId(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 updateTeam(
|
||||
data: Prisma.TeamUpdateInput,
|
||||
where: Prisma.TeamWhereUniqueInput,
|
||||
): Promise<Team> {
|
||||
return prisma.client.role.update({
|
||||
return prisma.client.team.update({
|
||||
data,
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteTeam(teamId: string): Promise<Team> {
|
||||
return prisma.client.role.update({
|
||||
return prisma.client.team.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ export async function createTeamUser(
|
|||
});
|
||||
}
|
||||
|
||||
export async function getTeamUser(where: Prisma.TeamUserWhereUniqueInput): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.findUnique({
|
||||
export async function getTeamUser(where: Prisma.TeamUserWhereInput): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.findFirst({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
import { Prisma, TeamWebsite } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createTeamWebsite(
|
||||
data: Prisma.TeamWebsiteCreateInput | Prisma.TeamWebsiteUncheckedCreateInput,
|
||||
): Promise<TeamWebsite> {
|
||||
return prisma.client.teamWebsite.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeamWebsite(
|
||||
where: Prisma.TeamWebsiteWhereUniqueInput,
|
||||
): Promise<TeamWebsite> {
|
||||
return prisma.client.teamWebsite.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeamWebsites(where: Prisma.TeamWebsiteWhereInput): Promise<TeamWebsite[]> {
|
||||
return prisma.client.teamWebsite.findMany({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateTeamWebsite(
|
||||
data: Prisma.TeamWebsiteUpdateInput,
|
||||
where: Prisma.TeamWebsiteWhereUniqueInput,
|
||||
): Promise<TeamWebsite> {
|
||||
return prisma.client.teamWebsite.update({
|
||||
data,
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteTeamWebsite(teamWebsiteId: string): Promise<TeamWebsite> {
|
||||
return prisma.client.teamWebsite.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
},
|
||||
where: {
|
||||
id: teamWebsiteId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
import { Prisma } from '@prisma/client';
|
||||
import { UmamiApi } from 'lib/constants';
|
||||
import cache from 'lib/cache';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
|
|
@ -99,14 +98,14 @@ export async function deleteUser(
|
|||
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> {
|
||||
const { client } = prisma;
|
||||
|
||||
const websites = await client.userWebsite.findMany({
|
||||
const websites = await client.website.findMany({
|
||||
where: { userId },
|
||||
});
|
||||
|
||||
let websiteIds = [];
|
||||
|
||||
if (websites.length > 0) {
|
||||
websiteIds = websites.map(a => a.websiteId);
|
||||
websiteIds = websites.map(a => a.id);
|
||||
}
|
||||
|
||||
return client
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ export async function createUserRole(
|
|||
});
|
||||
}
|
||||
|
||||
export async function getUserRole(where: Prisma.UserRoleWhereUniqueInput): Promise<UserRole> {
|
||||
return prisma.client.userRole.findUnique({
|
||||
export async function getUserRole(where: Prisma.UserRoleWhereInput): Promise<UserRole> {
|
||||
return prisma.client.userRole.findFirst({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,43 +0,0 @@
|
|||
import { Prisma, UserWebsite } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createUserWebsite(
|
||||
data: Prisma.UserWebsiteCreateInput | Prisma.UserWebsiteUncheckedCreateInput,
|
||||
): Promise<UserWebsite> {
|
||||
return prisma.client.userWebsite.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserWebsite(where: Prisma.UserWebsiteWhereInput): Promise<UserWebsite> {
|
||||
return prisma.client.userWebsite.findFirst({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserWebsites(where: Prisma.UserWebsiteWhereInput): Promise<UserWebsite[]> {
|
||||
return prisma.client.userWebsite.findMany({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateUserWebsite(
|
||||
data: Prisma.UserWebsiteUpdateInput,
|
||||
where: Prisma.UserWebsiteWhereUniqueInput,
|
||||
): Promise<UserWebsite> {
|
||||
return prisma.client.userWebsite.update({
|
||||
data,
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteUserWebsite(userWebsiteId: string): Promise<UserWebsite> {
|
||||
return prisma.client.userWebsite.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
},
|
||||
where: {
|
||||
id: userWebsiteId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -3,54 +3,12 @@ import cache from 'lib/cache';
|
|||
import prisma from 'lib/prisma';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function createWebsiteByUser(
|
||||
userId: string,
|
||||
data: {
|
||||
id: string;
|
||||
name: string;
|
||||
domain: string;
|
||||
shareId?: string;
|
||||
},
|
||||
export async function createWebsite(
|
||||
data: Prisma.WebsiteCreateInput | Prisma.WebsiteUncheckedCreateInput,
|
||||
): Promise<Website> {
|
||||
return prisma.client.website
|
||||
.create({
|
||||
data: {
|
||||
userWebsite: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
...data,
|
||||
},
|
||||
})
|
||||
.then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.storeWebsite(data);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
export async function createWebsiteByTeam(
|
||||
teamId: string,
|
||||
data: {
|
||||
id: string;
|
||||
name: string;
|
||||
domain: string;
|
||||
shareId?: string;
|
||||
},
|
||||
): Promise<Website> {
|
||||
return prisma.client.website
|
||||
.create({
|
||||
data: {
|
||||
teamWebsite: {
|
||||
connect: {
|
||||
id: teamId,
|
||||
},
|
||||
},
|
||||
...data,
|
||||
},
|
||||
data,
|
||||
})
|
||||
.then(async data => {
|
||||
if (cache.enabled) {
|
||||
|
|
@ -103,11 +61,7 @@ export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise
|
|||
export async function getWebsitesByUserId(userId): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
userWebsite: {
|
||||
every: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
userId,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
|
|
@ -118,11 +72,7 @@ export async function getWebsitesByUserId(userId): Promise<Website[]> {
|
|||
export async function getWebsitesByTeamId(teamId): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
teamWebsite: {
|
||||
every: {
|
||||
teamId,
|
||||
},
|
||||
},
|
||||
teamId,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
|
|
@ -130,35 +80,26 @@ export async function getWebsitesByTeamId(teamId): Promise<Website[]> {
|
|||
});
|
||||
}
|
||||
|
||||
export async function getAllWebsites(): Promise<(Website & { user: string })[]> {
|
||||
return await prisma.client.website
|
||||
.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
include: {
|
||||
userWebsite: {
|
||||
include: {
|
||||
user: true,
|
||||
},
|
||||
},
|
||||
export async function getAllWebsites(): Promise<Website[]> {
|
||||
return await prisma.client.website.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
})
|
||||
.then(data => data.map(i => ({ ...i, user: i.userWebsite[0]?.userId })));
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteWebsite(
|
||||
websiteId: string,
|
||||
) {
|
||||
export async function deleteWebsite(websiteId: string) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => deleteWebsiteRelationalQuery(websiteId),
|
||||
[CLICKHOUSE]: () => deleteWebsiteClickhouseQuery(websiteId),
|
||||
});
|
||||
}
|
||||
|
||||
async function deleteWebsiteRelationalQuery(websiteId): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
||||
async function deleteWebsiteRelationalQuery(
|
||||
websiteId,
|
||||
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
||||
const { client, transaction } = prisma;
|
||||
|
||||
return transaction([
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue