forked from BRETT/BRETT
Initial Commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { permissions, rolePermissions, userRoles } from '../db/schema'
|
||||
|
||||
export async function getUserPermissions(userId: number) {
|
||||
const rows = await useDb()
|
||||
.select({ key: permissions.key })
|
||||
.from(rolePermissions)
|
||||
.innerJoin(permissions, eq(rolePermissions.permissionId, permissions.id))
|
||||
.innerJoin(userRoles, eq(rolePermissions.roleId, userRoles.roleId))
|
||||
.where(eq(userRoles.userId, userId))
|
||||
return [...new Set(rows.map(r => r.key))]
|
||||
}
|
||||
|
||||
export async function getRolePermissions() {
|
||||
const rows = await useDb()
|
||||
.select({ roleId: rolePermissions.roleId, key: permissions.key })
|
||||
.from(rolePermissions)
|
||||
.innerJoin(permissions, eq(rolePermissions.permissionId, permissions.id))
|
||||
const byRole = new Map<number, string[]>()
|
||||
for (const row of rows) {
|
||||
const list = byRole.get(row.roleId) ?? []
|
||||
list.push(row.key)
|
||||
byRole.set(row.roleId, list)
|
||||
}
|
||||
return byRole
|
||||
}
|
||||
Reference in New Issue
Block a user