mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
fix(api): 🐛 Add default role with default permissions in roles API
This commit is contained in:
parent
11369649c0
commit
d2f5aaf114
5 changed files with 58 additions and 7 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { config } from "config-manager";
|
||||
import {
|
||||
type InferInsertModel,
|
||||
type InferSelectModel,
|
||||
|
|
@ -36,15 +37,48 @@ export class Role {
|
|||
return new Role(found);
|
||||
}
|
||||
|
||||
public static async getUserRoles(userId: string) {
|
||||
public static async getUserRoles(userId: string, isAdmin: boolean) {
|
||||
return (
|
||||
await db.query.RoleToUsers.findMany({
|
||||
where: (role, { eq }) => eq(role.userId, userId),
|
||||
with: {
|
||||
role: true,
|
||||
user: {
|
||||
columns: {
|
||||
isAdmin: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
).map((r) => new Role(r.role));
|
||||
)
|
||||
.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,
|
||||
}),
|
||||
]
|
||||
: [],
|
||||
);
|
||||
}
|
||||
|
||||
public static async manyFromSql(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue