mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 23:27:12 +01:00
Dev (#1702)
* 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>
This commit is contained in:
parent
94848cc41b
commit
8732d056dd
165 changed files with 3370 additions and 6268 deletions
72
queries/admin/team.ts
Normal file
72
queries/admin/team.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import { Prisma, Team } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
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({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
|
||||
return prisma.client.team.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,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
export async function updateTeam(
|
||||
data: Prisma.TeamUpdateInput,
|
||||
where: Prisma.TeamWhereUniqueInput,
|
||||
): Promise<Team> {
|
||||
return prisma.client.team.update({
|
||||
data: {
|
||||
...data,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteTeam(teamId: string): Promise<Team> {
|
||||
return prisma.client.team.update({
|
||||
data: {
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: {
|
||||
id: teamId,
|
||||
},
|
||||
});
|
||||
}
|
||||
56
queries/admin/teamUser.ts
Normal file
56
queries/admin/teamUser.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
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,
|
||||
role: string,
|
||||
): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.create({
|
||||
data: {
|
||||
id: uuid(),
|
||||
userId,
|
||||
teamId,
|
||||
role,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateTeamUser(
|
||||
data: Prisma.TeamUserUpdateInput,
|
||||
where: Prisma.TeamUserWhereUniqueInput,
|
||||
): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.update({
|
||||
data,
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteTeamUser(teamUserId: string): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.update({
|
||||
data: {
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: {
|
||||
id: teamUserId,
|
||||
},
|
||||
});
|
||||
}
|
||||
159
queries/admin/user.ts
Normal file
159
queries/admin/user.ts
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
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;
|
||||
});
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createUser(data) {
|
||||
return prisma.client.user.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function deleteUser(userId) {
|
||||
const { client } = prisma;
|
||||
|
||||
const websites = await client.website.findMany({
|
||||
where: { userId },
|
||||
});
|
||||
|
||||
let websiteIds = [];
|
||||
|
||||
if (websites.length > 0) {
|
||||
websiteIds = websites.map(a => a.id);
|
||||
}
|
||||
|
||||
return client
|
||||
.$transaction([
|
||||
client.websiteEvent.deleteMany({
|
||||
where: { websiteId: { in: websiteIds } },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { websiteId: { in: websiteIds } },
|
||||
}),
|
||||
client.website.deleteMany({
|
||||
where: { userId },
|
||||
}),
|
||||
client.user.delete({
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getUser(where) {
|
||||
return prisma.client.user.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getUsers() {
|
||||
return prisma.client.user.findMany({
|
||||
orderBy: [
|
||||
{ isAdmin: 'desc' },
|
||||
{
|
||||
username: 'asc',
|
||||
},
|
||||
],
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
isAdmin: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function updateUser(data, where) {
|
||||
return prisma.client.user.update({
|
||||
where,
|
||||
data,
|
||||
});
|
||||
}
|
||||
110
queries/admin/website.ts
Normal file
110
queries/admin/website.ts
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
import { Prisma, Website } from '@prisma/client';
|
||||
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> {
|
||||
return prisma.client.website
|
||||
.create({
|
||||
data,
|
||||
})
|
||||
.then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.storeWebsite(data);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateWebsite(
|
||||
websiteId,
|
||||
data: Prisma.WebsiteUpdateInput | Prisma.WebsiteUncheckedUpdateInput,
|
||||
): Promise<Website> {
|
||||
return prisma.client.website.update({
|
||||
where: {
|
||||
id: websiteId,
|
||||
},
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function resetWebsite(
|
||||
websiteId,
|
||||
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
||||
const { client, transaction } = prisma;
|
||||
|
||||
const { revId } = await getWebsite({ id: websiteId });
|
||||
|
||||
return transaction([
|
||||
client.websiteEvent.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.website.update({ where: { id: websiteId }, data: { revId: revId + 1 } }),
|
||||
]).then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.storeWebsite(data[2]);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteWebsite(websiteId: string) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => deleteWebsiteRelationalQuery(websiteId),
|
||||
[CLICKHOUSE]: () => deleteWebsiteClickhouseQuery(websiteId),
|
||||
});
|
||||
}
|
||||
|
||||
async function deleteWebsiteRelationalQuery(
|
||||
websiteId,
|
||||
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
||||
const { client, transaction } = prisma;
|
||||
|
||||
return transaction([
|
||||
client.websiteEvent.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.website.delete({
|
||||
where: { id: websiteId },
|
||||
}),
|
||||
]).then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.deleteWebsite(websiteId);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
async function deleteWebsiteClickhouseQuery(websiteId): Promise<Website> {
|
||||
return prisma.client.website.update({
|
||||
data: {
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
where: { id: websiteId },
|
||||
});
|
||||
}
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function createWebsite(userId, data) {
|
||||
return prisma.client.website
|
||||
.create({
|
||||
data: {
|
||||
user: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
...data,
|
||||
},
|
||||
})
|
||||
.then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.storeWebsite(data);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function deleteWebsite(id) {
|
||||
const { client, transaction } = prisma;
|
||||
|
||||
return transaction([
|
||||
client.websiteEvent.deleteMany({
|
||||
where: { websiteId: id },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { websiteId: id },
|
||||
}),
|
||||
client.website.delete({
|
||||
where: { id },
|
||||
}),
|
||||
]).then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.deleteWebsite(id);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAllWebsites() {
|
||||
let data = await prisma.client.website.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
userId: 'asc',
|
||||
},
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return data.map(i => ({ ...i, user: i.user.username }));
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getUserWebsites(userId) {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getWebsite(where) {
|
||||
return prisma.client.website.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import { getWebsite } from 'queries';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function resetWebsite(id) {
|
||||
const { client, transaction } = prisma;
|
||||
|
||||
const { revId } = await getWebsite({ id });
|
||||
|
||||
return transaction([
|
||||
client.websiteEvent.deleteMany({
|
||||
where: { websiteId: id },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { websiteId: id },
|
||||
}),
|
||||
client.website.update({ where: { id }, data: { revId: revId + 1 } }),
|
||||
]).then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.storeWebsite(data[2]);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function updateWebsite(id, data) {
|
||||
return prisma.client.website.update({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
data,
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue