mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
fix(api): 🐛 Add default role with default permissions in roles API
This commit is contained in:
parent
11369649c0
commit
d2f5aaf114
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -182,7 +182,8 @@ describe(meta.route, () => {
|
|||
|
||||
expect(response2.ok).toBe(true);
|
||||
const roles = await response2.json();
|
||||
expect(roles).toHaveLength(2);
|
||||
// The default role will still be there
|
||||
expect(roles).toHaveLength(3);
|
||||
expect(roles).toContainEqual({
|
||||
id: roleNotLinked.id,
|
||||
name: "test2",
|
||||
|
|
@ -228,7 +229,8 @@ describe(meta.route, () => {
|
|||
|
||||
expect(response2.ok).toBe(true);
|
||||
const roles = await response2.json();
|
||||
expect(roles).toHaveLength(1);
|
||||
// The default role will still be there
|
||||
expect(roles).toHaveLength(2);
|
||||
expect(roles).not.toContainEqual({
|
||||
name: "test",
|
||||
permissions: ADMIN_ROLES,
|
||||
|
|
|
|||
|
|
@ -45,7 +45,10 @@ export default (app: Hono) =>
|
|||
return errorResponse("Unauthorized", 401);
|
||||
}
|
||||
|
||||
const userRoles = await Role.getUserRoles(user.id);
|
||||
const userRoles = await Role.getUserRoles(
|
||||
user.id,
|
||||
user.getUser().isAdmin,
|
||||
);
|
||||
const role = await Role.fromId(id);
|
||||
|
||||
if (!role) {
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ describe(meta.route, () => {
|
|||
|
||||
expect(response.ok).toBe(true);
|
||||
const roles = await response.json();
|
||||
expect(roles).toHaveLength(1);
|
||||
expect(roles).toHaveLength(2);
|
||||
expect(roles[0]).toMatchObject({
|
||||
name: "test",
|
||||
permissions: ADMIN_ROLES,
|
||||
|
|
@ -58,5 +58,14 @@ describe(meta.route, () => {
|
|||
visible: true,
|
||||
icon: "test",
|
||||
});
|
||||
|
||||
expect(roles[1]).toMatchObject({
|
||||
name: "Default",
|
||||
permissions: config.permissions.default,
|
||||
priority: 0,
|
||||
description: "Default role for all users",
|
||||
visible: false,
|
||||
icon: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -27,7 +27,10 @@ export default (app: Hono) =>
|
|||
return errorResponse("Unauthorized", 401);
|
||||
}
|
||||
|
||||
const userRoles = await Role.getUserRoles(user.id);
|
||||
const userRoles = await Role.getUserRoles(
|
||||
user.id,
|
||||
user.getUser().isAdmin,
|
||||
);
|
||||
|
||||
return jsonResponse(userRoles.map((r) => r.toAPI()));
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue