2024-06-12 06:57:04 +02:00
|
|
|
import { proxyUrl } from "@/response";
|
2024-06-10 04:14:36 +02:00
|
|
|
import { config } from "config-manager";
|
2024-06-08 05:31:17 +02:00
|
|
|
import {
|
|
|
|
|
type InferInsertModel,
|
|
|
|
|
type InferSelectModel,
|
|
|
|
|
type SQL,
|
|
|
|
|
and,
|
|
|
|
|
desc,
|
|
|
|
|
eq,
|
|
|
|
|
inArray,
|
|
|
|
|
} from "drizzle-orm";
|
|
|
|
|
import { db } from "~/drizzle/db";
|
|
|
|
|
import { RoleToUsers, Roles } from "~/drizzle/schema";
|
2024-06-13 02:45:07 +02:00
|
|
|
import { BaseInterface } from "./base";
|
2024-06-08 05:31:17 +02:00
|
|
|
|
2024-06-13 02:45:07 +02:00
|
|
|
export type RoleType = InferSelectModel<typeof Roles>;
|
|
|
|
|
|
|
|
|
|
export class Role extends BaseInterface<typeof Roles> {
|
|
|
|
|
async reload(): Promise<void> {
|
|
|
|
|
const reloaded = await Role.fromId(this.data.id);
|
|
|
|
|
|
|
|
|
|
if (!reloaded) {
|
|
|
|
|
throw new Error("Failed to reload role");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.data = reloaded.data;
|
|
|
|
|
}
|
2024-06-08 05:31:17 +02:00
|
|
|
|
|
|
|
|
public static async fromId(id: string | null): Promise<Role | null> {
|
|
|
|
|
if (!id) return null;
|
|
|
|
|
|
|
|
|
|
return await Role.fromSql(eq(Roles.id, id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async fromIds(ids: string[]): Promise<Role[]> {
|
|
|
|
|
return await Role.manyFromSql(inArray(Roles.id, ids));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async fromSql(
|
|
|
|
|
sql: SQL<unknown> | undefined,
|
|
|
|
|
orderBy: SQL<unknown> | undefined = desc(Roles.id),
|
|
|
|
|
) {
|
|
|
|
|
const found = await db.query.Roles.findFirst({
|
|
|
|
|
where: sql,
|
|
|
|
|
orderBy,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!found) return null;
|
|
|
|
|
return new Role(found);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 04:14:36 +02:00
|
|
|
public static async getUserRoles(userId: string, isAdmin: boolean) {
|
2024-06-08 05:31:17 +02:00
|
|
|
return (
|
|
|
|
|
await db.query.RoleToUsers.findMany({
|
|
|
|
|
where: (role, { eq }) => eq(role.userId, userId),
|
|
|
|
|
with: {
|
|
|
|
|
role: true,
|
2024-06-10 04:14:36 +02:00
|
|
|
user: {
|
|
|
|
|
columns: {
|
|
|
|
|
isAdmin: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-06-08 05:31:17 +02:00
|
|
|
},
|
|
|
|
|
})
|
2024-06-10 04:14:36 +02:00
|
|
|
)
|
|
|
|
|
.map((r) => new Role(r.role))
|
|
|
|
|
.concat(
|
|
|
|
|
new Role({
|
|
|
|
|
id: "default",
|
|
|
|
|
name: "Default",
|
|
|
|
|
permissions: config.permissions.default,
|
|
|
|
|
priority: 0,
|
|
|
|
|
description: "Default role for all users",
|
|
|
|
|
visible: false,
|
|
|
|
|
icon: null,
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.concat(
|
|
|
|
|
isAdmin
|
|
|
|
|
? [
|
|
|
|
|
new Role({
|
|
|
|
|
id: "admin",
|
|
|
|
|
name: "Admin",
|
|
|
|
|
permissions: config.permissions.admin,
|
|
|
|
|
priority: 2 ** 31 - 1,
|
|
|
|
|
description:
|
|
|
|
|
"Default role for all administrators",
|
|
|
|
|
visible: false,
|
|
|
|
|
icon: null,
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
: [],
|
|
|
|
|
);
|
2024-06-08 05:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async manyFromSql(
|
|
|
|
|
sql: SQL<unknown> | undefined,
|
|
|
|
|
orderBy: SQL<unknown> | undefined = desc(Roles.id),
|
|
|
|
|
limit?: number,
|
|
|
|
|
offset?: number,
|
2024-06-08 06:57:29 +02:00
|
|
|
extra?: Parameters<typeof db.query.Roles.findMany>[0],
|
2024-06-08 05:31:17 +02:00
|
|
|
) {
|
|
|
|
|
const found = await db.query.Roles.findMany({
|
|
|
|
|
where: sql,
|
|
|
|
|
orderBy,
|
|
|
|
|
limit,
|
|
|
|
|
offset,
|
|
|
|
|
with: extra?.with,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return found.map((s) => new Role(s));
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 02:45:07 +02:00
|
|
|
async update(newRole: Partial<RoleType>): Promise<RoleType> {
|
|
|
|
|
await db.update(Roles).set(newRole).where(eq(Roles.id, this.id));
|
|
|
|
|
|
|
|
|
|
const updated = await Role.fromId(this.data.id);
|
|
|
|
|
|
|
|
|
|
if (!updated) {
|
|
|
|
|
throw new Error("Failed to update role");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updated.data;
|
2024-06-08 05:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-13 02:45:07 +02:00
|
|
|
async save(): Promise<RoleType> {
|
|
|
|
|
return this.update(this.data);
|
2024-06-08 05:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-13 02:45:07 +02:00
|
|
|
async delete(ids: string[]): Promise<void>;
|
|
|
|
|
async delete(): Promise<void>;
|
|
|
|
|
async delete(ids?: unknown): Promise<void> {
|
|
|
|
|
if (Array.isArray(ids)) {
|
|
|
|
|
await db.delete(Roles).where(inArray(Roles.id, ids));
|
|
|
|
|
} else {
|
|
|
|
|
await db.delete(Roles).where(eq(Roles.id, this.id));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async insert(
|
|
|
|
|
data: InferInsertModel<typeof Roles>,
|
|
|
|
|
): Promise<Role> {
|
|
|
|
|
const inserted = (await db.insert(Roles).values(data).returning())[0];
|
|
|
|
|
|
|
|
|
|
const role = await Role.fromId(inserted.id);
|
|
|
|
|
|
|
|
|
|
if (!role) {
|
|
|
|
|
throw new Error("Failed to insert role");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return role;
|
2024-06-08 05:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async linkUser(userId: string) {
|
|
|
|
|
await db.insert(RoleToUsers).values({
|
|
|
|
|
userId,
|
|
|
|
|
roleId: this.id,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async unlinkUser(userId: string) {
|
|
|
|
|
await db
|
|
|
|
|
.delete(RoleToUsers)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(RoleToUsers.roleId, this.id),
|
|
|
|
|
eq(RoleToUsers.userId, userId),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get id() {
|
2024-06-13 02:45:07 +02:00
|
|
|
return this.data.id;
|
2024-06-08 05:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public toAPI() {
|
|
|
|
|
return {
|
|
|
|
|
id: this.id,
|
2024-06-13 02:45:07 +02:00
|
|
|
name: this.data.name,
|
|
|
|
|
permissions: this.data.permissions,
|
|
|
|
|
priority: this.data.priority,
|
|
|
|
|
description: this.data.description,
|
|
|
|
|
visible: this.data.visible,
|
|
|
|
|
icon: proxyUrl(this.data.icon),
|
2024-06-08 05:31:17 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|