mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
8732d056dd
* Initial Typescript models. * Re-add realtime data * get distinct sessions for session metrics * Add queries for new schema. * Fix Typo. * Add some api/team endpoints. * Fix destructure error. * Fix getWebsites call. * Ignore typescript build errors. * Fix enum issue. * add clickhouse route to deleteWebsite * Fix Website auth. * Updated lint-staged config. * Add permission checks. * Add user role api. * Fix error when updating website. * Fix isAdmin check. Fix Schema. * Initial conversion to react-basics. * Remove user/team transfer from website update. * delete website in relational query * Fix login secure token creation. * Add event type to event. * Allow user to be added to team with role. * Updated login form. * Add Role to TeamUser. * Add database migration. * Refactored permissions check. Updated redis lib. * Feat/um 114 roles and permissions (#1683) * Auth checkpoint. * Merge branch 'dev' into feat/um-114-roles-and-permissions * Add 02 migration. * Added lib/types. * Updated schema. * Updated roles and permissions logic. * Implement react-basics styles. Fix queries. * Update website details layout. * Add 01 migration. * Fix admin create. * Update react-basics. Co-authored-by: Francis Cao <franciscao@gmail.com> Co-authored-by: Mike Cao <mike@mikecao.com> Co-authored-by: Mike Cao <moocao@gmail.com>
160 lines
3.0 KiB
TypeScript
160 lines
3.0 KiB
TypeScript
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;
|
|
username: string;
|
|
password?: string;
|
|
createdAt?: Date;
|
|
}
|
|
|
|
export async function getUser(
|
|
where: Prisma.UserWhereUniqueInput,
|
|
options: { includePassword?: boolean } = {},
|
|
): Promise<User> {
|
|
const { includePassword = false } = options;
|
|
|
|
return prisma.client.user.findUnique({
|
|
where,
|
|
select: {
|
|
id: true,
|
|
username: true,
|
|
password: includePassword,
|
|
role: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getUsers(): Promise<User[]> {
|
|
return prisma.client.user.findMany({
|
|
orderBy: [
|
|
{
|
|
username: 'asc',
|
|
},
|
|
],
|
|
select: {
|
|
id: true,
|
|
username: true,
|
|
role: true,
|
|
createdAt: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
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,
|
|
role: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function updateUser(
|
|
data: Prisma.UserUpdateInput,
|
|
where: Prisma.UserWhereUniqueInput,
|
|
): Promise<User> {
|
|
return prisma.client.user.update({
|
|
where,
|
|
data,
|
|
select: {
|
|
id: true,
|
|
username: true,
|
|
role: true,
|
|
createdAt: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function deleteUser(
|
|
userId: string,
|
|
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> {
|
|
const { client } = prisma;
|
|
|
|
const websites = await client.website.findMany({
|
|
where: { userId },
|
|
});
|
|
|
|
let websiteIds = [];
|
|
|
|
if (websites.length > 0) {
|
|
websiteIds = websites.map(a => a.id);
|
|
}
|
|
|
|
return prisma
|
|
.transaction([
|
|
client.websiteEvent.deleteMany({
|
|
where: { websiteId: { in: websiteIds } },
|
|
}),
|
|
client.session.deleteMany({
|
|
where: { websiteId: { in: websiteIds } },
|
|
}),
|
|
client.website.updateMany({
|
|
data: {
|
|
deletedAt: new Date(),
|
|
},
|
|
where: { id: { in: websiteIds } },
|
|
}),
|
|
client.user.update({
|
|
data: {
|
|
deletedAt: new Date(),
|
|
},
|
|
where: {
|
|
id: userId,
|
|
},
|
|
}),
|
|
])
|
|
.then(async data => {
|
|
if (cache.enabled) {
|
|
const ids = websites.map(a => a.id);
|
|
|
|
for (let i = 0; i < ids.length; i++) {
|
|
await cache.deleteWebsite(`website:${ids[i]}`);
|
|
}
|
|
}
|
|
|
|
return data;
|
|
});
|
|
}
|